3

I can't figure out how to implement this in a performing way, so I decided to ask you guys.

I have a list of rectangles - actually atm only squares, but I might have to migrate to rectangles later, so let's stick to them and keep it a bit more general - in a 2 dimensional space. Each rectangle is specified by two points, rectangles can overlap and I don't care all too much about setup time, because the rectangles are basicly static and there's some room for precalculate any setup stuff (like building trees, sorting, precalculating additional vectors, whatever etc). Oh I am developing in JavaScript if this is of any concern.

To my actual question: given a point, how do I get a set of all rectangles that include that point?

Linear approaches do not perform well enough. So I look for something that performs better than O(n). I read some stuff, like on Bounding Volume Hierarchies and similar things, but whatever I tried the fact that rectangles can overlap (and I actually want to get all of them, if the point lies within multiple rectangles) seems to always get into my way.

Are there any suggestions? Have I missed something obvious? Are BVH even applicable to possibly overlapping bounds? If so, how do I build such a possibly overlapping tree? If not, what else could I use? It is of no concern to me if borders are inside, outside or not determined.

If someone could come up with anything helpfull like a link or a rant on how stupid I am to use BVH and not Some_Super_Cool_Structure_Perfectly_Suited_For_My_Problem I'd really appreciate it!

Edit: Ok, I played around a bit with R-Trees and this is exactly what I was looking for. Infact I am currently using the RTree implementation http://stackulator.com/rtree/ as suggested by endy_c. It performs really well and fullfills my requirements entirely. Thanks alot for your support guys!

4

2 回答 2

7

You could look at R-Trees

Java code

there's also a wiki, but can only post one link ;-)

于 2010-10-31T15:51:22.890 回答
2

您可以将空间划分为网格,并且每个网格单元都有一个矩形列表(或矩形标识符),这些矩形(或矩形标识符)至少部分存在于该网格中。仅在相应网格的单元格中搜索矩形。复杂度应该是 O(sqrt(n))。

另一种方法是维护 x1,y1,x2,y2 值的四个排序数组,并在这 4 个数组中二进制搜索您的点。每次搜索的结果是一组矩形候选,最终结果是这 4 组的交集。根据集合交集的实现方式,这应该比 O(n) 有效。

于 2010-10-31T15:29:44.933 回答