4

I have set of rectangles of various sizes in 2D space. Number of rectangles may be changed dynamically from 10 to 100 000, their position, as well as their sizes are often updated.

Which spatial structure would you recommend to find rectangle at given point (x,y)? Assuming that search operation also performed very often (on mouse move for example). If you could give a reference to various spatial indexing algorithms comparison or compare their search/build/update performance here - that would be lovely.

CuriousG
  • 327
  • 1
  • 3
  • 7
  • 1
    do you want something like finding all rectangles that include the given point? are the rectangles rotated by arbitrary amounts, or are their axes parallel to x and y? – andrew cooke May 21 '12 at 02:25
  • their axes are parellel to Ox, Oy, sorry for missing this. Finding all rectangles would be nice, but even finding one that include a point is good enough. I'm aware about various [indexes](http://en.wikipedia.org/wiki/Spatial_index#Spatial_index), but I never worked with them, so was more curious in expert opinion which one should I pick based on given situation... – CuriousG May 21 '12 at 03:13

2 Answers2

2

I would suggest R-Tree. It is primarily designed for rectangles (or N-dimensional axis aligned cubes).

Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
0

Use a quadtree (http://en.wikipedia.org/wiki/Quadtree).

Determine all possible X and Y values at which rectangles start and end. Then build a quadtree upon these values. In each leaf of the quadtree, store which rectangles overlap with the coordinate-ranges of the leaf. Finding which rectangles overlap is then just a matter of finding the leaf containing the coordinate.

Patrick
  • 23,217
  • 12
  • 67
  • 130