今天圣诞节,我分别用R和Python绘制了圣诞树,祝大家圣诞节快乐。
Python篇
@方法一
n = 50
from turtle import *
speed("fastest")
left(90)
forward(3*n)
color("orange", "yellow")
begin_fill()
left(126)
for i in range(5):
forward(n/5)
right(144)
forward(n/5)
left(72)
end_fill()
right(126)
color("dark green")
backward(n*4.8)
def tree(d, s):
if d <= 0: return
forward(s)
tree(d-1, s*.8)
right(120)
tree(d-3, s*.5)
right(120)
tree(d-3, s*.5)
right(120)
backward(s)
tree(15, n)
backward(n/2)
![Clipboard Image.png](https://ask.hellobi.com/uploads/article/20161225/da780694ac5447144df9314906af1696.png)
@方法二
def paintleaves(m):
for i in range(m):
if(i == 10):
print( ' '*(m-i) + '*'*( 2*i + 1-len( 'happy Christmas')) + 'happy Christmas'+ ' '*(m-i))
continue
if(i == 20):
print( ' '*(m-i) + '*'*( 2*i + 1-len( 'happy Christmas')) +'happy Christmas'+ ' '*(m-i))
continue
if(i == m-1):
print( ' '*(m-i) + 'happy Christmas'+ '*'*( 2*i + 1-len( 'happy Christmas')) + ' '*(m-i))
continue
print(' '*(m-i) + '*'*(2*i + 1) + ' '*(m-i))
def paintTrunk(n):
for j in range (8 ):
print(' '*(n - 5) + '*'*10 + ' '*(n - 5))
paintleaves(30)
paintTrunk(30)
![Clipboard Image.png](https://ask.hellobi.com/uploads/article/20161225/a86d598ae7f3c9ce1ffa141a2d9f36c5.png)
@方法三
import turtle
screen = turtle.Screen()
screen.setup(800,600)
circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.speed('fastest')
circle.up()
square = turtle.Turtle()
square.shape('square')
square.color('green')
square.speed('fastest')
square.up()
circle.goto(0,280)
circle.stamp()
k = 0
for i in range(1, 17):
y = 30*i
for j in range(i-k):
x = 30*j
square.goto(x,-y+280)
square.stamp()
square.goto(-x,-y+280)
square.stamp()
if i % 4 == 0:
x = 30*(j+1)
circle.color('red')
circle.goto(-x,-y+280)
circle.stamp()
circle.goto(x,-y+280)
circle.stamp()
k += 2
if i % 4 == 3:
x = 30*(j+1)
circle.color('yellow')
circle.goto(-x,-y+280)
circle.stamp()
circle.goto(x,-y+280)
circle.stamp()
square.color('brown')
for i in range(17,20):
y = 30*i
for j in range(3):
x = 30*j
square.goto(x,-y+280)
square.stamp()
square.goto(-x,-y+280)
square.stamp()
turtle.exitonclick()
![Clipboard Image.png](https://ask.hellobi.com/uploads/article/20161225/f34babaf455442079904ff3516aa6cdb.png)
R语言篇
@方法一
L <- matrix(c(0.03,0,0,0.1,0.85,0.00,0.00,0.85,0.8,0.00,0.00,0.8,0.2,
-0.08,0.15, 0.22, -0.2,0.08,0.15, 0.22,0.25, -0.1,0.12, 0.25,-0.2,0.1,0.12,
0.2),nrow=4)
B <- matrix(c(0,0,0,1.5,0,1.5,0,0.85,0,0.85,0,0.3,0, 0.4),nrow=2)
prob = c(0.02, 0.6,.08, 0.07, 0.07, 0.07, 0.07)
N = 1e5
x = matrix(NA,nrow=2,ncol=N)
x[,1] = c(0,2)
k <- sample(1:7,N,prob,replace=TRUE)
for(i in 2:N)
{
x[,i] = crossprod(matrix(L[,k[i]],nrow=2),x[,i-1]) + B[,k[i]]
}
par(bg='black',mar=rep(0,4))
plot(x=x[1,],y=x[2,],col=grep('green',colors(),value=TRUE),axes=FALSE,
cex=.1, xlab='', ylab='',pch='.')
bals <- sample(N,20)
points(x=x[1,bals],y=x[2,bals]-.1,col=c('red','blue','yellow','orange'),
cex=1.5,pch=19)
text(x=-.7,y=8, labels='Merry', adj=c(.5,.5), srt=45,
vfont=c('script','plain'),cex=3,col='gold' )
text(x=0.7,y=8,labels='Christmas',adj=c(.5,.5),srt=-45,
vfont=c('script','plain'),cex=3, col='gold' )
text(x=-0.6,y=0,cex=0.8,labels="@ZSRNOG",
col="white)
![Clipboard Image.png](https://ask.hellobi.com/uploads/article/20161225/0d22618e49845ca356ab23cbee5235af.png)
@方法二
par(bg='black',mar=rep(0,4))
plot(1:10,1:10,xlim=c(-5,5),ylim=c(0,10),type="n",xlab="",ylab="",xaxt="n",yaxt="n")
rect(-1,0,1,2,col="tan3",border="tan4",lwd=3)
polygon(c(-5,0,5),c(2,4,2),col="palegreen3",border="palegreen4",lwd=3)
polygon(c(-4,0,4),c(3.5,5.5,3.5),col="palegreen4",border="palegreen3",lwd=3)
polygon(c(-3,0,3),c(5,6.5,5),col="palegreen3",border="palegreen4",lwd=3)
polygon(c(-2,0,2),c(6.25,7.5,6.25),col="palegreen4",border="palegreen3",lwd=3)
points(x=runif(4,-5,5),y=rep(2,4),col=sample(c("blue","red"),size=4,replace=T),cex=3,pch=19)
points(x=runif(4,-4,4),y=rep(3.5,4),col=sample(c("blue","red"),size=4,replace=T),cex=3,pch=19)
points(x=runif(4,-3,3),y=rep(5,4),col=sample(c("blue","red"),size=4,replace=T),cex=3,pch=19)
points(x=runif(4,-2,2),y=rep(6.25,4),col=sample(c("blue","red"),size=4,replace=T),cex=3,pch=19)
points(0,7.5,pch=8,cex=5,col="gold",lwd=3)
xPres = runif(10,-4.5,4.5)
xWidth = runif(10,0.1,0.5)
xHeight=runif(10,0,1)
for(i in 1:10){
rect(xPres[i]-xWidth[i],0,xPres[i]+xWidth[i],xHeight[i],col=sample(c("blue","red"),size=1))
rect(xPres[i]-0.2*xWidth[i],0,xPres[i]+0.2*xWidth[i],xHeight[i],col=sample(c("gold","grey87"),size=1))
}
![Clipboard Image.png](https://ask.hellobi.com/uploads/article/20161225/523d17e4e367f8f090af1554ba199fc9.png)
![Clipboard Image.png](https://ask.hellobi.com/uploads/article/20161221/6ae71d861e94a93b003a87cede094182.png)