Thursday, August 25, 2011

CGRect contains another CGRect



















 If you have 2 rectangles and you want to find out if one rectangle is inside the other, following code will do:

     CGRect b=CGRectMake(40, 50, 240, 150);
    CGRect c=CGRectMake(75, 80, 85, 90);
    
    BOOL contains=CGRectContainsRect(b, c);
    BOOL contains1=CGRectContainsRect(c, b);
    
    if(contains) NSLog(@"yes"); else NSLog(@"no");
    //This will print yes because b contains c. 
    //NOTE: c should be completely inside b.
    
    
    if(contains1) NSLog(@"yes");else NSLog(@"no");
    //This will print no because b doesn't contain c
    
    

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

2 comments: