As a non famous and non professional painter in the world , Of course, let's enjoy my masterpiece of the century first :
Of course, a well-known painter must master different painting styles , Now let's enjoy another masterpiece of mine :
as everyone knows , As a programmer , Of course not a cripple , The hand speed gained by tapping the keyboard day and night and going to the toilet at night , It's not a minute to draw such a cherry tree ? This cherry tree , We used python of turtle modular , Using some mathematical functions , Randomly draw different branches and falling petals , Each time we run, we generate a relatively unique tree ( If the random numbers are not the same ). No more nonsense , Look at the source code :
import turtle
from random import *
from math import *
def tree(n, l):
turtle.pendown()
t = cos(radians(turtle.heading() + 45)) / 8 + 0.25
turtle.pencolor(t, t, t)
turtle.pensize(n / 3)
turtle.forward(l)
if n > 0:
b = random() * 15 + 10
c = random() * 15 + 10
d= l * (random() * 0.25 + 0.7)
turtle.right(b)
tree(n - 1, d)
turtle.left(b + c)
tree(n - 1, d)
turtle.right(c)
else:
turtle.right(90)
n = cos(radians(turtle.heading() - 45)) / 4 + 0.5
turtle.pencolor(n, n*0.8, n*0.8)
turtle.circle(3)
turtle.left(90)
z=random()
if z>0.7:
turtle.up()
t = turtle.heading()
an = -40 + random()*40
turtle.setheading(an)
dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
turtle.forward(dis)
turtle.setheading(t)
turtle.down()
turtle.right(90)
n = cos(radians(turtle.heading() - 45)) / 4 + 0.5
turtle.pencolor(n*0.5+0.5, 0.4+n*0.4, 0.4+n*0.4)
turtle.circle(2)
turtle.left(90)
turtle.up()
t = turtle.heading()
turtle.setheading(an)
turtle.backward(dis)
turtle.setheading(t)
turtle.up()
turtle.backward(l)
turtle.bgcolor(0.5, 0.5, 0.5) # Background color
turtle.hideturtle() # hide turtle
turtle.speed(0) # speed ,1-10 gradual ,0 Fastest
turtle.tracer(0, 0)
turtle.up() # pen-up
turtle.backward(100)
turtle.left(90) # Turn left 90 degree
turtle.up() # pen-up
turtle.backward(300) # back off 300
tree(12, 100) # recursion 7 layer
turtle.done()
Technology