玩两个 CGRect

playing with two CGRect

我有两个矩形 R1 和 R2

---------------------------------
|R1                             |
|                               |
|     ------------------        |
|     |                |        |
|     |  R2            |        |
|     |                |        |
---------------------------------

R1代表最大的,R2代表最小的。如果我们将 R2 减去 R1,我想将我们拥有的区域发送到以矩形作为输入的我的方法。这可能吗?任何躲避的措施

I would like to send the area that we have if we subtract R2 to R1 to my method that takes a rect as input. Is that even possible?

不,因为 areasize (CGSize) 表示,但是您的方法将 [=22= 作为输入]rect (CGRect).

但也许当您说 "area" 时,您实际上是指 "shape"?然后你将不得不自己绘制或导出形状。同样,在这种情况下,您不能将它发送到 "takes a rect as input" 的方法,因为您现在拥有的将是 path(例如 UIBezierPath 或 CGPath)或否则 绘图(例如 UIImage)。

因此,正如您从这个屏幕截图中看到的那样,很容易制作出矩形形状:

...但是描述该形状的信息不能表示为单纯的矩形。

查找区域

-(CGSize)areaOfRect:(CGRect)outerRect minusRect:(CGRect)innerRect
{
    return (outerRect.size.width - innerRect.size.width) * (outerRect.size.height - innerRect.size.height);
}

查找矩形(假设这两个矩形在同一范围内工作)

typedef void(^RectCompletion)(CGRect topRect,CGRect leftRect,CGRect rightRect,CGRect bottomRect);


-(void)rectsForRect:(CGRect)outerRect minusRect:(CGRect)innerRect withCompletion:(RectCompletion)completion
{
    // First sanity..
    // You'll need to build up a list of prerequisite checks

    if (innerRect.origin.x < outerRect.origin.x)
        outerRect.origin.x = innerRect.origin.x;

    if (innerRect.origin.y < outerRect.origin.y)
        outerRect.origin.y = innerRect.origin.y;

    if (innerRect.origin.x + innerRect.size.width > outerRect.origin.x + outerRect.size.width)
        NSLog(@"Trim to edge");

    if (innerRect.origin.x > outerRect.origin.x + outerRect.size.width || innerRect.origin.y > outerRect.origin.y + outerRect.size.width)
        NSLog(@"Trim to edge");

    // etc...


    // Assuming the rect is always in bounds below should give you the 4 rects surrounding the inner rect.. 

    // With overlap..

    CGRect topRect = CGRectMake(outerRect.origin.x, outerRect.origin.y, outerRect.size.width, innerRect.origin.y);
    CGRect leftRect = CGRectMake(outerRect.origin.x, outerRect.origin.y, innerRect.origin.x, outerRect.size.height);
    CGRect rightRect = CGRectMake(innerRect.origin.x + innerRect.size.width, outerRect.origin.y, outerRect.size.width - (innerRect.origin.x + innerRect.size.width), outerRect.size.height);
    CGRect bottomRect = CGRectMake(outerRect.origin.x, outerRect.size.height - (innerRect.origin.y + innerRect.size.height), outerRect.size.width, outerRect.size.height - (innerRect.origin.y + innerRect.size.height));


    // No overlap

    CGRect topRect = CGRectMake(outerRect.origin.x, outerRect.origin.y, outerRect.size.width, innerRect.origin.y);
    CGRect leftRect = CGRectMake(outerRect.origin.x, innerRect.origin.y, innerRect.origin.x, innerRect.size.height);
    CGRect rightRect = CGRectMake(innerRect.origin.x + innerRect.size.width, innerRect.origin.y, outerRect.size.width - (innerRect.origin.x + innerRect.size.width), innerRect.size.height);
    CGRect bottomRect = CGRectMake(outerRect.origin.x, outerRect.size.height - (innerRect.origin.y + innerRect.size.height), outerRect.size.width, outerRect.size.height - (innerRect.origin.y + innerRect.size.height));

    completion(topRect,leftRect,rightRect,bottomRect);
}