Fast algorithm to find all points inside a rectangle

前端 未结 5 731
忘了有多久
忘了有多久 2021-02-14 17:04

Given a set of distinct points in 2D space, and a rectangle (coordinates of all four points, sides parallel with xy axis) how can I quickly find which points are inside the rect

5条回答
  •  梦毁少年i
    2021-02-14 17:54

    A classical answer is the kD-tree (2D-tree in this case).

    For a simple alternative, if your points are spread uniformly enough, you can try by gridding.

    Choose a cell size for a square grid (if the problem is anisotropic, use a rectangular grid). Assign every point to the grid cell that contains it, stored in a linked list. When you perform a query, find all cells that are overlapped by the rectangle and scan them to traverse their lists. For the partially covered cells, you will need to perform the point-in-rectangle test.

    The choice of the size is important: too large can result in too many points needing to be tested anyway; too small can result in too many empty cells.

提交回复
热议问题