Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The leaves of ginkgo biloba are shaped like hearts, with one handle and two leaves representing two loving hearts linked together. Ginkgo tree is also called Gongsun tree, can blossom and bear fruit for thousands of years, although the statement is exaggerated, but said that only after a long waiting, can keep the blossom and bear fruit, symbolizing the long years of guarding love and the final integration of two as one.

Ginkgo biloba is also a contemporary of dinosaurs and is known as a living fossil. It has strong adaptability, a long growth period and a life span of up to 1,000 years. Therefore, ginkgo biloba is a symbol of longevity. Next, use Python’s Turtle library to draw beautiful scenes of ginkgo trees.

1. Import required libraries

import turtle

import random

from math import *

2. Generate Fibonacci numbers

The Fibonacci sequence is a sequence where the sum of the first two terms adds up to the last term. Two functions are used to generate the Fibonacci sequence.

Def Fibonacci_Recursion_tool(n): # Fibonacci_Recursion_tool(n): # if n <= 0: return 0 elif n == 1: return 1 else: return Fibonacci_Recursion_tool(n - 1) + Fibonacci_Recursion_tool(n - 2) def Fibonacci_Recursion(n): Result_list = [] for I in range(1, n + 3): result_list.append(Fibonacci_Recursion_tool(i)) return result_listCopy the code

Call the function to generate a sequence as follows:

Print (yu) = Fibonacci_Recursion(top) #Copy the code

The running results are as follows:

3. Define the method for generating leaves

Def leaf(x, y, node): til = turtle.heading() I = random.random() an = random.randint(10,) def leaf(x, y, node): til = turtle.heading() I = random. 180) ye = random.randint(6, 9)/10 turtle.color(ye, ye*0.9, 0) turtle.fillcolor(ye+0.1, ye+0.05, 0) turtle.pensize(1) turtle.pendown() turtle.setheading(an + 90) turtle.forward(8*i) px = turtle.xcor() py = Turtle.ycor () turtle.begin_fill() turtle.circle(7.5* I, 120) # Draw a 120-degree arc turtle.penup() # Lift the pen turtle.goto(px, Py) # return to dot position turtle.setheading(an + 90) # draw up turtle.pendown() # pen, Start drawing Turtle. circle(-7.5* I, 120) # Draw a 120 degree arc turtle.setheading(an + 100) Turtle. circle(10.5* I, Turtle.end_fill () # Draw a 150-degree arc turtle.penup() turtle.goto(x, y) turtle.setheading(til) turtle.pensize(node / 2 + 1)Copy the code

4. Define the method of spanning tree

I’m going to use x to generate random numbers, and I’m going to use the if condition to decide whether or not I’m going to continue to draw branches, whether or not I’m going to draw leaves, to make the tree a little bit more natural, a little bit more random, a little bit more interesting, and that’s going to cause every time you run it, you’re going to draw a different tree. I’ve annotated the details. If you want to adjust the proportion of leaves in the air and the degree of tree bifurcation, modify the value range of X in the if judgment statement to increase or decrease the probability. As for how to achieve the perfect effect in your heart to try slowly.

def draw(node, length, level, yu, button): Turtle.pendown () t = cos(radians(turtle.heading()+5)) / 8 + 0.25 turtle.pencolor(t*1.6, t*1.2, t*1.4) #(r, g, Pensize (node/1.2) # generate a random number to determine whether to draw a tree or a falling leaf if level == top and x > 6: # Now paint falling leaves, Forward (length) # Draw twig yu[level] = yu[level] -1 c = random. Randint (2, 10) for I in range(1, c): Leaf (turtle.xcor(), turtle.ycor(), node) # Add 0.3 times the fallen leaf if random.random() > 0.3: Turtle.penup () # fall T1 = turtle.heading() AN1 = -40 + random.random() * 40 Turtle.setheading (an1) dis = int(800 *) Random. Random () * 0.5 + 400 * random. Random () * 0.3 + 200 * random. Random () * 0.2) Turtle.forward (dis) Turtle.setheading (T1) turtle.right(90) # Draw leaf(turtle.xcor(), turtle.ycor(), Node) turtle.left(90) # Return T2 = turtle.heading() turtle.setheading(an1) turtle.backward(dis) turtle.setheading(T2) elif level==top and x < 7 : Turtle.penup () turtle.forward(length) elif level>3 and (x>6) : There is a 40% chance of executing the following strategy turtle.pendown() turtle.forward(length) c = random.randint(4, 6) for I in range(3, c): leaf(turtle.xcor(), turtle.ycor(),node) leaf(turtle.xcor(), turtle.ycor(),node) button=1# jump""" else: Turtle. forward(length) # Yu [level] = yu[level] -1 if node > 0 and button == 0: # Calculate the right branch deflection Angle and add a random offset to the fixed Angle deflection right = random.random() * 5 + 17 # Calculate the left branch deflection Angle and add a random offset to the fixed Angle deflection left = random.random() * Child_length = length * (random. Random () * 0.25 + 0.7) r=random. Randint (0, 1) if r==1: turtle.right(right) level = level + 1 #print("level", level) else: turtle.left(right) level = level + 1 #print("level", level) draw(node - 1, Child_length,level,yu,button) yu[level] = yu[level] +1 if yu[level] > 1: Turtle. left(right + left) draw(node-1, child_length, level, yu,button) # Turtle. right(left) yu[level] = yu[level] -1 else: Turtle. right(right + left) draw(node-1, child_length, level, yu,button) # Left (left) yu[level] = Yu [level] -1 else: if r==1: turtle.left(right + left) turtle.right(left) else: Turtle.right (right + left) turtle.left(left) turtle.penup() # Return to the top position of the upper-level node turtle.backward(length) 5. Top controls the height of the tree, turtle.speed controls the speed of drawing, and turtle.write () is used to write the signature at the bottom. ```clike if __name__ == '__main__': Turtle.setup (width=1.0, height=1.0) # set full screen display turtle.hideturtle() # hideturtle turtle.speed(0) # set speed of pen movement, Tracer (0,0) # Set the animation switch and delay to 0 turtle.penup() # Lift the brush turtle.left(90) # Default orientation is positive toward the X axis, Backward (300) # Set the position of Turtle, Yu = Fibonacci_Recursion(top) #print(yu) Fibonacci_Recursion(top) #print(yu) Fibonacci_Recursion(top) #print(yu) #print(yu) Fibonacci_Recursion(top) Write (" WSW ", font=(" Microsoft Yahei ", 14, "normal")) # Generate signature turtle.done()Copy the code

After running the program, the “Turtle” will help you draw the whole tree, you just need to watch it draw, you need to wait for a certain amount of time, the final product is as follows, is the desired feeling of half the leaves in the air.

More interesting source code sharing, you can pay attention to me, private free sharing.