Test if point is in some rectangle

后端 未结 5 835
我寻月下人不归
我寻月下人不归 2020-12-30 08:06

I have a large collection of rectangles, all of the same size. I am generating random points that should not fall in these rectangles, so what I wish to do is test if the ge

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 08:19

    I suggest you take a look at BSP trees (and possible quadtrees or octrees, links available on that page as well). They are used to partition the whole space recursively and allow you to quickly check for a point which rectangles you need to check at all.

    At minimum you just have one huge partition and need to check all rectangles, at maximum your partitions get so small, that they get down to the size of single rectangles. Of course the more fine-grained the partition, the longer you need to walk down the tree in order to find the rectangles you want to check.

    However, you can freely decide how many rectangles are suitable to be checked for a point and then create the corresponding structure.

    Pay attention to overlapping rectangles though. As the BSP tree needs to be precomputed anyways, you may as well remove overlaps during that time, so you can get clear partitions.

提交回复
热议问题