Efficient point inside rectangle boundaries search

I'm working on a vector map editor and I have a set of elements, each of which specifies its bounding box within the view. As the mouse moves I want to highlight the first element whose bounding box contains the current mouse location. Right now I use a simple list and go through it, but as the number of elements is likely to increase, the O(n) complexity of the current search algorithm will be problematic for an interactive application.

What would be a better algorithm/data structure for this?

Some additional constraints/requirements:

  • The filling of the bounding boxes data structure would have to be relatively quick (since I need to do that each time the map moves or the zoom or projection changes).
  • The algorithm would have to be able to find all of the matching elements (not just the first one). The reason is that some map elements can have irregular shape, so the simple bounding box match is not strict enough. I would then go through the list of matches and do some precise matching.
  • The order in which the boxes were added to the set needs to be maintained somehow - a map element which is drawn above another element should have a priority when matching their bounding boxes.
6
задан Igor Brejc 15 May 2011 в 08:40
поделиться