我正在同一圖上繪製幾個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 2014-09-19 10:00:25
嗨@digEmAll,我已經嘗試過這個解決方案,你可以看到我在我的問題中提到過它。然而,我想從最小的多邊形到最大的多邊形,這樣我可以看到當我的情況下多邊形的邊緣數量增加時,繪圖區域的增加情況。 – 2014-09-19 10:07:09
Sossy,我沒有注意到(我承認我讀得很急)...... – digEmAll 2014-09-19 12:15:44