从多边形的边绘制等距点
Drawing equidistant points from the sides of a polygon
我正在绘制具有以下顶点的多边形
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
使用以下代码
plot(x,y)
polygon(x,y)
points(mean(x),mean(y),col="red")
现在我想沿着多边形的边绘制 50 个等距点。有什么建议吗?
您可以使用 sp
包中的 spsample
执行此操作。
首先我们将加载库并读入您的顶点。
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)
现在从顶点创建一个 SpatialLines
对象。这有点混乱 - 如果您遇到困难,请参阅 ?SpatialLines
和 ?`SpatialLines-Class`
。
l <- SpatialLines(list(Lines(Line(rbind(xy, xy[1, ])), ID=1)))
然后对点进行采样并用 as.data.frame(pts)
或 coordinates(pts)
强制转换为 data.frame
。
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)
我正在绘制具有以下顶点的多边形
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
使用以下代码
plot(x,y)
polygon(x,y)
points(mean(x),mean(y),col="red")
现在我想沿着多边形的边绘制 50 个等距点。有什么建议吗?
您可以使用 sp
包中的 spsample
执行此操作。
首先我们将加载库并读入您的顶点。
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)
现在从顶点创建一个 SpatialLines
对象。这有点混乱 - 如果您遇到困难,请参阅 ?SpatialLines
和 ?`SpatialLines-Class`
。
l <- SpatialLines(list(Lines(Line(rbind(xy, xy[1, ])), ID=1)))
然后对点进行采样并用 as.data.frame(pts)
或 coordinates(pts)
强制转换为 data.frame
。
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)