Finding nearest point in an efficient way

后端 未结 6 1604
野性不改
野性不改 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:53

    If you are creating the set of N points then instead of just pushing them into a set, you can hash and map them based on their linear distance from the point under consideration . So points with same linear distance will be in the same bucket. Then fetching the point(s) based on distance will be a constant time operation.

提交回复
热议问题