【问题标题】:Find whether two rectangles overlap [duplicate]查找两个矩形是否重叠[重复]
【发布时间】:2018-09-29 12:31:33
【问题描述】:

我试图找出两个矩形是否相互重叠。我有以下矩形表示为[x1,x2] x [y1,y2]

Rect 1 = [0.0, 1.0] x [0.0, 1.0]
Rect 2 = [0.7, 1.2] x [0.9, 1.5]

我只需要一个可以实现的伪代码来查找矩形是否相互重叠。

【问题讨论】:

标签: algorithm math rectangles overlapping


【解决方案1】:
  if ((Rect1.topLeft.x  >  Rect2.bottomRight.x) || (Rect1.bottomRight.x  <  Rect2.topLeft.x)  || (Rect1.topLeft.y > Rect2.bottomRight.y) || (   Rect1.bottomRight.y  <  Rect2.topLeft.y)):
            Then NOT OVERLAPPING
  else:
            OVERLAPPING

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
【解决方案2】:

如果你的矩形表示为[x1,x2] x [y1,y2]:

if (a.x1 > b.x2 || b.x1 > a.x2) return false; // check x-axis
if (a.y1 > b.y2 || b.y1 > a.y2) return false; // check y-axis
return true;

条件a.x1 &gt; b.x2 检查a 的左边界是否位于b 右边界的右侧,如果是,则矩形不会重叠。其他三个条件类似。

【讨论】:

    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多