# Import the turtle package, so we can draw
import turtle
# Need math for math.pi
import math
# Create a turtle
ttl = turtle.Turtle()
# Create a screen on which we can play
screen = turtle.Screen()
# Configure the playing field
dimX = 400
dimY = 400
# I have a fudge factor here to account for windows being too small
fudgeFactor = 100
screen.setup(dimX+fudgeFactor, dimY+fudgeFactor)
Call the following reset()
function whenever you want to start your turtle instance over.
def resetTurtle():
"""Call this function to reset your screen as necessary"""
# Reset the screen
turtle.resetscreen()
screen.reset()
screen.screensize(dimX, dimY)
Using the ttl
variable created above, use a for loop to draw a square near the center of your screen. The square should be 100 pixels on a side and look like:
resetTurtle()
# Draw four sides, each of length 100, and turn 90 degrees between each line
for i in range(4):
ttl.forward(100)
ttl.left(90)
Using the ttl
variable and a for loop of 50 iterations, create a square-based spiral similar to the picture below. To draw this shape, in your for loop, increment the forward distance your ttl
moves every odd iteration, starting with a distance of 10.
resetTurtle()
dist = 10
# For fifty iterations...
for i in range(50):
# Every other iteration (i.e., every even iteration), increase your distance
# This step ensures we draw lines of increasing size to get the nice
# spiral effect
if i % 2 == 0:
dist += 10
# Draw a line of length dist and turn
ttl.forward(dist)
ttl.left(90)
As in the Exercise 2, draw a spiral, but this time, add an additional few degrees to your turtle's rotation (e.g., turn left 92 degrees). Start with the same initial distance and iteration count as in Exercise 2.
Your output should look like the following:
resetTurtle()
dist = 10
# Again, fifty iterations...
for i in range(50):
# Increment every other side's length
if i % 2 == 0:
dist += 10
# This time, turn 92 degrees, just off from 90.
# Over several iterations, it creates a cool effect
ttl.forward(dist)
ttl.left(92)
Getting a little more complex, use your ttl
to draw a circle of radius 100. To do this task, draw the circle in 12-degree increments over 30 iterations. In each iteration, move turtle forward an appropriate distance calculated by dividing $\frac{\text{circumference}}{\text{number of iterations}}$. Experiment with different numbers of increments as well to see how it changes your circle's appearance.
Recall the circumference of a circle is given by: $c = 2\pi\cdot r$.
Your end result should look as follows:
resetTurtle()
ttl.speed(10)
# 12 degrees = 360 degrees / 30 increments
arcDegree = 12
# Initial radius
r = 100
# Need to calculate the circumference of a circle
# of given radius, so we know how far to move each step
circum = math.pi * 2 * r
# How long should each step of our circle approximation be?
arcLength = circum / (360 / arcDegree)
# In each iteration, turn a few degrees and draw a line
# With enough iterations, this will create a circle
for i in range(30):
ttl.left(arcDegree)
ttl.forward(arcLength)
This circle isn't really a circle. It's an approximation of a circle. In the code above, we actually created a 30-sided figure.
As you increase the number of sides (i.e., decrease the arc degree), you'll get an increasingly better approximation of a circle. Likewise, decreasing the number of sides/increasing the degree gives you a worse approximation of a circle.
resetTurtle()
ttl.speed(10)
# A function for playing with the number of sides...
def drawCircle(ttl, r, sideCount):
# X degrees = 360 degrees / sideCount increments
arcDegree = 360 / sideCount
# Need to calculate the circumference of a circle
# of given radius, so we know how far to move each step
circum = math.pi * 2 * r
# How long should each step of our circle approximation be?
arcLength = circum / sideCount
# Each iteration really corresponds to drawing a side.
# With enough iterations, this will create a circle
for i in range(sideCount):
ttl.left(arcDegree)
ttl.forward(arcLength)
sideCount = 4
r = 100
drawCircle(ttl, r, sideCount)
We can use this property to see the increasing resolution of our approximations...
resetTurtle()
ttl.speed(10)
for sideCount in range(3, 20):
drawCircle(ttl, r, sideCount)
As in exercises 2 and 3, use the above circle-drawing code to draw a circular spiral, spiralling out from the center. To accomplish this task, after drawing half of your circle with your current radius, increment your radius by 10 and continue drawing. Start with a radius of 10 and continue for 10 iterations. Experiment with smaller and larger radii and radii increments.
End result should be similar to:
# Reset the screen
resetTurtle()
ttl.speed(10)
arcDegree = 12
r = 10
for i in range(10):
circum = math.pi * r
arcLength = circum / (180 / arcDegree)
for i in range(180//arcDegree):
ttl.left(arcDegree)
ttl.forward(arcLength)
r += 10
Alternately, you could use functions
# Reset the screen
resetTurtle()
ttl.speed(10)
# Encapsulate our circle drawing function
def drawHalfCircle(ttl, r, steps):
# We don't want to go the full 360 degrees, only half
arcDegree = 180 / steps
# How far must we travel?
halfCircum = math.pi * r
# And what's the arc length necessary to make semicircle of r?
arcLength = halfCircum / steps
# Now draw the steps
for i in range(steps):
ttl.left(arcDegree)
ttl.forward(arcLength)
# Initial radius
r = 10
# For each iteration, increment the radius after drawing the half circle
for i in range(10):
drawHalfCircle(ttl, r, 12)
r += 10
Try to reproduce the following images:
resetTurtle()
ttl.forward(100)
ttl.left(90)
ttl.forward(100)
angle = ttl.towards((0,0))
ttl.right(angle)