我看到有提供矩形的方法
CGRect CGRectIntersection(CGRect r1, CGRect r2)
CGRectUnion(CGRect r1, CGRect r2)
但是有没有一种方法可以提供具有 A 联合 B 减去 A 交点 B 的坐标区域或列表。其中 A 是一个大矩形,B 是一个完全包含在其中的小矩形?
I guess you mean "draw to screen" by "provide the area": Use a CGPath with two rectangles and the even-odd filling rule.
// 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();
How would you represent the list of coordinates, what data type would you use? CGRects use float coordinates, so clearly you cannot have a set of (x, y) points. I guess you break down the union minus the intersection into a bunch of separate CGRects, but it seems like a lot of hassle. Perhaps you could be a bit clearer on exactly what you are trying to achieve?
To attempt to answer your question, how about something like:
BOOL CGPointIsInUnionButNotIntersectionOfRects(CGRect r1, CGRect r2, CGPoint p)
{
CGRect union = CGRectUnion(r1, r2);
CGRect intersection = CGRectIntersection(r1, r2);
return CGRectContainsPoint(union) && !CGRectContainsPoint(instersection);
}