Day 6
Reading Journal Review
Pair with someone who did at least one of the exercises that you didn’t do from section 4.12. First, discuss your approaches to exercises 1-4 in Chapter 4.3. Make sure to clear up any misconceptions that you may have. Finally, discuss the approaches taken in the book to refactor the code. Make sure that you are clear on the advantages of the refactored code.
Now, turn your attention to the exercises in 4.12. Take turns presenting one partner’s solution to one of the problems that the other partner didn’t attempt. Try to get across both your high-level approach to the problem as well as your specific implementation in Python. If you have any suggestions for code improvements that your partner code make, feel free to suggest them, but be sure to deliver this feedback in a constructive manner.
More Fun with Turtles
Take your turtles to the next level! There are a bunch of ways you might do this.
Continue with Think Python
You can continue to work through the exercises in 4.12 of Think Python, there’s a lot of good stuff there.
Freestyle
Find a cool line drawing and try to replicate it with Python code!
Teleportation, Cloning, and Other Unethical Experiments on Turtles
A Turtle is a Python object, which we will learn more about next week. Turtles
have methods, which we can call to inspect change their behavior. One trick that will be useful here, which you saw in shapes.py
but may not have thought about much, is the speed()
function. The speed()
function can be used to speed up slowpoke Turtles. While it seems weird that a speedy turtle would have a speed of 0, in this case the input 0
is reserved for having the turtle go as fast as possible (remember, when in doubt, check
the documentation).
import turtle
speedy = turtle.Turtle()
speedy.speed(0)
Other important Turtle methods include xcor()
and ycor()
position, and
heading()
.
Read more about turtles here.
Since Turtles are simple creatures, mainly defined by their current position and heading, we can “clone” them by reading these values and using them to direct a new Turtle.
leo = turtle.Turtle()
# leo does some arbitrary drawing (e.g., makes a 45 degree angle)
leo.fd(100)
leo.lt(45)
leo.fd(100)
# Create a new Turtle with the same attributes as the first
don = turtle.Turtle()
don.penup()
don.setx(leo.xcor())
don.sety(leo.ycor())
don.setheading(leo.heading())
if leo.isdown():
don.pendown()
# don.bandana_color = "purple" # TODO: Ninja functionality not yet implemented
Exercise: encapsulate this functionality in a clone
function that
takes a Turtle argument and returns a new Turtle with the same position
and heading, leaving the original Turtle untouched.
Draw Parametric Curves
A parametric curve in 2D is defined by two functions, and (where is some parameter, which we can think of as time). If we imagine our turtle traveling along this curve, we can use calculus to compute various properties of our turtle’s motion. For instance, the speed of the turtle can be calculated as follows.
Further you can calculate, your turtle’s heading as follows.
Using these ideas you can program your turtle to draw arbitrary parametric curves. Suppose we want our turtle to draw a sine wave with amplitude of 10 pixels and a period of pixels. This path can be defined as follows.
Additionally, the heading and speed functions are defined as follows.
Challenge: write a Python program that takes as input a parametric curve and uses the turtle
module to draw it.
Hint: the method presented above to compute the turtle’s heading has some numerical issues (e.g., when ). A better way to compute the heading is using the math.atan2
function. Suppose we have computed and stored it in a variable dx
and we have computed and stored it in a variable dy
, the heading (in radians) can be computed as math.atan2(dy, dx)
.
Once you can draw a parametric curve, you can draw some really cool stuff like Spirographs!