@[TOC]

Reading today, found a good program, good guy, no words, direct use.

This is what the best version looks like.The code is as follows:

code

from turtle import *
from random import *
from math import *

def tree(n,l) :
    pd()# writing
    # Shadow effect
    t = cos(radians(heading()+45)) /8+0.25
    pencolor(t,t,t)
    pensize(n/3)
    forward(l)# draw branches

    if n>0:
        b = random()*15+10 # Right branch deflection Angle
        c = random()*15+10 # Left branch deflection Angle
        d = l*(random()*0.25+0.7) The length of the next branch
        

        # Turn right and draw the right branch
        right(b)
        tree(n-1,d)
        # Turn left a certain Angle and draw the left branch
        left(b+c)
        tree(n-1,d)
        # back
        right(c)
    else:
        # painting leaves
        right(90)
        n=cos(radians(heading()-45)) /4+0.5
        ran=random()
        # Compared with the original randomly added filled circles, the cherry leaves look a little more
        if(ran>0.7):
            begin_fill()
            circle(3)
            fillcolor('pink')
        # Replaced the randomly generated leaves with a uniform pink color
        pencolor("pink")
        circle(3)
        if(ran>0.7):
            end_fill()
        left(90)
        Add 0.3 times the fallen leaves
        if(random()>0.7):
            pu()
            # falling
            t = heading()
            an = -40 +random()*40
            setheading(an)
            dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
            forward(dis)
            setheading(t)
            # painting leaves
            pd()
            right(90)
            n = cos(radians(heading()-45)) /4+0.5
            pencolor(n*0.5+0.5.0.4+n*0.4.0.4+n*0.4)
            circle(2)
            left(90)
            pu()
            # returns
            t=heading()
            setheading(an)
            backward(dis)
            setheading(t)
    pu()
    backward(l)# back

    
if __name__ == '__main__':    
    bgcolor(0.856.0.8255.0.8882)# Set the background color (change gray to mauve)
    ht()# hidden turtle
    speed(10)# Speed 1-10 progressive, 0 is the fastest
    tracer(0.0)
    pu()# pen up
    backward(50)
    left(90)# Turn left 90 degrees
    pu()# pen up
    backward(300)# back 300
    tree(12.100)# 12 levels of recursion
    done()

Copy the code

The principle of

1. The binary tree

As you can see from your own observation, our branches are all twofold and twofold. This is achieved by building a binary tree recursively. The middle is not complicated, but find the right number of levels to build the binary tree and draw with Turtle.

For example, when the number of recursive layers is small, we can easily find the composition of binary tree, and we can take the case by randomly selecting the length of each branch during construction.

2. Turtle library for Python

The cherry tree is drawn by Turtle in Python. The power of Python is that there are many powerful libraries, turtle is one of them. It is a library that comes with Python.

Introduction to basic can refer to this article: zhuanlan.zhihu.com/p/64594462

For details, please check the official document: docs.python.org/zh-cn/3/lib…

Draw a tree for the girl you like

Recursive effect exploration

Five layers

When you recurse five levels, it looks like this. It looks bald

Seven layers

Ten layer

Twelve floors (best)

14 layer

There are so many of them, it’s not pretty, it takes a few minutes for the program to run.

(I wish programmers had hair as thick as this,(* ^ del ^ *))