【发布时间】:2014-09-19 09:48:57
【问题描述】:
我在同一个图上绘制几个 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)
【问题讨论】:
-
你不能简单地以相反的顺序添加多边形(即先灰色然后粉色)吗?
-
嗨@digEmAll,我已经尝试过这个解决方案,你可以看到我在我的问题中提到了它。但是,我想从最小的多边形到最大的多边形,这样我就可以看到当多边形的边缘数量增加时,绘图区域是如何增加的。
-
Sossy,我没注意到(我承认我读的很匆忙)......
标签: r