Welcome to Code Sharer, a code gallery for creating, saving and sharing all kinds of code snippets.

R — Christmas Tree

Posted On: Tuesday, December 2, 2014 at 11:54 AM Last updated: Friday, December 5, 2014 at 4:54 AM

Plots a Christmas tree with decorations in random places along the branches. try running the same code a few times to see for your self!


xmas tree





#The canvas
plot(1:10,1:10,xlim=c(-5,5),ylim=c(0,10),type="n",xlab="",ylab="",xaxt="n",yaxt="n")
#The branches
rect(-1,0,1,2,col="tan3",border="tan4",lwd=3)
polygon(c(-5,0,5),c(2,4,2),col="palegreen2",border="palegreen2",lwd=3)
polygon(c(-4,0,4),c(3.5,5.5,3.5),col="palegreen4",border="palegreen4",lwd=3)
polygon(c(-3,0,3),c(5,6.5,5),col="palegreen3",border="palegreen3",lwd=3)
polygon(c(-2,0,2),c(6.25,7.5,6.25),col="palegreen4",border="palegreen4",lwd=3)

#The ornaments
points(x=runif(4,-5,5),y=rep(2,4),col=sample(c("Yellow","red"),size=4,replace=T),cex=3,pch=19)
points(x=runif(4,-4,4),y=rep(3.5,4),col=sample(c("Yellow","red"),size=4,replace=T),cex=3,pch=19)
points(x=runif(4,-3,3),y=rep(5,4),col=sample(c("Yellow","red"),size=4,replace=T),cex=3,pch=19)
points(x=runif(4,-2,2),y=rep(6.25,4),col=sample(c("Yellow","red"),size=4,replace=T),cex=3,pch=19)
points(0,7.5,pch=8,cex=5,col="gold",lwd=3)