2013-05-16 15 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(); 
相關問題