[英]A union B minus A intersection B
我看到有些方法提供矩形
CGRect CGRectIntersection(CGRect r1, CGRect r2)
CGRectUnion(CGRect r1, CGRect r2)
但是,是否有一種方法提供的區域或坐標列表包含A聯合B減去A交點B。其中A是一個大矩形,B是一個完全包含在其中的小矩形?
您將如何表示坐標列表,將使用哪種數據類型? CGRect使用浮點坐標,因此顯然您不能擁有一組(x,y)點。 我想您將聯合減去交叉點分解為一堆單獨的CGRect,但是這似乎很麻煩。 也許您可能對要實現的目標更加清楚?
要嘗試回答您的問題,怎么樣:
BOOL CGPointIsInUnionButNotIntersectionOfRects(CGRect r1, CGRect r2, CGPoint p)
{
CGRect union = CGRectUnion(r1, r2);
CGRect intersection = CGRectIntersection(r1, r2);
return CGRectContainsPoint(union) && !CGRectContainsPoint(instersection);
}
我猜您是通過“提供區域”來表示“繪制到屏幕上”:使用帶有兩個矩形和奇偶填充規則的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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.