Algorithm for hit test in non-overlapping rectangles

前端 未结 4 2333
鱼传尺愫
鱼传尺愫 2020-12-19 13:26

I have a collection of non-overlapping rectangles that cover an enclosing rectangle. What is the best way to find the containing rectangle for a mouse click?

The obv

4条回答
  •  春和景丽
    2020-12-19 13:54

    Create an Interval Tree. Query the Interval Tree. Consult 'Algorithms' from MIT press.

    An Interval Tree is best implemented as a Red-Black Tree.

    Keep in mind that it is only advisable to sort your rectangles if you are going to be clicking at them more then you are changing their positions, usually.

    You'll have to keep in mind, that you have build build your indices for different axis separately. E.g., you have to see if you overlap an interval on X and on Y. One obvious optimization is to only check for overlap on either X interval, then immediately check for overlap on Y.

    Also, most stock or 'classbook' Interval Trees only check for a single interval, and only return a single Interval (but you said "non-overlapping" didn't you?)

提交回复
热议问题