微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Math / Algorithm / JS:给定每个矩形的TopLeft(x0,y0)和Bottom-Right(x1,y1),如何确定2个矩形是否相交

我遇到了完成我的申请所需的数学问题,所以我正在寻求帮助.

给出2个(或更多,但基本上为2个)矩形,每个矩形有2个已知点:左上角(x1,y1)和右下角(x2,y2)(我可以找到这些信息的长度,如果是需要解决问题).

TL(x1, y1)
   +-----------------+
   |                 |
   |                 |       TL(x3, y3)
   |                 |            +---------------------------+
   +-----------------+            |                           |
               BR(x2, y2)         +---------------------------+
                                                         BR(x4, y4)

无论如何确定它们是否有交叉,在区域中,我的意思是,如果这个矩形的任何部分放在另一个的任何部分上?

搜索并找到了一些帮助,但它没有解决问题:

There are 4 conditions in which the two rectangles will not intersect:

  • The left edge of one rectangle is on the right side of the right edge of the other one, means that the first one is completely laid on the right side of the second one, no intersection.

  • The right edge of one rectangle is on the left side of the left edge of the other one, means that the first one is completely laid on the left side of the second one, no intersection.

  • The top edge of one rectangle is under the bottom edge of the other one, means that the first one is completely laid under the second one, no intersection.

  • The bottom edge of one rectangle is above the top edge of the other one, means that the first one is completely laid above the second one, no intersection.

所以我试图扭转条件,即如果没有发生上述4,则矩形可能会相交.但我仍然可以找到2个矩形不满足任何条件但仍然不相交的条件(如上图).

任何帮助都非常感谢,请告诉我这样做的方法或算法或代码(仅限JS和PHP).

非常感谢!

[X]

解决方法:

任意数量的交叉检测(“重叠”)的算法
矩形可以如下工作.使用两种数据结构.

>矩形左右边缘的x坐标的排序列表S.
>矩形的y坐标(底部/顶部)给出的间隔为070​​00 T.

该算法扫过x坐标的排序列表S:

>如果当前的x坐标是矩形R的左边缘
将R的y坐标[y1,y2]与区间树T进行比较.
如果找到重叠,则算法停止并报告OVERLAP.如果
在树T中没有重叠,那么区间[y1,y2]是
插入树中.
>如果当前的x坐标是矩形R的右边缘
从区间树T中删除其y-区间[y1,y2].
>如果已完全处理排序列表S,则没有重叠.该算法停止并报告NO-OVERLAP.

N个矩形的时间复杂度为O(N * log(N))
因为每个2 * N.
x坐标在区间树中搜索N个区间.
辅助数据结构S和T的空间复杂度为O(N).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐