🌲 Drawing Trees 🌴

using recursion

Prior Knowledge

To access the content of this lesson, you should be comfortable defining and calling a Python function, as well as understanding the difference between parameters and arguments.
Additionally, you need to have a solid grasp of the following functions from the Python turtle module:

  • goto(x, y)
  • forward(n)
  • setheading(angle)
  • penup() and pendown()

 

Look at the code to your right and try to guess the shape that would get drawn.

▶️ to run

✏️ to view the code

Learning Objectives

1. Step by step, create a recursive function that draws a simple tree.

2. Add randomness to give the tree a more natural appearance.

3. Explore the elements and essential components that define a recursive function.

1. Drawing a simple tree

Let's break down the problem of drawing a tree into its most essential part. What might this be?

 

A. drawing the trunk

B. drawing a branch

C. drawing the outline

B. drawing a branch

We can consider the trunk a special and important type of branch, while drawing the outline of a tree doesn't really break down the problem does it?

draw_branch()

▶️ to run

✏️ to view the code

draw_branch() accepts the following parameters:

  • x, y as starting coordinates
  • angle, length and width of the branch.

And draws the branch for us.

 

Practice running the code and viewing the source before you complete the tasks.

Draw the practice branches always from bottom to top.

Reveal tasks

Reveal solution
Reveal solution

Task 1: Modify the code to draw

draw_branch(0, 0, 90, 150, 5)

Task 2: Modify the code to draw

draw_branch(-50, -100, 45, 212, 5)

Adding two more branches

The below diagram might help you so keep it in mind.

Task 1:

Call the draw_branch() function two more times,  knowing the length will be 75% of the main trunk, and the width 1px less.

Task 2:

 

 

Add these lines to the end of the draw_branch() function, which of these coordinates are shown?

 

  • (53.03, -53.03)
  • (56.88, 56.88)
  • (-53.03, 53.03)
  • (-56.88, 56.88)

45°

45°

draw_branch(0, 0, 135, 75, 4)
draw_branch(0, 0, 45, 75, 4)
end_x, end_y = t.pos()
print(end_x, end_y)
Reveal solution

Reveal solution

The recursive step

Let's dynamically calculate new length, width and angle values within the draw branch function:

  • length: 75%
  • angle: +/- 45°
  • width: -1px

now let's draw the next two branches based of the ending coordinates and new values, within the same function.

Task 1:

Uncomment the lines of the recursive step and complete them.

45°

45°

new_length = length * 0.75
new_angle_left = angle + 45
new_angle_right = angle - 45
new_width = width - 1
draw_branch(end_x, end_y, new_length, new_angle_left, new_width)
draw_branch(end_x, end_y, new_length, new_angle_left, new_width)
Reveal solution

Which branch gets drawn for you?

 

left / right

No wrong answer here! Depending on which angle you chose to draw the first branch with, you are stuck in a recursive loop. More on that soon!

😱 Stuck in a recursive loop?

Let's pause and read our first formal definition of recursion:

a problem-solving technique where a function calls itself to break down a complex problem into smaller, similar subproblems.

def draw_branch(x, y, angle, length, width):
    t = turtle.Turtle()
    
    # Draw branch (hidden)
    
    # Recursive step
    end_x, end_y = t.pos()
    new_length = length * 0.75
    new_angle_left = angle + 45
    new_angle_right = angle - 45
    new_width = width - 1
    draw_branch(end_x, end_y, new_angle_left, new_length, new_width)
    draw_branch(end_x, end_y, new_angle_right, new_length, new_width)

It should become clear now that we are trying to solve our tree drawing problem using a recursive branch drawing function which calls itself to draw smaller, similar branches every time.

The only issue, is that we are stuck in a recursive loop. Let's see why that is by calling our function and tracing code execution!

draw_branch(0,-100,90,100,5)

We will never be able to draw any right branches... unless?

Recursive Trees

By Jakob Stanley Warth

Recursive Trees

Learn recursion by drawing trees with python's turtle library

  • 60