57

我有两个矩形,每个矩形都有 4 个值:

左侧位置X、顶部位置Y、宽度W和高度H

X1, Y1, H1, W1
X2, Y2, H2, W2

矩形不旋转,如下所示:

+--------------------> X axis
|
|    (X,Y)      (X+W, Y)
|    +--------------+
|    |              |
|    |              |
|    |              |
|    +--------------+
v    (X, Y+H)     (X+W,Y+H)

Y axis

确定两个矩形的交点是否为空的最佳解决方案是什么?

4

7 回答 7

94
if (X1+W1<X2 or X2+W2<X1 or Y1+H1<Y2 or Y2+H2<Y1):
    Intersection = Empty
else:
    Intersection = Not Empty

如果你有四个坐标 -((X,Y),(A,B))((X1,Y1),(A1,B1))- 而不是两个加上宽度和高度,它看起来像这样:

if (A<X1 or A1<X or B<Y1 or B1<Y):
    Intersection = Empty
else:
    Intersection = Not Empty
于 2012-11-15T01:58:30.873 回答
5

最好的例子..

/**
 * Check if two rectangles collide
 * x_1, y_1, width_1, and height_1 define the boundaries of the first rectangle
 * x_2, y_2, width_2, and height_2 define the boundaries of the second rectangle
 */
boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2)
{
  return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2);
}

还有另一种方式看到这个链接......并自己编码......

于 2014-05-26T11:50:51.180 回答
3

如果两个矩形具有相同的尺寸,您可以这样做:

if (abs (x1 - x2) < w && abs (y1 - y2) < h) {
    // overlaps
}
于 2017-07-07T06:25:23.163 回答
0

我刚刚尝试使用 ac 程序并在下面写道。

#include<stdio.h>

int check(int i,int j,int i1,int j1, int a, int b,int a1,int b1){
    return (\
    (((i>a) && (i<a1)) && ((j>b)&&(j<b1))) ||\ 
    (((a>i) && (a<i1)) && ((b>j)&&(b<j1))) ||\ 
    (((i1>a) && (i1<a1)) && ((j1>b)&&(j1<b1))) ||\ 
    (((a1>i) && (a1<i1)) && ((b1>j)&&(b1<j1)))\
    );  
}
int main(){
    printf("intersection test:(0,0,100,100),(10,0,1000,1000) :is %s\n",check(0,0,100,100,10,0,1000,1000)?"intersecting":"Not intersecting");
    printf("intersection test:(0,0,100,100),(101,101,1000,1000) :is %s\n",check(0,0,100,100,101,101,1000,1000)?"intersecting":"Not intersecting");
    return 0;
}
于 2013-09-15T14:57:34.760 回答
0

使用坐标系,其中 (0, 0) 是左上角。

我从垂直和水平滑动窗口的角度想到了它,并提出了这个:

(B.Bottom > A.Top && B.Top < A.Bottom) && (B.Right > A.Left && B.Left < A.Right)

如果您将德摩根定律应用于以下内容,您会得到什么:

不是(B.Bottom < A.Top || B.Top > A.Bottom || B.Right < A.Left || B.Left > A.Right)

  1. B高于A
  2. B低于A
  3. B在A的左边
  4. B在A的右边
于 2015-07-22T01:02:48.747 回答
0

如果矩形的左下角和右上角的坐标是:
(r1x1, r1y1), (r1x2, r1y2) 代表 rect1 和
(r2x1, r2y1), (r2x2, r2y2) 代表 rect2
(Python 类似下面的代码)

    intersect = False
    for x in [r1x1, r1x2]:
        if (r2x1<=x<=r2x2):
            for y in [r1y1, r1y2]:
                if (r2y1<=y<=r2y2):
                    intersect = True
                    return intersect
                else:
                    for Y in [r2y1, r2y2]:
                        if (r1y1<=Y<=r1y2):
                            intersect = True
                            return intersect
        else:  
            for X in [r2x1, r2x2]:
                if (r1x1<=X<=r1x2):
                    for y in [r2y1, r2y2]:
                        if (r1y1<=y<=r1y2):
                            intersect = True
                            return intersect
                        else:
                            for Y in [r1y1, r1y2]:
                                if (r2y1<=Y<=r2y2):
                                    intersect = True
                                    return intersect
    return intersect
于 2017-11-23T17:34:50.173 回答
-1

if( X1<=X2+W2 && X2<=X1+W1 && Y1>=Y2-H2 && Y2>=Y1+H1 ) 相交

在问题中,Y 是最高位置..

注意:此解决方案仅适用于矩形与 X / Y 轴对齐的情况。

于 2016-10-02T23:48:25.223 回答