Closest pair for any of a huge number of points

后端 未结 3 1690
天命终不由人
天命终不由人 2020-12-22 02:37

We are given a huge set of points in 2D plane. We need to find, for each point the closest point within the set. For instance suppose the initial set is as follows:

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-22 03:30

    The traditional approach is to preprocess the data and put it in a data structure, often a K-d tree, for which the "nearest point" query is very fast.

    There is an implementation in the nnclust package.

    library(nnclust)
    foo <- cbind(x=c(1,2,4,4,10),y=c(1,2,4,4,10))
    i <- nnfind(foo)$neighbour
    plot(foo)
    arrows( foo[,1], foo[,2], foo[i,1], foo[i,2] )
    

提交回复
热议问题