Find rectangles that contain point – Efficient Algorithm

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

Good afternoon.

My situation:

  • In two-dimensional space.
  • Input: a set of rectangles (ov
5条回答
  •  长情又很酷
    2020-12-31 09:37

    In java you can use shape.contains

    But in general, assuming a rectangle is defined by (x,y,width,height) you do

    if (pt.x >= x && pt.x <= x + width && pt.y >= y && pt.y <= y + height) ...

    If you have all your rectangles in a collection you can iterate over the collection and find the ones that contain the point

提交回复
热议问题