场景
假设有两个不相交的矩形,宽 10 磅,高 5 磅。下图中的x/y值表示两个矩形角点的坐标。
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
下面是设置这些矩形的代码:
CGRect rect1 = CGRectMake(0.0f, 0.0f, 10.0f, 5.0f);
CGRect rect2 = CGRectMake(0.0f, 5.0f, 10.0f, 5.0f);
CGRectGetMaxX
CGRectGetMaxX
Core Graphics 函数的返回值记录如下:
The largest value of the x-coordinate for the rectangle.
这是使用该函数的代码片段:
CGFloat maxX = CGRectGetMaxX(rect1);
根据文档,我希望 maxX
为 9。但是,maxX
为 10。嗯?
CGRectIntersection
CGRectIntersection
Core Graphics 函数的返回值记录如下:
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.
这是使用该函数的代码片段:
CGRect intersectionRect = CGRectIntersection(rect1, rect2);
bool intersectionRectIsNull = CGRectIsNull(intersectionRect);
根据文档,我希望 intersectionRectIsNull
为真,因为 rect1
和 rect2
显然不相交。但是,intersectionRectIsNull
是错误的。咦???
如果我们在调试器中检查 intersectionRect
,我们会看到:
(CGRect) $0 = origin=(x=0, y=5) size=(width=10, height=0)
是的,现在很清楚为什么 CGRectIsNull
返回 false,但为什么 intersectionRect
不是这个?
(CGRect) $0 = origin=(x=+Inf, y=+Inf) size=(width=0, height=0)
问题
有人可以解释为什么这些函数的文档没有错吗?我一定是错过了什么,但对于我的生活,我看不到它是什么。
最佳答案
您的图表不正确。它与您的 CGRectMake
调用不匹配。
原点为 0 的 10 宽矩形的最大 X 值为 10,正如您得到的那样。
图表中的 9
应为 10
,图表中的 4
应为 5
。
鉴于对 CGRectMake
的两次调用,这两个矩形共享一个公共(public)边。它们之间没有间隙。
请记住,0 + 10 是 10,而不是 9,0 + 5 是 5,而不是 4。
关于ios - 有人可以解释 CGRectGetMaxX 和 CGRectIntersection 的结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25938460/