R语言和Python绘制圣诞树

  • A+
所属分类:Python R语言

今天圣诞节,我分别用R语言和Python绘制了圣诞树,祝大家圣诞节快乐。

Python篇

@方法一

  1. n = 50
  2. from turtle import *
  3. speed("fastest")
  4. left(90)
  5. forward(3*n)
  6. color("orange""yellow")
  7. begin_fill()
  8. left(126)
  9. for i in range(5):
  10.     forward(n/5)
  11.     right(144)
  12.     forward(n/5)
  13.     left(72)
  14. end_fill()
  15. right(126)
  16. color("dark green")
  17. backward(n*4.8)
  18. def tree(d, s):
  19.     if d <= 0: return
  20.     forward(s)
  21.     tree(d-1, s*.8)
  22.     right(120)
  23.     tree(d-3, s*.5)
  24.     right(120)
  25.     tree(d-3, s*.5)
  26.     right(120)
  27.     backward(s)
  28. tree(15, n)
  29. backward(n/2)

R语言和Python绘制圣诞树

@方法二

  1. def paintleaves(m):
  2. for i in range(m):
  3. if(i == 10):
  4. print( ' '*(m-i) + '*'*( 2*i + 1-len( 'happy Christmas')) + 'happy Christmas'+ ' '*(m-i))
  5. continue
  6. if(i == 20):
  7. print( ' '*(m-i) + '*'*( 2*i + 1-len( 'happy Christmas')) +'happy Christmas'+ ' '*(m-i))
  8. continue
  9. if(i == m-1):
  10. print( ' '*(m-i) + 'happy Christmas'+ '*'*( 2*i + 1-len( 'happy Christmas')) + ' '*(m-i))
  11. continue
  12. print(' '*(m-i) + '*'*(2*i + 1) + ' '*(m-i))
  13. def paintTrunk(n):
  14. for j in range (8 ):
  15. print(' '*(n - 5) + '*'*10 + ' '*(n - 5))
  16. paintleaves(30)
  17. paintTrunk(30)

R语言和Python绘制圣诞树 @方法三

  1. import turtle
  2. screen = turtle.Screen()
  3. screen.setup(800,600)
  4. circle = turtle.Turtle()
  5. circle.shape('circle')
  6. circle.color('red')
  7. circle.speed('fastest')
  8. circle.up()
  9. square = turtle.Turtle()
  10. square.shape('square')
  11. square.color('green')
  12. square.speed('fastest')
  13. square.up()
  14. circle.goto(0,280)
  15. circle.stamp()
  16. k = 0
  17. for i in range(1, 17):
  18. y = 30*i
  19. for j in range(i-k):
  20. x = 30*j
  21. square.goto(x,-y+280)
  22. square.stamp()
  23. square.goto(-x,-y+280)
  24. square.stamp()
  25. if i % 4 == 0:
  26. x =  30*(j+1)
  27. circle.color('red')
  28. circle.goto(-x,-y+280)
  29. circle.stamp()
  30. circle.goto(x,-y+280)
  31. circle.stamp()
  32. k += 2
  33. if i % 4 == 3:
  34. x =  30*(j+1)
  35. circle.color('yellow')
  36. circle.goto(-x,-y+280)
  37. circle.stamp()
  38. circle.goto(x,-y+280)
  39. circle.stamp()
  40. square.color('brown')
  41. for i in range(17,20):
  42. y = 30*i
  43. for j in range(3):
  44. x = 30*j
  45. square.goto(x,-y+280)
  46. square.stamp()
  47. square.goto(-x,-y+280)
  48. square.stamp()
  49. turtle.exitonclick()

R语言和Python绘制圣诞树

R语言

@方法一

  1. 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,
  2. -0.08,0.150.22, -0.2,0.08,0.150.22,0.25, -0.1,0.120.25,-0.2,0.1,0.12,
  3. 0.2),nrow=4)
  4. B <- matrix(c(0,0,0,1.5,0,1.5,0,0.85,0,0.85,0,0.3,00.4),nrow=2)
  5. prob = c(0.020.6,.080.070.070.070.07)
  6. N = 1e5
  7. x = matrix(NA,nrow=2,ncol=N)
  8. x[,1] = c(0,2)
  9. k <- sample(1:7,N,prob,replace=TRUE)
  10. for(i in 2:N)
  11. {
  12. x[,i] = crossprod(matrix(L[,k[i]],nrow=2),x[,i-1]) + B[,k[i]]
  13. }
  14. par(bg='black',mar=rep(0,4))
  15. plot(x=x[1,],y=x[2,],col=grep('green',colors(),value=TRUE),axes=FALSE,
  16. cex=.1, xlab='', ylab='',pch='.')
  17. bals <- sample(N,20)
  18. points(x=x[1,bals],y=x[2,bals]-.1,col=c('red','blue','yellow','orange'),
  19. cex=1.5,pch=19)
  20. text(x=-.7,y=8, labels='Merry', adj=c(.5,.5), srt=45,
  21. vfont=c('script','plain'),cex=3,col='gold' )
  22. text(x=0.7,y=8,labels='Christmas',adj=c(.5,.5),srt=-45,
  23. vfont=c('script','plain'),cex=3, col='gold' )
  24. text(x=-0.6,y=0,cex=0.8,labels="@ZSRNOG",
  25. col="white)

R语言和Python绘制圣诞树

@方法二

  1. par(bg='black',mar=rep(0,4))
  2. plot(1:10,1:10,xlim=c(-5,5),ylim=c(0,10),type="n",xlab="",ylab="",xaxt="n",yaxt="n")
  3. rect(-1,0,1,2,col="tan3",border="tan4",lwd=3)
  4. polygon(c(-5,0,5),c(2,4,2),col="palegreen3",border="palegreen4",lwd=3)
  5. polygon(c(-4,0,4),c(3.5,5.5,3.5),col="palegreen4",border="palegreen3",lwd=3)
  6. polygon(c(-3,0,3),c(5,6.5,5),col="palegreen3",border="palegreen4",lwd=3)
  7. polygon(c(-2,0,2),c(6.25,7.5,6.25),col="palegreen4",border="palegreen3",lwd=3)
  8. points(x=runif(4,-5,5),y=rep(2,4),col=sample(c("blue","red"),size=4,replace=T),cex=3,pch=19)
  9. points(x=runif(4,-4,4),y=rep(3.5,4),col=sample(c("blue","red"),size=4,replace=T),cex=3,pch=19)
  10. points(x=runif(4,-3,3),y=rep(5,4),col=sample(c("blue","red"),size=4,replace=T),cex=3,pch=19)
  11. points(x=runif(4,-2,2),y=rep(6.25,4),col=sample(c("blue","red"),size=4,replace=T),cex=3,pch=19)
  12. points(0,7.5,pch=8,cex=5,col="gold",lwd=3)
  13. xPres = runif(10,-4.5,4.5)
  14. xWidth = runif(10,0.1,0.5)
  15. xHeight=runif(10,0,1)
  16. for(i in 1:10){
  17. rect(xPres[i]-xWidth[i],0,xPres[i]+xWidth[i],xHeight[i],col=sample(c("blue","red"),size=1))
  18. rect(xPres[i]-0.2*xWidth[i],0,xPres[i]+0.2*xWidth[i],xHeight[i],col=sample(c("gold","grey87"),size=1))
  19. }

R语言和Python绘制圣诞树

R语言实战(中文完整版)
精选各名校数学专业考研初试试卷
MySQL必知必会
2016年度中国软件开发者白皮书下载(PDF)

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: