Review our work from class on Tuesday 3/6 and complete this prelab before lab on Thursday / Friday.
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.
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.
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:
Move forward by some amount (this draws the trunk)
Turn left by 45 degrees
Draw a smaller version of the tree (e.g., perhaps half size)
Turn right by 90 degrees (you should actually think of this as undo-ing the previous 45 degrees and then turning right 45 degrees)
Draw a smaller version of the tree
Turn left by 45 degrees (i.e., undo the turn to the right)
Move backward by the length of the trunk (the turtle should now be in its original position and orientation)
Here is what that tree looks like taken out to three generations:
And here it is taken out to eight generations:
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)
turtle.tracer(False)
# pick up the pen and move the turtle so it starts at the left edge of the canvas
turtle.penup()
turtle.goto(0, -turtle.window_height()/2 + 20)
turtle.left(90)
turtle.pendown()
# draw the tree by calling your function
drawTree(200,5)
# finished
turtle.update()
turtle.done()
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.