Drawing equidistant points from the sides of a polygon

时光总嘲笑我的痴心妄想 提交于 2019-12-05 21:02:37

You can do this with spsample from the sp package.

First we'll load the library and read in your vertices.

library(sp)

xy <- read.table(text='x            y
-0.02208709 -0.039161304
0.01184081 -0.020268029
0.04578401 -0.001351904
0.02210236  0.039176396
-0.01185226  0.020252146
-0.04578784  0.001352696', header=TRUE)

Now create a SpatialLines object from the vertices. This is a bit messy - see ?SpatialLines and ?`SpatialLines-Class` if you get stuck.

l <- SpatialLines(list(Lines(Line(rbind(xy, xy[1, ])), ID=1)))

Then sample the points and coerce to a data.frame with as.data.frame(pts) or coordinates(pts).

pts <- spsample(l, 50, type="regular")
coordinates(pts) # only the head shown here
##                 x           y
## [1,] -0.019343310 -0.03763339
## [2,] -0.014987452 -0.03520776
## [3,] -0.010631594 -0.03278213
## [4,] -0.006275735 -0.03035651
## [5,] -0.001919877 -0.02793088
## [6,]  0.002435981 -0.02550525

plot(l)
points(pts, pch=20)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!