Thursday, August 25, 2011

Find if CGRect contains a point

So we have a point P(200,60) inside the rect and another point O(200,400) outside the rectangle. Now we want to find it out using iPhone code. Following is how we do it

       CGRect b=CGRectMake(40, 50, 280, 200);
    CGPoint p=CGPointMake(200, 60);
    CGPoint o=CGPointMake(200, 400);
    BOOL contains=CGRectContainsPoint(b, p);
    BOOL contains1=CGRectContainsPoint(b, 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

If this is not what you were looking for CGRect, try this comprehensive LIST.


  1. Nice post! Can’t wait for the next one. Keep stuff like this coming.
    Asus Laptops


  2. Thanks for sharing information.
    recharge plans
    Videocon Telecom comes up with LOWEST & UNMATCHED TARIFF in Haryana; Offers ALL LOCAL CALLS at only 25P/min.