首页 > 解决方案 > 如何在单位正方形上多次进行 100 个点的 Voronoi 镶嵌

问题描述

简而言之,我将进行 100 个点的 Voronoi 细分,并创建 1000 次不同的 100 个点集,并对每组点进行细分。

我创建了点,

x=matrix(runif(100*1000),nrow=100,ncol=1000)
y=matrix(runif(100*1000),nrow=100,ncol=1000)

spatstat使用包执行 Voronoi 细分的基本代码是

dir = ppp(x=x, y=y, window = square(c(0,1)))
tess = dirichlet(dir)
plot(tess, main = "")
points(dir, pch=19, cex = 0.5)

但是,我需要对 1000 个样本进行 Voronoi 细分并尝试创建一个循环。我想选择 x 和 y 的每一列,最终得到 1000 个“目录”。然后为 1000 'dir' 进行镶嵌 'tess'。我还需要使用函数计算 voronoi 细胞的面积area=tile.areas(tess)

我创建的循环是

for (i in 1000) {
  dir[i] = ppp(x=x[,i], y=y[,i], window = square(c(0,1)))
}

但我得到的只是错误和警告。你知道怎么做吗?

标签: rvoronoitessellation

解决方案


您需要将输出存储在一个对象中,在这种情况下,让我们将它放在一个名为dirList. 此外,您必须指定要迭代的序列。for (i in 100)什么都不做,它必须是for (i in 1:100)

library(deldir)
library(spatstat)

x <- matrix(runif(100 * 1000), nrow = 100, ncol = 1000)
y <- matrix(runif(100 * 1000), nrow = 100, ncol = 1000)

dirList <- list()

for (i in 1:1000){
  dirList[[i]] <- ppp(x = x[ , i], y = y[ , i], window = square(c(0, 1)))
}

然后进行绘图,您可以使用[[]]

tess = dirichlet(dirList[[1]])
plot(tess, main = "")

对于问题的第二部分,将您的对象tess用于一组点:

归功于@D杰克的一般过程

library(deldir)
library(spatstat)
library(maptools)
library(rgeos)

x <- matrix(runif(100 * 1000), nrow = 100, ncol = 1000)
y <- matrix(runif(100 * 1000), nrow = 100, ncol = 1000)

x <- x[ , 1]
y <- y[ , 1]

dir = ppp(x=x, y=y, window = square(c(0,1)))
tess = dirichlet(dir)

t1 <- as(tess, "SpatialPolygons")
t2 <- unionSpatialPolygons(t1, ID=rep(1,length(t1)))
vor_border <- which(gRelate(t1,t2, byid=TRUE) %in% c("2FF11F212"))

par(mfcol=c(1,3))
plot(t1)
plot(t1[vor_border,], col="red")
# you could also invert the mask to get just the inner polygons
plot(t1[-vor_border,], col="lightblue")

在此处输入图像描述


推荐阅读