从 CGRect 中减去 CGRect——其中最大的一块不包含另一个

发布于 2024-10-14 04:36:38 字数 2151 浏览 0 评论 0原文

如何从一个 CGRect 中减去另一个?我希望结果 R1 - R2 是 R1 中不与 R2 相交的最大子矩形。

示例1

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

R3 = CGRectSubstract(R2,R1);

            +----------------------+
            |                      |
            |                      |
            |                      |
            |          R3          |
            |                      |
            |                      |
            |                      |
            +----------------------+

示例2

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

R3 = CGRectSubstract(R2,R1);

+-----------------------+
|                       |
|                       |
|                       |
|          R3           |
|                       |
|                       |
|                       |
+-----------------------+

示例3

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

R3 = CGRectSubstract(R2,R1);

+----------------------------------+
|                                  |
|                                  |
|              R3                  |
|                                  |
+----------------------------------+



How I can substract one CGRect from another? I want the result R1 - R2 to be the largest subrectangle of R1 that does not intersect R2.

Example 1:

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

R3 = CGRectSubstract(R2,R1);

            +----------------------+
            |                      |
            |                      |
            |                      |
            |          R3          |
            |                      |
            |                      |
            |                      |
            +----------------------+

Example 2:

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

R3 = CGRectSubstract(R2,R1);

+-----------------------+
|                       |
|                       |
|                       |
|          R3           |
|                       |
|                       |
|                       |
+-----------------------+

Example 3:

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

R3 = CGRectSubstract(R2,R1);

+----------------------------------+
|                                  |
|                                  |
|              R3                  |
|                                  |
+----------------------------------+



如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江城子 2024-10-21 04:36:39

你的定义相当模糊,到底是水平减法还是垂直减法呢?我建议结合使用 CGRectIntersection 和 CGRectDivide,并指定一个方向以消除歧义。

(未经测试,甚至编译)

CGRect rectSubtract(CGRect r1, CGRect r2, CGRectEdge edge) {
    // Find how much r1 overlaps r2
    CGRect intersection = CGRectIntersection(r1, r2);

    // If they don't intersect, just return r1. No subtraction to be done
    if (CGRectIsNull(intersection)) {
        return r1;
    }

    // Figure out how much we chop off r1
    float chopAmount = (edge == CGRectMinXEdge || edge == CGRectMaxXEdge)
                       ? intersection.size.width
                       : intersection.size.height;

    CGRect r3, throwaway;
    // Chop
    CGRectDivide(r1, &throwaway, &r3, chopAmount, edge);
    return r3;
}

Your definition is fairly ambiguous, what says whether the subtraction is horizontal or vertical? I recommend using a combination of CGRectIntersection and CGRectDivide, along with specifying a direction to remove ambiguity.

(not tested, or even compiled)

CGRect rectSubtract(CGRect r1, CGRect r2, CGRectEdge edge) {
    // Find how much r1 overlaps r2
    CGRect intersection = CGRectIntersection(r1, r2);

    // If they don't intersect, just return r1. No subtraction to be done
    if (CGRectIsNull(intersection)) {
        return r1;
    }

    // Figure out how much we chop off r1
    float chopAmount = (edge == CGRectMinXEdge || edge == CGRectMaxXEdge)
                       ? intersection.size.width
                       : intersection.size.height;

    CGRect r3, throwaway;
    // Chop
    CGRectDivide(r1, &throwaway, &r3, chopAmount, edge);
    return r3;
}
思念满溢 2024-10-21 04:36:39
CGRect newRect = CGRectMake(0, 0, rect2.size.width - rect1.size.width, rect2.size.height - rect1.size.height);

为了回应你的插图,我在这里给你的这段代码将完全按照你想要的方式进行(假设你不关心原点 XY 坐标)。我浏览了 CGGeometry 函数的 文档,并且似乎没有定义 CGRectDifference 或其他此类方法。然而,有CGRectUnion,但它的作用与您正在寻找的相反。

CGRect newRect = CGRectMake(0, 0, rect2.size.width - rect1.size.width, rect2.size.height - rect1.size.height);

In response to your illustration, this code I've given you here will do exactly what you want (assuming you don't care about the origin XY coordinates). I've looked through the docs for CGGeometry functions, and there doesn't seem to be a CGRectDifference or other such method defined. There is, however, CGRectUnion, but that does the opposite of what you are looking for.

放手` 2024-10-21 04:36:39

可能会这样:

CGRect frame = CGRectMake(0, 0, 320, 480);
float aWidth  = frame.size.width; /* say for instance 320 */
float aHeight = frame.size.height; /* say for instance 480 */

int final = aWidth - aHeight;
NSLog(@"Should be -160, your answer: %i",final);

Would probably go something like this:

CGRect frame = CGRectMake(0, 0, 320, 480);
float aWidth  = frame.size.width; /* say for instance 320 */
float aHeight = frame.size.height; /* say for instance 480 */

int final = aWidth - aHeight;
NSLog(@"Should be -160, your answer: %i",final);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文