Voronoi图查询

时间:2014-08-16 06:44:03

标签: r voronoi

我在R中创建了一个voronoi图 给出了点标签

使用代码:

install.packages("deldir") 
library(deldir)  
x <- runif(20); y <- runif(20); window <- c(0,1,0,1) 
tess <- deldir(x, y, rw = window) 
plot.deldir(tess, wpoints="real", wlines="tess") 
label=c('a','b','c'.....'t')
library(plotrix)
thigmophobe.labels(x, y, labels=label)

有人可以提出查询,以便 如果我给出一个新点的位置,该点位于包含点&#39; a&#39; 输出应该是&#39; a&#39;。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

由于voronoi tesselation是最近点之间的边界线,因此您的问题只会减少到找到最近的邻居。

您可以通过使用dist()计算距离矩阵,然后以最小距离提取点来完成此操作。

试试这个:

library(deldir)  

n <- 20
dat <- data.frame(
  x = runif(n),
  y =runif(n)
)

tess <- deldir(dat, rw = c(0,1,0,1)) 
plot.deldir(tess, wpoints="none", wlines="tess", col="blue") 
label=letters[1:n]
text(dat, labels=label)


for (i in 1:500){
  newdat <- data.frame(x=runif(1), y=runif(1))
  np <- which.min(unname(as.matrix(dist(rbind(newdat, dat), diag=FALSE))[-1, 1]))

  text(newdat, col="red", labels=label[np], cex=0.5)
}

enter image description here

答案 1 :(得分:0)

您可以使用点位置测试:http://en.wikipedia.org/wiki/Point_location

相关问题