有效地集团重叠的矩形 [英] Efficiently Group Overlapping Rectangles

查看:125
本文介绍了有效地集团重叠的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好的方法组重叠的矩形?我已经使用OpenCV的,但按照预期的 GROU prectangles 方法是行不通的尝试。

Best way to group overlapping rectangles? I've tried using OpenCV but the grouprectangles method doesn't work as intended.

我也想过做这样的事情:

I've thought about doing something like this:

L = [every rectangle]
L_next = []
while not L.empty():
    for rectangle in L:
        L.remove(rectangle)
        for other_rectangle in L:
            if rectangle overlaps with other_rectangle:
                L_next += rectangle + other_rectangle
    L = L_next
    L_next = []

由于未合并会从下一个列表中删除,在最坏的情况下每一个矩形,我有外循环的N / 2 迭代。两个内环分别应执行 N N - 1 倍,这样的算法应该是大致为O(n ^ 3)在最坏的情况下,假设我没有错过任何东西,每一步只需 O(1)

Since every rectangle that isn't merged would be dropped from the next list, in the worst case scenario, I'd have n/2 iterations of the outer loop. The two inner loops should execute n and n - 1 times, so that algorithm should be roughly O(n^3) in a worst case scenario, assuming I didn't miss anything and that each step only takes O(1).

问题:

1)需要用等价类或东西的程度,正确合并矩形的群体。升压是否有这样的事情?

1) Need to use Equivalence Classes or something to that extent to merge the groups of rectangles correctly. Does Boost have anything like that?

2)这似乎是那种操作,将需要经常做的,所以我很惊讶没有找到更多的材料就可以了。怎么了呢?

2) This seems like the kind of operation that would have to be done frequently, so I'm surprised about not finding more material on it. What's up with that?

3)假设真的是不是已经实施,以做到这一点的东西,没有任何人有一些建议,以提高我的方法?

3) Assuming there really is not something already implemented to do this, does anyone have some advice to improve my method?

4)什么是最好的方法,如果两个矩形重叠?

4) What is the best way to see if two rectangles overlap?

推荐答案

我想看看 pygame的.Rect.collide 的方法,你的问题的回应。由于矩形重叠检测是在游戏中如此普遍,我想他们实现在计算复杂性方面相当不错。

I'd look at pygame.Rect.collide methods for a response to your problem. Since rectangle overlapping detection is so common in games, I guess their implementation is quite good in terms of computational complexity.

这篇关于有效地集团重叠的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆