Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. library(sp); library(rgeos); library(deldir)
  2.  
  3. 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)
  4. 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)
  5. crds<-cbind(x,y)
  6.  
  7. x.p<-c(0.1,0.1,3.5,3,5,1,6,6,0.1)
  8. y.p<-c(0.1,5,4.8,1,5,5.5,6.5,1,0.1)
  9. poly<-cbind(x.p,y.p)
  10.  
  11. x.h<-c(1,1.1,1.5,2.1,1.9,2.3,3,1)
  12. y.h<-c(1,2.9,3.1,3,2.8,2.2,1.5,1)
  13. hole<-cbind(x.h,y.h)
  14.  
  15. deldir(crds[,1],crds[,2])
  16.  
  17. voronoipolygons <- function(crds) {
  18. z <- deldir(crds[,1], crds[,2],rw=c(0,7,0,7))
  19. w <- tile.list(z)
  20. polys <- vector(mode='list', length=length(w))
  21. for (i in seq(along=polys)) {
  22. pcrds <- cbind(w[[i]]$x, w[[i]]$y)
  23. pcrds <- rbind(pcrds, pcrds[1,])
  24. polys[[i]] <- Polygons(list(Polygon(pcrds)), ID=(1:nrow(crds))[i])
  25. }
  26. SP <- SpatialPolygons(polys)
  27. voronoi <- SpatialPolygonsDataFrame(SP, data=data.frame(x=crds[,1],y=crds[,2], row.names=sapply(slot(SP, 'polygons'),function(x) slot(x, 'ID'))))
  28. return(voronoi)
  29. }
  30. SP<-voronoipolygons(crds[,1:2])
  31. plot(SP)
  32.  
  33. ybox<-xbox<-c(0,7)
  34.  
  35. polypath(c(poly[,1], NA, c(xbox, rev(xbox))), c(poly[,2], NA, rep(ybox, each=2)), col="light blue", rule="evenodd")
  36. polygon(hole[,1],hole[,2],col="light blue")
  37. text(crds[,1],crds[,2],1:nrow(crds))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement