问题
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
?
回答1:
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:
- IEEE 754 defines two infinities (+∞ and -∞). One has to be chosen (or there are ambiguous values for
CGRectInfinite
). - Infinity is not really a number. This can lead to various issues when converting to a string for serialization.
- 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).
来源:https://stackoverflow.com/questions/25135341/why-is-there-a-difference-between-cgrectinfinite-and-a-cgrect-with-all-members-s