Search code examples
objective-cios7cgrectcgrectmake

CGRectContainsPoint fails to reach the boundaries


//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

I have been working on drawing a CGRect and for a series of tile objects. Yet no matter how I draw it, I cannot get a square which maps 4 points across (wide) and 3 points tall (height). Also, I have searched thoroughly on here as well as trial and error all day.


Solution

  • After working all day, I discovered that this is a zero-based CGRect Meaning that the 0-3 equals four points. Also 0-2 is three points. Therefore, the points count 0,0 as origin, so the four points of the CGRect are: 0,0 - 3,0 2,0 - 2,3