简体   繁体   English

如何在给定的矩形列表中找到与特定矩形相邻的所有矩形?

[英]How can I find all rectangles adjacent to particular rectangle on a given list of rectangles?

Given a rectangle R1 and a list of of rectangles R2,R3,.... How can I find all rectangles that are connected with the main rectangle R1.给定一个矩形 R1 和一个矩形列表 R2,R3,.... 我怎样才能找到与主矩形 R1 相连的所有矩形。

I don't just need the rectangle that are directly connected to R1 but also all that are indirectly connected to R1.我不仅需要直接连接到 R1 的矩形,还需要所有间接连接到 R1 的矩形。 For Example if R2 is connected to R1 and R3 is connected to R2.例如,如果 R2 连接到 R1,R3 连接到 R2。 R3 is considered connected to R1. R3 被视为连接到 R1。

Rectangles are given in the form (xmin, ymin, xmax, ymax).矩形以 (xmin, ymin, xmax, ymax) 的形式给出。 All rectangles are parallel to the axis.所有矩形都平行于轴。 Rectangles are considered connected when they are either overlapping or touching.当矩形重叠或接触时,它们被认为是连接的。 When they just touch in the corner they are not considered connected.当他们只是在角落里接触时,他们不被认为是连接的。

Example:例子:

____________
_111________
_11122______
____22______
____22______
____333333__ 
____22______
__55___4444_
__55___4444_ 

In this example R1,R2,R3 are connected with each other.在本例中,R1、R2、R3 相互连接。 So I need to return R1,R2,R3.所以我需要返回 R1、R2、R3。

R4, and R5 are not connected. R4 和 R5 未连接。

An obvious solution would be to compare each each rectangle with eachother O(n^2).一个明显的解决方案是将每个矩形与彼此 O(n^2) 进行比较。 But I think there should be faster solutions.但我认为应该有更快的解决方案。 I have tried to use Implement a sweep line algorithm with an Interval Tree.我曾尝试使用用区间树实现扫描线算法。 But it is to slow.但它是缓慢的。 I need a solution in O(n log n)我需要一个 O(n log n) 的解决方案

Make 2 lists, one containing X-coordinate objects and one containing Y-containing objects.制作 2 个列表,一个包含 X 坐标对象,一个包含包含 Y 坐标的对象。

The object will consist of (the coordinate, the rectangle number and whether the coordinate corresponds to min or max).对象将由(坐标、矩形编号以及坐标对应于最小值还是最大值)组成。

Sort both lists O(nlogn) and traverse any one.对两个列表进行排序 O(nlogn) 并遍历任何一个。 On encountering a min-coordinate add the rectangle in a stack, on encountering a max-coordinate remove the rectangle.在遇到最小坐标时将矩形添加到堆栈中,在遇到最大坐标时删除矩形。 If multiple rectangles are added together in a stack, add them in a group, while removing a rectangle the group will correspond to all the overlapping rectangles.如果多个矩形堆叠在一起,则将它们添加到一个组中,而删除一个矩形时,该组将对应于所有重叠的矩形。 Store the information of such groups.存储此类组的信息。 Do the same procedure using the other list.使用另一个列表执行相同的过程。 The common rectangles in groups obtained from both X and Y lists will be the overlapping rectangles.从 X 和 Y 列表中获得的组中的公共矩形将是重叠矩形。

The solution is O(nlogn).解决方案是 O(nlogn)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM