【问题标题】:Two Rectangles intersection两个矩形相交
【发布时间】:2023-03-12 21:40:02
【问题描述】:

我有两个矩形,每个矩形都有 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

判断两个矩形的交点是否为空的最佳方案是什么?

【问题讨论】:

标签: algorithm math pseudocode shapes


【解决方案1】:
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

【讨论】:

  • 如果一个矩形完全在另一个矩形内,则不起作用。
  • @AnkeshAnand 你能详细说明一下吗?当我运行这个算法时,它似乎可以很好地处理“完全内部”的情况。
  • @TopherHunt 它在这种情况下检测到一个没有任何交叉点的交叉点。
  • 啊,明白了。感谢您的澄清。我没有注意到这一点,因为就我而言,我关心的是 overlap,该算法可以完美处理。
  • @AnkeshAnand 交集表示布尔交集 - 即矩形 1 的某些区域是否与矩形 2 的某些区域重叠 - 不是矩形的“轮廓”是否相互交叉。
【解决方案2】:

最好的例子..

/**
 * 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);
}

还有另一种方式看到这个link ...并自己编写代码..

【讨论】:

    【解决方案3】:

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

    if (abs (x1 - x2) < w && abs (y1 - y2) < h) {
        // overlaps
    }
    

    【讨论】:

      【解决方案4】:

      我刚刚尝试了一个c程序并在下面写了。

      #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;
      }
      

      【讨论】:

        【解决方案5】:

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

        我认为它是一个垂直和水平滑动窗口 并想出这个:

        (B.Bottom > A.Top && B.Top A.Left && B.Left

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

        不是(B.Bottom A.Bottom || B.Right A.Right)

        1. B 高于 A
        2. B 低于 A
        3. B 在 A 的左边
        4. B 在 A 的右边

        【讨论】:

          【解决方案6】:

          如果矩形左下角和右上角的坐标是:
          (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
          

          【讨论】:

            【解决方案7】:

            如果( X1=Y2-H2 && Y2>=Y1+H1 ) 相交

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

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

            【讨论】:

            • 这意味着它不是一个通用的解决方案
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-11-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多