Thursday, August 25, 2011

Find intersecting area of 2 CGRects























If you have 2 rectangles overlapping each other and you want to find the area of the intersecting area which is shown in the picture with checks pattern. You can find width and height of intersection area. Since area is a rectangle, you can simply do width x height.

       CGRect b=CGRectMake(40, 50, 240, 150);
    CGRect c=CGRectMake(100, 125, 80, 275);
    
    
     CGRect i=CGRectIntersection(c, b);
    
    NSLog(@"(%.0f,%.0f,%.0f,%.0f)",CGRectGetMinX(i),CGRectGetWidth(i),CGRectGetMinY(i),CGRectGetHeight(i));



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

No comments:

Post a Comment