Finding nearest point in an efficient way

后端 未结 6 1599
野性不改
野性不改 2020-11-30 07:23

I\'ve got a point in 2d plane for example (x0,y0) and a set of n points (x1,y1)...(xn,yn) and I want to find nearest point to (x0,y0) in a way better than trying all points

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 07:42

    Voronoi diagram is designed specifically for finding nearest point very fast. Although it's quite a pain to implement, you might find some existing library/implementation.

    There's also an option to of repeatedly dividing plane in squares, thus building some kind of tree where each non-leaf node has 4 children (top-right square, bottom-right square, etc.). Then, of four squares you find the one your point is in and proceed with it recursively. Often this yields point close enough, so you may eliminate the need to check other squares.
    But it's easy to create a 'counter-example' for this strategy which will result in linear time.

    But there's not much you can do with your sorted array to speed up the process. You'll need a special data structure.

    edit
    Second structure is called Quadtree, thanks to VGE for providing the name.

提交回复
热议问题