In this assignment, you’ll use the Turtle graphics system that you learned about in the reading, along with your powerful new recursion techniques, to create fractals. A fractal is a simple pattern that repeats itself on smaller scales, resulting in attractive pictures.

Lab Partnership Planning

Before starting today’s lab assignment, exchange contact information with your partner and find at least two hours in your schedule during which you can meet with your partner to finish the lab. If you are genuinely unable to find a time, please come speak with me!

Fractal Tree

An example of a fractal tree, very similar to the one we will make today with Turtle.
Image source

We’re going to create fractal trees. A fractal tree is a graphic in which a single line divides into two or more lines at an angle, repeatedly. The result is an elegant tree-like structure.

0. Create Your Script

Make a new file called fractal-tree.py and save it somewhere you’ll be able to find it. On the first line, copy this to import turtle:

import turtle

1. Draw a Vertical Line

In this lab, we’re going to practice incremental development. We’ll start with a very simple function. Then, at each stage, we’ll add just a bit of complexity until we achieve the behavior that we are looking for.

Here’s the first step:

  1. Define a function called draw_tree() that accepts a single argument called length.
  2. This function should cause the turtle to move forward by length, and then backwards by the same length. So, the turtle ends where it started.

Call your function like this:

draw_tree(100)

At this stage, the expected result is that your turtle moves up and then down, tracing a single vertical line.

Hint: Remember that you can move the turtle forward with the command turtle.forward(length). You can move the turtle backward with the command turtle.backward(length)

2. Pause to Look Around

Next, make your turtle look around after the forward step and before the backward step.

  1. Add an argument to draw_tree() called angle.
  2. After the forward step, make the turtle turn angle degrees to the right.
  3. Then, make the turtle turn 2*angle degrees to the left.
  4. Make the turtle turn angle degrees to the right again.
  5. Then finish with the backward step.

Call your function like this:

draw_tree(100, 90)

The end result is still a simple vertical line. When you run the file, you should still see your turtle move straight up and down. After it moves up, though, you should see it “look around” to both the right and the left before returning to the starting point.

Hint: Remember that you can make the turtle turn left with turtle.left(angle).

3. Establish the Base Case

We are going to use recursion to draw the tree. Modify your function in the following ways:

  1. Add an argument to your function called level.
  2. Add an if-statement to your function. Check if level == 0. If so, do nothing! You can do nothing by simply writing pass inside the body of the if-statement. This is our base case.
  3. In the else clause, place the code you’ve written previously.

Call your function twice:

draw_tree(100, 90, 0) # should do nothing
draw_tree(100, 90, 1) # should do same thing as in Part 2

4. Implement the Recursive Step

This is where the magic happens! Add two recursive function calls, one immediately after the turtle turns right and another immediately after the turtle turns left.

Each call should decrease the length and the level.

  • Decrease length by a multiple. For example, the new length is 75% of the old length.
  • Decrease level by 1. For example, if the previous level was 5, the new level is 4.

The purpose of decreasing the level is to ensure that your function will eventually stop.

Here’s a sample function call and result:

draw_tree(100, 45, 3)


Example output for a tree with 3 levels.

Your result might be a little different, depending on exactly how you chose to reduce the length in each step. In any case, however, you should see a “tree” with two large branches and four smaller branches.

Here’s another one. The first command makes the turtle move more quickly, which is helpful when the figure gets more complex.

turtle.speed(.1)
draw_tree(100, 15, 7)


Example output for a tree with 7 levels

5. Make Some Pretty Pictures!

Play a little with all three arguments of the draw_tree() function in order to produce some attractive trees.

6. Leaves


Example output for the “Leaves” problem.

The commands turtle.color("green") and turtle.color("brown") will change the color of the line drawn by the turtle. The command turtle.circle(4) will draw a circle. Using these new commands, add color and leaves to your tree.

If your implementation results in each branch having two green circular leaves (like mine does above), explain this: why are there two circles on each branch? Write your response in a comment!

Submit Your Script

Please submit the file fractal-tree.py on Gradescope, including the names of both you and your partner with your submission. We are not autograding this assignment, so you don’t need to worry about whether you have exactly the expected output when the functions are called. You just need to check that your functions work correctly and that you have followed the instructions throughout this assignment.

Partner Feedback

Fill out the partner feedback form by the lab due date for participation points!

Nice work!