Lab 10: Turtles and Fractals

Due 11:59pm Tuesday November 24

You may work with a partner on this lab.
A skeleton version of the program will appear when you run update21 in a terminal window. The program handin21 will only submit files in this directory.

Some of the problems will have optional components that allow you to further practice your skills in Python. Optional portions will not be graded, but may be interesting for those wanting some extra challenges.

Introduction
For this assignment you will write a program that implements a class of turtle objects. These are no ordinary turtles however as these turtles have a penchant for both the fine arts and computer science. These are turtles that can draw. Imagine placing a turtle on a canvas with a pen attached to his tail (no turtles are actually harmed in this process). This turtle can move forward and backward, turn right or left, and lift up or put down its tail. If the turtle moves while its tail is down, the pen leaves a trail behind the path of the turtle. You will write a class that allows you to create and manipulate drawing turtles. Then you will use this class to draw fractals. Note in the discussion below the turtle is purely virtual. You do not need to actually draw a physical turtle. You only need to draw the path the turtle takes.

A Turtle class
The current state of the turtle can be described by the following variables:

Begin by implementing the constructor and string methods:

To change or query the turtle's state, you can use a number of Turtle methods:


Testing the Turtle class
Turtle objects exist in a graphics window, whose center has the coordinates (0,0). When the turtle moves with its tail lowered, a line is drawn in the graphics window that tracks the turtle's movement. An example drawing is shown below for a turtle that initially starts facing East in the center of a 200x200 window.



Below is the code that was used to create this image:
  trevor = Turtle(200, 200)  # at (0,0) facing East
  trevor.left(90) # turn left 90 degrees (now facing North)

  # A triangle
  for i in range(3):
    trevor.forward(50)  # move forward 50 steps
    trevor.left(120)    # turn left 120 degrees

  trevor.up()           #stop drawing        
  trevor.goto(-80,-80)  #go to (-80, 80)
  trevor.down()         #resume drawing

  # A square
  for i in range(4):
    trevor.forward(40)  # move forward 40 steps
    trevor.right(90)    # turn right 90 degrees

  trevor.done()         # wait for mouse click to close window

You can also test your Turtle class with the drawHouse function that is included in the turtle.py file. Be sure to test the Turtle class on these examples before implementing the fractals described below.

Koch curves
The Zelle textbook gives an example on page 465-466 of a fractal called the "Koch curve" that can be drawn recursively. Different levels (0 through 3) of the Koch curve are shown below (level 0 on top, level 3 on bottom):

You will write a function koch(turtle, length, level) which draws a Koch curve of length length and level level. The koch function is not a method of the Turtle class.

The Koch curve is recursively defined:


Koch snowflakes

Write a function snowflake(turtle, sides, length, level) that can draw a Koch snowflake, as described below (an on page 465-466 of the Zelle textbook available in the lab). A Koch snowflake is formed by putting together multiple copies of the Koch curve. The snowflake function is not a method of the Turtle class.

The algorithm for drawing a Koch snowflake with n sides is:

for each side:
  draw a Koch curve of the appropriate length and level
  turn right 360.0/n degrees
The snowflake shown below is an example of a Koch snowflake made with three Koch curves.


Writing main
Your main function should consist of four or more primary tests of your turtle class. For the first test, make a call to the function drawHouse() included in your starting code. This function should draw the image below:

For the second test, make a call to the function drawCurves() included in your starting code. This function should draw the image below:

For the third test, write code to draw a level three snowflake with three sides. The final image should look similar to the snowflake above but with slightly less detail. The snowflake should be drawn in a separate graphics window and the window should close when the user clicks on it.

For the fourth test, create a new turtle and draw a pattern or scene of your own choosing. You may use your snowflake or koch functions, write additional functions, implement one of the optional components shown below, or do something else. After your scene is drawn, wait for the user to click on the scene before closing the window.

You may add additional test as desired. Each should appear in a new window and wait for a user to click the window before closing.

Optional Components
You may include any additional methods in your class. Some of these methods can make writing other methods easier or extend the feature of you class. As noted above, these questions are NOT required to receive full credit. Furthmore, do not attempt to solve these problems until the required portion of the assignment is complete. There are many extensions you could add to this program. Here are a few we thought might be interesting.
Submit

Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.