0

When working with (contentless) SpriteKit nodes I bumped into an infinite rectangle

NSLog(@"%@", NSStringFromCGRect([node calculateAccumulatedFrame]));

which outputted

{{inf, inf}, {inf, inf}}

I thought to check for this with CGRectIsInfinite, but that test failed, which lead me to trying the following check

CGRect rect = [node calculateAccumulatedFrame];
if (rect.origin.x == INFINITY && rect.origin.y == INFINITY && 
    rect.size.width == INFINITY && rect.size.height == INFINITY) {
    if (!CGRectIsInfinite(rect)) {
        NSLog(@"I don't get it");
    }
}

Which outputs: I don't get it, which neatly summarises my position right now.

As pointed out by allestuetsmerweh in an answer the function does return true for CGRectInfinite, when I output this rectangle I get

{{-8.9884656743115785e+307, -8.9884656743115785e+307}, {1.7976931348623157e+308, 1.7976931348623157e+308}}

(the sizes are both DBL_MAX)

What's the reasoning behind CGRectInfinite being a rectangle with some values set to DBL_MAX i.s.o. a rectangle with all elements set to INFINITY? (while the API does return CGRect with all members set to INFINITY)
Or rather, why doesn't a rectangle with the elements set to INFINITY register as a CGRectIsInfinite?

nielsbot
  • 15,922
  • 4
  • 48
  • 73
Pieter
  • 17,435
  • 8
  • 50
  • 89

3 Answers3

3
CGRect r = CGRectInfinite;
NSLog(@"%i", CGRectIsInfinite(r)); //prints "1"

From the docs:

An infinite rectangle is one that has no defined bounds. Infinite rectangles can be created as output from a tiling filter. For example, the Core Image framework perspective tile filter creates an image whose extent is described by an infinite rectangle.

2

What's the reasoning behind CGRectInfinite being a rectangle with some values set to DBL_MAX i.s.o. a rectangle with all elements set to INFINITY?

Here are some guesses:

  1. IEEE 754 defines two infinities (+∞ and -∞). One has to be chosen (or there are ambiguous values for CGRectInfinite).
  2. Infinity is not really a number. This can lead to various issues when converting to a string for serialization.
  3. It's a bit awkward to work with infinite values in C.

And why doesn't a rectangle with the elements set to INFINITY register as a CGRectIsInfinite?

Because, as expected, CGRectIsInfinite compares its argument to CGRectInfinite (which has a special meaning in some APIs and so should be unambiguously identifiable).

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • I get what you're saying, but still, why does Apple's API choose to return a rect with all members set to INFINITY rather than CGRectInfinite? – Pieter Aug 05 '14 at 12:20
  • @Pieter So your question is really "Why is SpriteKit's usage of infinite rects in `calculateAccumulatedFrame` not consistent with other Apple APIs, like Core Image?". – Nikolai Ruhe Aug 05 '14 at 13:09
  • You could put it like that I suppose, or "Why is SpriteKit introducing a new kind of infinite rect that isn't considered infinite by Apple's own function CGRectIsInfinite" – Pieter Aug 05 '14 at 13:23
0

CG has defined the infinite rectangle, see CGRectInfinite. As a probable surprise to many, this rect is defined to have bounds ±CGFLOAT_MAX/2, with a finite size equal to CGFLOAT_MAX.

The CGRectIsInfinite() function does not tell you that the rectangle is infinite, despite documentation to the contrary. It tells you whether the rectangle is the infinite rect (which isn't er... actually infinite).

Ian Ollmann
  • 1,592
  • 9
  • 16