Find rectangles that contain point – Efficient Algorithm

前端 未结 5 1099
自闭症患者
自闭症患者 2020-12-31 08:50

Good afternoon.

My situation:

  • In two-dimensional space.
  • Input: a set of rectangles (ov
5条回答
  •  自闭症患者
    2020-12-31 09:47

    A rectangle (left, top, right, bottom) contains a point (x, y) if left < x < right and top < y < bottom (assuming coordinates increase going downwards, which is the case with most hardware i've seen; if your coordinates increase going upwards, the more traditionally mathematical case, swap top and bottom). You're not going to get much more efficient than that for the test.

    If you consider a rectangle as "containing" a point if it's on the border as well, then replace all the <s with <=.

    As for what to do with the collection of rectangles...i dunno. I'd think a list sorted by the coordinates of the corners would do something, but i'm not really seeing much benefit to it...at most, you'd be cutting your list of stuff to check by half, on average (with the worst case still requiring checking everything). Half of a whole freaking lot can still be a whole lot. :)

提交回复
热议问题