我在同一个图上绘制了几个n边多边形。让我们说:
1 / n = 3:绘制带有3条边的多边形,用"粉红色"
着色2 / n = 6:绘制带有6条边的多边形,将用"灰色"进行着色。此时,我看到步骤1中的第一个多边形与此重叠。在这种情况下,我只想保持"粉红色"第一个多边形的颜色和其余的颜色"不重叠"第二个多边形的区域为"灰色"颜色。
我尝试了一些代码如下,但它始终显示"灰色"多边形,而不是"粉红色"和"灰"区域。
顺便说一句,我也绕过这个问题"首先绘制6边多边形(n = 6),然后绘制3边多边形(n = 3)"。通过将绘制顺序从最大多边形更改为最小多边形,我可以保留最大和最小多边形的颜色。但是,我想按照我在本问题开头提到的步骤进行操作,这样当n(边数)不断增加时,我可以看到绘图区域正在增加。
如果您有任何建议,请告诉我。非常感谢你!
cat("\014")
rm(list=ls())
#############################
# first polygon
#n=3
xx3=c(0,-3,3);xx3
yy3=c(1,1,-2);yy3
#plot each intersection /vertex of polygon n=3
plot (xx3, yy3,type = "p", xlim=c(-8,8), ylim=c(-8,8),col="blue",xlab = "x", ylab = "y")
# display value of each point above
text(xx3, yy3, paste("(",round(xx3, 2),"," ,round(yy3, 2), ")"),
cex=0.8,family="mono", font=1, adj=1.5, pos=3)
#fill the shade area
polygon(xx3, yy3, col = "pink", border = "pink")
title("Plot n-edge polygon")
#############################
# RUN untill this point and stop.
#And then run following part, you will see the 1st polygon is overlapping region
#and is fully overwrited by the second polygon.
#############################
# Second polygon
#n=6
par(new=TRUE)
xx=c(0,-15/11,-15/4,-45/11,-3, 3);xx
yy=c(1,20/11,5/2,20/11,1,-2);yy
#plot each intersection /vertex of polygon n=6
points(xx, yy,type = "p", col="blue",xlab = "x", ylab = "y")
# display value of each point above
text(xx, yy, paste("(",round(xx, 2),"," ,round(yy, 2), ")"),
cex=0.8,family="mono", font=1, adj=1.5, pos=3)
#fill the shade area
polygon(xx, yy, col = "grey", border = "grey")
#draw x=0,y=0
abline(a=0,b=0,v=0)
答案 0 :(得分:2)
一种可能性是计算当前多边形(较大)和前一个(较小)之间的差异。除了使用sp
(空间对象)和rgeos
之外,我不知道是否有一些简单的计算几何的方法。
这里有一些使用包sp
和rgeos
包的代码。该方法包括通过空间对象计算多边形差异并绘制它。这可能不是最优雅的方式,但至少它会做你想要的。
require(sp)
require(rgeos)
#test data
xx3=c(0,-3,3);xx3
yy3=c(1,1,-2);yy3
xx=c(-5,-5,5,5);xx
yy=c(-5,5,5,-5);yy
#create a SpatialPolygons object for n = 3
sp3 <- as.data.frame(cbind(xx3,yy3))
sp3 <- rbind(sp3, sp3[1,])
coordinates(sp3) <- c("xx3","yy3")
p3 <- Polygon(sp3)
ps3 <- Polygons(list(p3),1)
sps3 <- SpatialPolygons(list(ps3))
#create a SpatialPolygons object for n = 6
sp <- as.data.frame(cbind(xx,yy))
sp <- rbind(sp, sp[1,])
coordinates(sp) <- c("xx","yy")
p <- Polygon(sp)
ps <- Polygons(list(p),1)
sps <- SpatialPolygons(list(ps))
#compute the difference (with rgeos)
#between the current polygon (bigger) and the previous one (smaller)
spsdiff <- gDifference(sps, sps3)
为了绘制差异,有两种方式:
#Plotting 1: based on sp-plot
#===========
plot(sps, border="transparent") #to set some bigger extent
plot(sps3, add=T, col = "pink")
plot(spsdiff, add=T, col = "grey")
#Plotting 2: use polygon and polypath base functions
#===========
#preparing data for using polypath (polygons with hole)
polys <- spsdiff@polygons[[1]]@Polygons
coords <- do.call("rbind", lapply(polys, function(x){ if(x@hole) x@coords }))
holes <- do.call("rbind", lapply(polys,function(x){ if(!x@hole) rbind(rep(NA,2),x@coords) }))
poly.coords <- rbind(coords,holes)
#plot it
plot(xx, yy, col = "transparent")
polygon(xx3, yy3, col = "pink")
polypath(poly.coords[,1],poly.coords[,2],col="grey", rule="evenodd")
如果必须重复此操作,可以在循环中重复使用此代码,以迭代绘制多边形差异。
注意:rgeos
要求您在计算机上安装GEOS库