# INST326 In-Class Exercises¶

## 20170608, Turtles Revisited¶

These exercises will let you explore the turtle package. Remember, if you have questions about turtle capabilities, check out the documentation here:

Turtle Documentation

I've also provided some setup and utility functions to help you get started.

In [1]:
# Import the turtle package, so we can draw
import turtle

# Need math for math.pi
import math

In [2]:
# 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.

In [3]:
def resetTurtle():
"""Call this function to reset your screen as necessary"""
# Reset the screen
turtle.resetscreen()
screen.reset()
screen.screensize(dimX, dimY)


## Exercise 1. Draw a 100x100 Square¶

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:

In [4]:
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)


## Exercise 2. Draw a Square-based Spiral¶

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.

In [ ]:
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)



## Exercise 3. Draw a Rotated Square-based Spiral¶

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:

In [ ]:
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)


## Exercise 4. Draw a Circle of Radius 100¶

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:

In [ ]:
resetTurtle()

ttl.speed(10)

# 12 degrees = 360 degrees / 30 increments
arcDegree = 12

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)


## Playing with Approximations¶

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.

In [ ]:
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)


## Nesting¶

We can use this property to see the increasing resolution of our approximations...

In [ ]:
resetTurtle()

ttl.speed(10)

for sideCount in range(3, 20):
drawCircle(ttl, r, sideCount)


## Exercise 5. Draw a Circular Spiral¶

End result should be similar to:

In [ ]:
# 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

In [ ]:
# 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)

r = 10

# For each iteration, increment the radius after drawing the half circle
for i in range(10):
drawHalfCircle(ttl, r, 12)
r += 10


## Extra Credit¶

Try to reproduce the following images:

In [11]:
resetTurtle()

In [12]:
ttl.forward(100)
ttl.left(90)
ttl.forward(100)

In [13]:
angle = ttl.towards((0,0))
ttl.right(angle)

In [ ]: