CGRectContainsPoint无法到达边界

时间:2014-06-16 23:19:49

标签: objective-c ios7 cgrect cgrectmake

//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和一系列tile对象。然而,无论我如何绘制它,我都无法得到一个正方形,它可以划出4个点(宽)和3个点(高)。此外,我在这里进行了彻底的搜索以及整天的反复试验。

2 个答案:

答案 0 :(得分:2)

来自CGRectContainsPoint的文档。

  

讨论

     

如果是矩形,则在矩形内部考虑一个点   坐标位于矩形内或最小X 最小Y   边缘。

似乎您正在检查最大边缘点,这就是您第二次调用false时返回-CGRectContainsPoint的原因。

答案 1 :(得分:0)

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