Assume two non-intersecting rectangles, both 10 points wide and 5 points high. The x/y values in the following schema indicate the coordinates of the corner points of the two rectangles.
x/y = 0/0 +------------------+ x/y = 9/0
| |
| |
| |
| |
x/y = 0/4 +------------------+ x/y = 9/4
x/y = 0/5 +------------------+ x/y = 9/5
| |
| |
| |
| |
x/y = 0/9 +------------------+ x/y = 9/9
Here's the code to set up these rectangles:
CGRect rect1 = CGRectMake(0.0f, 0.0f, 10.0f, 5.0f);
CGRect rect2 = CGRectMake(0.0f, 5.0f, 10.0f, 5.0f);
The return value for the CGRectGetMaxX
Core Graphics function is documented like this:
The largest value of the x-coordinate for the rectangle.
Here is a code snippet that uses the function:
CGFloat maxX = CGRectGetMaxX(rect1);
From the documentation, I would expect maxX
to be 9. However, maxX
is 10. Huh?
The return value for the CGRectIntersection
Core Graphics function is documented like this:
A rectangle that represents the intersection of the two specified rectangles. If the two rectangles do not intersect, returns the null rectangle. To check for this condition, use CGRectIsNull.
Here is a code snippet that uses the function:
CGRect intersectionRect = CGRectIntersection(rect1, rect2);
bool intersectionRectIsNull = CGRectIsNull(intersectionRect);
From the documentation, I would expect intersectionRectIsNull
to be true since rect1
and rect2
are clearly not intersecting. However, intersectionRectIsNull
is false. Huh???
If we examine intersectionRect
in the debugger, we see this:
(CGRect) $0 = origin=(x=0, y=5) size=(width=10, height=0)
Yeah, so now it's clear why CGRectIsNull
is returning false, but why on earth is intersectionRect
not this?
(CGRect) $0 = origin=(x=+Inf, y=+Inf) size=(width=0, height=0)
Can someone explain why the documentation of these functions is NOT wrong? I must be missing something, but for the life of me I cannot see what it is.
Your diagram is incorrect. It doesn't match your CGRectMake
calls.
A rectangle that is 10 wide with an origin of 0 will have a max X of 10, just as you are getting.
The 9
in your diagram should be 10
and the 4
in your diagram should be 5
.
Given the two calls to CGRectMake
, the two rectangles share a common side. There is no gap between them.
Remember, 0 + 10 is 10, not 9 and 0 + 5 is 5, not 4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With