2013-05-16 20 views
0

我看到有提供矩形一名工会B再减去一个交点B

CGRect CGRectIntersection(CGRect r1, CGRect r2) 
CGRectUnion(CGRect r1, CGRect r2) 

方法,但在那里,提供有一个工会B减一路口B.凡区域或坐标列表的方法A是一个大的矩形,B是一个完整的小矩形?

+0

'CGRectContainsRect(rect1,rect2)'应该有帮助吗? 如果第一个矩形(rect1)包含第二个矩形(rect2),它将返回。然后,用'[rect2 frame]',你应该得到你想要的... – Larme

+0

这将是一个交集B ...但是我正在寻找一个联合B减去一个交集B –

+1

如果B完全包含在A那么不是联合B == A和A交集B == B? –

回答

0

您将如何表示坐标列表,您将使用哪种数据类型? CGRects使用浮点坐标,所以显然你不能有一组(x,y)点。我想你会把联合减去交叉点分解成一堆单独的CGRects,但它看起来很麻烦。也许你可以更清楚地知道你想要达到的目标?

要尝试回答你的问题,怎么样是这样的:

BOOL CGPointIsInUnionButNotIntersectionOfRects(CGRect r1, CGRect r2, CGPoint p) 
{ 
    CGRect union = CGRectUnion(r1, r2); 
    CGRect intersection = CGRectIntersection(r1, r2); 
    return CGRectContainsPoint(union) && !CGRectContainsPoint(instersection); 
} 
0

我猜你的意思是“提供区域”,“画到屏幕”:使用CGPath有两个矩形和奇偶填充规则。

// create a graphics context with the C-API of the QuartzCore framework 
// the graphics context is only required for drawing the subsequent drawing 
CGRect compositionBounds = CGRectMake(30, 30, 300, 508); 
UIGraphicsBeginImageContext(compositionBounds.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 

// create a mutable path with QuartzCore 
CGMutablePathRef p1 = CGPathCreateMutable(); 
CGRect r1 = CGRectMake(20, 300, 200, 100); 
CGPathAddRect(p1, nil, r1); 
CGPathAddRect(p1, nil, CGRectInset(r1, 10, 10)); 

// draw our mutable path to the context filling its area 
CGContextAddPath(ctx, p1); 
CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]); 
CGContextEOFillPath(ctx); 

// display our mutable path in an image view on screen 
UIImage *i = UIGraphicsGetImageFromCurrentImageContext(); 
UIImageView *iv = [[UIImageView alloc] initWithImage:i]; 
[self.view addSubview:iv]; 
iv.frame = self.view.frame; 

// release ressources created with the C-API of QuartzCore 
CGPathRelease(p1); 
UIGraphicsEndImageContext();