CS101 - HW4 Prelab

Review our work from class on Tuesday 3/6 and complete this prelab before lab on Thursday / Friday.

Create your file, add proper comments

From within the Thonny application, create a single Python file username_hw4.py with all your work for this assignment in it. Include a comment at the top with your name, the date and homework number, and your lab section.

Add stubs for all functions

Add "stubs" (i.e., empty function definitions that you will finish in lab and for homework) for each of the functions you'll write for this homework, including drawTree(), drawTreeScene(), drawSierpinski(), drawSierpinskiScene(). At this point your file should already have comments at the top of the file and for each function definition, and the file will compile without errors. Once you have your file created, try to replicate what we did in class on Tuesday so that you can draw a simple tree as described in the section below.

Drawing Trees

Trees are another self-similar shape we can draw with the turtle.

We'll draw a simple recursive tree having the important feature that the turtle is left in exactly the same position and orientation after drawing a tree. Using this, the basic tree shape can be accomplished by following these steps:

Here is what that tree looks like taken out to three generations:

Lab04 Tree3

And here it is taken out to eight generations:

Lab04 Tree8

Write the tree drawing function

Write a function called drawTree(length, generations) that follows the above algorithm. You can write this like the spiral, where there is nothing to be done if generations is zero (or more precisely, only do something if generations is greater than zero). Use length for the length of the trunk, and pass length/2 as the length for the smaller trees.

Here is a drawTreeScene() function for you that will make sure the tree is not laying on its side. See also the similar Tuesday 3/6/18 example shown in class.

def drawTreeScene():
    Moves the turtle to the bottom of the screen, points it upward, and draws a tree.
    # turn off drawing animation (too slow otherwise)
    # pick up the pen and move the turtle so it starts at the left edge of the canvas 
    turtle.goto(0, -turtle.window_height()/2 + 20)

    # draw the tree by calling your function

    # finished

What should be in the file

For the prelab portion, you should have 2 functions now written (drawTree() and drawTreeScene()) and 2 function stubs (drawSierpinski() and drawSierpinskiScene()). Each function should have a docstring, and the top of the file should include a comment listing (at a minimum) your name, the date and homework number, and your lab section. In the next section of HW4, you'll modify the drawTree() function to add randomness, color, and other variations.