Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(sp); library(rgeos); library(deldir)
- x<-c(0.9,1.7,2.4,2.9,4.83, 0.73, 2.31, 3.69, 4.23, 2.86, 1.91, 4.32, 4.60, 1.82)
- y<-c(1.9,0.9,2.8,1.9,1.81, 1.66, 4.54, 5.66, 1.99, 4.03, 4.32, 5.98, 5.56, 3.41)
- crds<-cbind(x,y)
- x.p<-c(0.1,0.1,3.5,3,5,1,6,6,0.1)
- y.p<-c(0.1,5,4.8,1,5,5.5,6.5,1,0.1)
- poly<-cbind(x.p,y.p)
- x.h<-c(1,1.1,1.5,2.1,1.9,2.3,3,1)
- y.h<-c(1,2.9,3.1,3,2.8,2.2,1.5,1)
- hole<-cbind(x.h,y.h)
- deldir(crds[,1],crds[,2])
- voronoipolygons <- function(crds) {
- z <- deldir(crds[,1], crds[,2],rw=c(0,7,0,7))
- w <- tile.list(z)
- polys <- vector(mode='list', length=length(w))
- for (i in seq(along=polys)) {
- pcrds <- cbind(w[[i]]$x, w[[i]]$y)
- pcrds <- rbind(pcrds, pcrds[1,])
- polys[[i]] <- Polygons(list(Polygon(pcrds)), ID=(1:nrow(crds))[i])
- }
- SP <- SpatialPolygons(polys)
- voronoi <- SpatialPolygonsDataFrame(SP, data=data.frame(x=crds[,1],y=crds[,2], row.names=sapply(slot(SP, 'polygons'),function(x) slot(x, 'ID'))))
- return(voronoi)
- }
- SP<-voronoipolygons(crds[,1:2])
- plot(SP)
- ybox<-xbox<-c(0,7)
- polypath(c(poly[,1], NA, c(xbox, rev(xbox))), c(poly[,2], NA, rep(ybox, each=2)), col="light blue", rule="evenodd")
- polygon(hole[,1],hole[,2],col="light blue")
- text(crds[,1],crds[,2],1:nrow(crds))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement