如何从delaunay三角剖分中提取距离以列出R中的对象

如何从delaunay三角剖分中提取距离以列出R中的对象,r,delaunay,R,Delaunay,假设我有点的坐标,每个点都有ID。我应该如何从delaunay三角剖分中提取距离来列出R中的对象 # My data are similar to this structure id <- c("A","B","C","D","E","F","G","H","I","J","K","L","M","N") x_coor <- c(0.5,1,1,1.5,2,3,3,3.5,4,4.5,5,5,6,7) y_coor <- c(5.5,3,7,6.5,5,3.5,3,1.5,1,

假设我有点的坐标,每个点都有ID。我应该如何从delaunay三角剖分中提取距离来列出R中的对象

# My data are similar to this structure
id <- c("A","B","C","D","E","F","G","H","I","J","K","L","M","N")
x_coor <- c(0.5,1,1,1.5,2,3,3,3.5,4,4.5,5,5,6,7)
y_coor <- c(5.5,3,7,6.5,5,3.5,3,1.5,1,2.5,4,5,3.5,5.5)
my.data <- data.frame(id = id, x_coor = x_coor, y_coor = y_coor)

# When I perform Delaunay triangulation, I can see the distances....
library(tripack)
my.triangles<-tri.mesh(my.data$x_coor, my.data$y_coor)
plot(my.triangles, do.points=FALSE, lwd=0.2)
points(my.data$x, my.data$y, col = "black", pch=20, cex = 1.5)
text(my.data$x, my.data$y, labels = my.data$id)

连接到“A”的所有线段的一个端点等于“A”的坐标。找到这些坐标:

xy<- c(x-coor[id=='A'],y_coor[id=='A']) 
通过观察xy值与此打印输出中的第一个坐标匹配,可以获取相邻顶点并查找其坐标。这可能更容易执行

my_neighbor<-neighbours(my.triangles)
# partial result:
[[1]]
[1] 2 3 4 5

[[2]]
[1] 1 5 6 7 8 9

[[3]]
[1]  1  4 14

[[4]]
[1]  1  3  5 12 14
第一列的结果是您想要的my.list$A的距离。从tri.mesh开始,我们有:

然后从原始数据帧中附加点ID,这样您几乎可以得到所需的列表,除了它包含相邻ID而不是距离:

dist(cbind(xtmp,ytmp))
names(neiblist) <- my.data$id         #append names for later reference
然后,出于矩阵完整性的原因,您可以通过以下方式消除先前生成的NA:

#Function to remove NA's

fun_NA <- function(x){x=x[!is.na(x)]
return(x)}
从结果中删除NA

results_list <- lapply(results_list, FUN=fun_NA)  
我的想法是,这将是非常非常迅速的,即使有很多很多点…但有人可能知道不同的:-


干杯。

嗨@CarlWitthotf。谢谢你的回答。但上面的数据只是我整个数据集中的一个片段。我有1000多个点,所以分别提取每个点的距离需要很长时间。有没有可能修改您的答案以更好地适应这种情况?@LadislavNado如果我有时间,我会这样做,但您应该能够接受我介绍的离散操作,并围绕它们编写一些for循环,以使整个过程自动化。干得好@Shekeine!非常感谢您的计算效率和速度。当然,这不是问题:-
neiblist <- neighbours(my_triangles)
names(neiblist) <- my.data$id         #append names for later reference
euc_dist <- as.matrix(dist(cbind(x=my_triangles$x, y=my_triangles$y)))

#Append dimnames for reference

colnames(euc_dist) <- my.data$id
rownames(euc_dist) <- my.data$id
max_n <- max(unlist(lapply(neiblist, length)))
npoints <- length(my.data$id)                 # This is just the total number of points
dist_2neigh_mat <- matrix(nrow=npoints, ncol=max_n)    #Create results matrix

rownames(dist_2neigh_mat) <- my.data$id
colnames(dist_2neigh_mat) <- colnames(data.frame(dist=matrix(data=1:6, nrow=1)))
for (i in my.data$id){
neighbors_i <- neiblist[[i]]
dist2neighbours_i <- euc_dist[,i][neighbors_i]

#Append vector with NAs to match ncol of results matrix

dist2neighbours_i <- c(dist2neighbours_i, rep(NA, 
times=(max_n-length(dist2neighbours_i))))

dist_2neigh_mat[i,] <- dist2neighbours_i   #Update results matrix with i'th distances
}
results_list <- as.list(data.frame(t(dist_2neigh_mat)))
#Function to remove NA's

fun_NA <- function(x){x=x[!is.na(x)]
return(x)}
results_list <- lapply(results_list, FUN=fun_NA)