联合B减A交点B.

时间:2013-05-16 13:55:21

标签: iphone ios cgrect cgpath

我看到有提供矩形的方法

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

但是有没有一种方法可以提供A联合B减去A交点B的坐标区域或列表。其中A是一个大矩形,B是一个完全包含在其中的小矩形?

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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();