Python's turtle module provides an intuitive way too create graphics, making it an excellent tool for visualizing recrusive algorithms like fractal trees. This section explores various implementations to draw different styles of trees.
1. Dynamic Grayscale Fractal Tree with Animated Leaves
This implementation constructs a tree with branches that thin and fade with each recursive level, topped with small, temporary leaf clusters. It also simulates leaves falling from the tree.
import turtle
import random
import math
def draw_fractal_tree_v1(current_level, segment_length):
"""
Recursively draws a fractal tree with dynamic branch properties and falling leaves.
current_level: The current recursion depth.
segment_length: The length of the current branch segment.
"""
turtle.pendown()
# Calculate color intensity and line thickness based on the current heading and level
shade_factor = math.cos(math.radians(turtle.heading() + 45)) / 8 + 0.25
turtle.pencolor(shade_factor, shade_factor, shade_factor) # Grayscale for branches
turtle.pensize(current_level / 3)
turtle.forward(segment_length) # Draw the branch segment
if current_level > 0:
# Randomize angles and length for sub-branches
right_branch_angle = random.uniform(10, 25) # Angle for the right branch
left_branch_angle = random.uniform(10, 25) # Angle for the left branch
next_segment_scale = segment_length * random.uniform(0.7, 0.95) # Scale factor for next segment
# Draw right branch
turtle.right(right_branch_angle)
draw_fractal_tree_v1(current_level - 1, next_segment_scale)
# Draw left branch
turtle.left(right_branch_angle + left_branch_angle)
draw_fractal_tree_v1(current_level - 1, next_segment_scale)
# Return to original heading for the current level
turtle.right(left_branch_angle)
else:
# At the lowest level, draw a leaf-like circle
turtle.right(90)
leaf_color_intensity = math.cos(math.radians(turtle.heading() - 45)) / 4 + 0.5
# A reddish hue for leaves
turtle.pencolor(leaf_color_intensity, leaf_color_intensity * 0.8, leaf_color_intensity * 0.8)
turtle.circle(3)
turtle.left(90)
# Add a chance for a falling leaf animation
if random.random() > 0.7:
turtle.penup()
original_heading = turtle.heading()
fall_direction_angle = random.uniform(-40, 0) # Angle for leaf fall direction
turtle.setheading(fall_direction_angle)
fall_distance = int(random.uniform(200, 800)) # Random fall distance
turtle.forward(fall_distance)
turtle.setheading(original_heading)
# Draw the falling leaf
turtle.pendown()
turtle.right(90)
falling_leaf_shade = math.cos(math.radians(turtle.heading() - 45)) / 4 + 0.5
turtle.pencolor(falling_leaf_shade * 0.5 + 0.5, 0.4 + falling_leaf_shade * 0.4, 0.4 + falling_leaf_shade * 0.4)
turtle.circle(2)
turtle.left(90)
turtle.penup()
# Return the turtle to its original position after drawing the falling leaf
turtle.setheading(fall_direction_angle)
turtle.backward(fall_distance)
turtle.setheading(original_heading)
turtle.penup()
turtle.backward(segment_length) # Retreat after drawing branch
# --- Main setup for Tree V1 ---
turtle.bgcolor(0.5, 0.5, 0.5) # Set background to a neutral gray
turtle.hideturtle()
turtle.speed(0) # Fastest speed
turtle.tracer(0, 0) # Turn off screen updates for smoother animation
turtle.penup()
turtle.left(90) # Orient upwards
turtle.backward(300) # Start from the bottom of the screen
draw_fractal_tree_v1(12, 100) # Initiate drawing with 12 recursion levels and initial length 100
turtle.done()
2. Gradient Color-Changing Recursive Tree
This example features a tree where branch colors porgressively shift as the recursion deepens, creating a vibrant gradient effect. The pen width also decreases with each level.
import turtle
def draw_gradient_tree(branch_length, current_level):
"""
Recursively draws a tree with gradient colors and decreasing branch width.
branch_length: The length of the current branch segment.
current_level: The current recursion depth.
"""
global global_red, global_green, global_blue # Access global color components
# Save current pen width and reduce it for sub-branches
initial_width = turtle.width()
turtle.width(initial_width * 0.75)
# Update global RGB components and set new pen color
global_red = (global_red + 1) % 200
global_green = (global_green + 2) % 200
global_blue = (global_blue + 3) % 200
turtle.pencolor(global_red, global_green, global_blue)
next_branch_length = 0.75 * branch_length
turn_angle = 45 # Fixed angle for branches
turtle.left(turn_angle)
turtle.forward(next_branch_length)
if current_level < MAX_RECURSION_LEVEL:
draw_gradient_tree(next_branch_length, current_level + 1)
turtle.backward(next_branch_length)
turtle.right(2 * turn_angle)
turtle.forward(next_branch_length)
if current_level < MAX_RECURSION_LEVEL:
draw_gradient_tree(next_branch_length, current_level + 1)
turtle.backward(next_branch_length)
turtle.left(turn_angle)
# Restore pen width to its state before this level's call
turtle.width(initial_width)
# --- Main setup for Tree V2 ---
turtle.colormode(255) # Use RGB color mode
turtle.hideturtle()
turtle.left(90) # Point turtle upwards
MAX_RECURSION_LEVEL = 14
INITIAL_BRANCH_LENGTH = 120
turtle.width(MAX_RECURSION_LEVEL) # Set initial pen width
# Initialize global RGB components for the base of the tree
global_red, global_green, global_blue = 0, 0, 0
turtle.pencolor(global_red, global_green, global_blue)
turtle.penup()
turtle.backward(INITIAL_BRANCH_LENGTH)
turtle.pendown()
turtle.forward(INITIAL_BRANCH_LENGTH)
turtle.speed("fastest")
draw_gradient_tree(INITIAL_BRANCH_LENGTH, 4) # Start drawing
turtle.done()
3. Cherry Blossom Tree with Falling Petals
This fractal tree simulation aims to resemble a cherry blossom tree, featuring branches with varying shades of pink and white, and scattered petals around its base.
import turtle
import random
import time
def draw_cherry_branch(length, t_obj):
"""
Recursively draws a branch of the cherry blossom tree.
length: The length of the current branch segment.
t_obj: The turtle object used for drawing.
"""
if length > 3:
# Adjust branch color and thickness based on length
if 8 <= length <= 12:
t_obj.color('snow' if random.randint(0, 2) == 0 else 'lightcoral')
t_obj.pensize(length / 3)
elif length < 8:
t_obj.color('snow' if random.randint(0, 1) == 0 else 'lightcoral')
t_obj.pensize(length / 2)
else:
t_obj.color('sienna') # Base branch color
t_obj.pensize(length / 10)
t_obj.forward(length)
angle_variation = 1.5 * random.random()
t_obj.right(20 * angle_variation)
length_reduction_factor = 1.5 * random.random()
draw_cherry_branch(length - 10 * length_reduction_factor, t_obj)
t_obj.left(40 * angle_variation)
draw_cherry_branch(length - 10 * length_reduction_factor, t_obj)
t_obj.right(20 * angle_variation)
t_obj.penup()
t_obj.backward(length)
t_obj.pendown()
def generate_petals(count, t_obj):
"""
Generates and draws 'petals' scattered around the base of the tree.
count: The number of petals to draw.
t_obj: The turtle object used for drawing.
"""
for _ in range(count):
# Randomize petal position
x_offset = random.uniform(-200, 200)
y_offset = random.uniform(-20, 10)
t_obj.penup()
t_obj.forward(y_offset) # Move slightly forward/backward
t_obj.left(90)
t_obj.forward(x_offset) # Move sideways
t_obj.pendown()
t_obj.color("lightcoral") # Petal color
t_obj.circle(1) # Small circle for a petal
t_obj.penup()
t_obj.backward(x_offset) # Return x
t_obj.right(90)
t_obj.backward(y_offset) # Return y
# --- Main setup for Tree V3 ---
def setup_cherry_tree():
drawing_turtle = turtle.Turtle()
screen = turtle.Screen()
screen.tracer(5, 0) # Accelerate drawing, update every 5 frames
screen.screensize(bg='wheat') # Set background color
drawing_turtle.hideturtle()
drawing_turtle.left(90) # Orient upwards
drawing_turtle.penup()
drawing_turtle.backward(150) # Position starting point
drawing_turtle.pendown()
drawing_turtle.color('sienna') # Initial trunk color
draw_cherry_branch(60, drawing_turtle) # Start drawing the tree
generate_petals(100, drawing_turtle) # Add petals
screen.exitonclick()
setup_cherry_tree()
turtle.done()
4. Simplified Grayscale Recursive Tree
This tree variant offers a more minimalist grayscale design, focusing on the core recursive branching structure with minor randomization in branch angles and lengths.
import turtle
import random
import math
def draw_minimal_tree(level, current_length):
"""
Recursively draws a simplified grayscale tree structure.
level: The current recursion level.
current_length: The length of the current branch segment.
"""
turtle.pendown()
# Calculate color intensity and pen size based on current orientation and level
pen_shade = math.cos(math.radians(turtle.heading() + 45)) / 8 + 0.25
turtle.pencolor(pen_shade, pen_shade, pen_shade) # Grayscale
turtle.pensize(level / 4)
turtle.forward(current_length)
if level > 0:
# Randomize branching angles and length reduction
angle_right = random.uniform(10, 25)
angle_left = random.uniform(10, 25)
length_scale = current_length * random.uniform(0.6, 0.95)
# Draw right sub-branch
turtle.right(angle_right)
draw_minimal_tree(level - 1, length_scale)
# Draw left sub-branch
turtle.left(angle_right + angle_left)
draw_minimal_tree(level - 1, length_scale)
# Return to original heading
turtle.right(angle_left)
else:
# At the final level, draw a small dark leaf-like circle
turtle.right(90)
leaf_intensity = math.cos(math.radians(turtle.heading() - 45)) / 4 + 0.5
turtle.pencolor(leaf_intensity, leaf_intensity, leaf_intensity)
turtle.circle(2)
turtle.left(90)
turtle.penup()
turtle.backward(current_length) # Retreat after drawing
# --- Main setup for Tree V4 ---
turtle.bgcolor(0.5, 0.5, 0.5) # Neutral gray background
turtle.hideturtle()
turtle.speed(0) # Fastest drawing speed
turtle.tracer(0, 0) # Disable screen updates for speed
turtle.left(90) # Orient upwards
turtle.penup()
turtle.backward(300) # Start from the bottom
draw_minimal_tree(13, 100) # Draw tree with 13 levels and initial length 100
turtle.done()