-2
//Test code to print all coordinates

CGRect b=CGRectMake(0, 0, 4, 3);
//top left
float topLX=CGRectGetMinX(b);
float topLY=CGRectGetMinY(b);

NSLog(@"(%f,%f)",topLX,topLY);

//top right
float topRX=CGRectGetMaxX(b);
float topRY=CGRectGetMinY(b);

NSLog(@"(%f,%f)",topRX,topRY);

//bottom left
float bottomLX=CGRectGetMinX(b);
float bottomLY=CGRectGetMaxY(b);

NSLog(@"(%f,%f)",bottomLX,bottomLY);

//bottom right
float bottomRX=CGRectGetMaxX(b);
float bottomRY=CGRectGetMaxY(b);

NSLog(@"(%f,%f)",bottomRX,bottomRY);


//Sample CGRectContainsPoint Test

CGRect d=CGRectMake(0, 0, 4, 3);
CGPoint p=CGPointMake(0, 0);
CGPoint o=CGPointMake(4, 3);

BOOL contains=CGRectContainsPoint(d, p);
BOOL contains1=CGRectContainsPoint(d, o);

if(contains) NSLog(@"yes"); else NSLog(@"no");
//This will print yes because p is inside rect b


if(contains1) NSLog(@"yes");else NSLog(@"no");
//This will print no because o is inside rect b

NSLog Output:
2014-06-16 16:08:37.291 Pirate Adventure[7564:60b] (0.000000,0.000000)
2014-06-16 16:08:37.291 Pirate Adventure[7564:60b] (4.000000,0.000000)
2014-06-16 16:08:37.292 Pirate Adventure[7564:60b] (0.000000,3.000000)
2014-06-16 16:08:37.292 Pirate Adventure[7564:60b] (4.000000,3.000000)
2014-06-16 16:08:37.292 Pirate Adventure[7564:60b] yes
2014-06-16 16:08:37.293 Pirate Adventure[7564:60b] no

我一直在绘制一个 CGRect 和一系列平铺对象。然而,无论我如何绘制它,我都无法得到一个映射 4 个点(宽)和 3 个点(高)的正方形。此外,我已经彻底搜索了这里以及一整天的反复试验。

4

2 回答 2

2

来自CGRectContainsPoint的文档。

讨论

如果一个点的坐标位于矩形内或最小 X最小 Y 边缘上,则认为该点位于矩形内。

似乎您正在检查最大边缘点,这就是为什么它false在您第二次调用时返回-CGRectContainsPoint.

于 2014-06-16T23:34:05.870 回答
0

经过一天的工作,我发现这是一个从零开始的 CGRect 意思是 0-3 等于四个点。0-2也是三分。因此,点数 0,0 为原点,因此 CGRect 的四个点为:0,0 - 3,0 2,0 - 2,3

于 2014-06-17T14:35:03.993 回答