{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# INST326 In-Class Exercises\n",
"## 20170608, Turtles Revisited\n",
"\n",
"These exercises will let you explore the `turtle` package. Remember, if you have questions about turtle capabilities, check out the documentation here:\n",
"\n",
"__[Turtle Documentation](https://docs.python.org/3.6/library/turtle.html)__\n",
"\n",
"I've also provided some setup and utility functions to help you get started."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Import the turtle package, so we can draw\n",
"import turtle\n",
"\n",
"# Need math for math.pi\n",
"import math"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Create a turtle\n",
"ttl = turtle.Turtle()\n",
"\n",
"# Create a screen on which we can play\n",
"screen = turtle.Screen()\n",
"\n",
"# Configure the playing field\n",
"dimX = 400\n",
"dimY = 400\n",
"\n",
"# I have a fudge factor here to account for windows being too small\n",
"fudgeFactor = 100\n",
"screen.setup(dimX+fudgeFactor, dimY+fudgeFactor) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Call the following `reset()` function whenever you want to start your turtle instance over."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def resetTurtle():\n",
" \"\"\"Call this function to reset your screen as necessary\"\"\"\n",
" # Reset the screen\n",
" turtle.resetscreen()\n",
" screen.reset()\n",
" screen.screensize(dimX, dimY)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 1. Draw a 100x100 Square\n",
"\n",
"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:\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"resetTurtle()\n",
"\n",
"# Draw four sides, each of length 100, and turn 90 degrees between each line\n",
"for i in range(4):\n",
" ttl.forward(100)\n",
" ttl.left(90)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2. Draw a Square-based Spiral\n",
"\n",
"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.\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"resetTurtle()\n",
"\n",
"dist = 10\n",
"\n",
"# For fifty iterations...\n",
"for i in range(50):\n",
" \n",
" # Every other iteration (i.e., every even iteration), increase your distance\n",
" # This step ensures we draw lines of increasing size to get the nice\n",
" # spiral effect\n",
" if i % 2 == 0:\n",
" dist += 10\n",
" \n",
" # Draw a line of length dist and turn\n",
" ttl.forward(dist)\n",
" ttl.left(90)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 3. Draw a Rotated Square-based Spiral\n",
"\n",
"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.\n",
"\n",
"Your output should look like the following:\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"resetTurtle()\n",
"\n",
"dist = 10\n",
"\n",
"# Again, fifty iterations...\n",
"for i in range(50):\n",
" \n",
" # Increment every other side's length\n",
" if i % 2 == 0:\n",
" dist += 10\n",
" \n",
" # This time, turn 92 degrees, just off from 90.\n",
" # Over several iterations, it creates a cool effect\n",
" ttl.forward(dist)\n",
" ttl.left(92)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 4. Draw a Circle of Radius 100\n",
"\n",
"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.\n",
"\n",
"Recall the circumference of a circle is given by: $c = 2\\pi\\cdot r$.\n",
"\n",
"Your end result should look as follows:\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"resetTurtle()\n",
"\n",
"ttl.speed(10)\n",
"\n",
"# 12 degrees = 360 degrees / 30 increments\n",
"arcDegree = 12\n",
"\n",
"# Initial radius\n",
"r = 100\n",
"\n",
"# Need to calculate the circumference of a circle\n",
"# of given radius, so we know how far to move each step\n",
"circum = math.pi * 2 * r\n",
"\n",
"# How long should each step of our circle approximation be?\n",
"arcLength = circum / (360 / arcDegree)\n",
"\n",
"# In each iteration, turn a few degrees and draw a line\n",
"# With enough iterations, this will create a circle\n",
"for i in range(30):\n",
"\n",
" ttl.left(arcDegree)\n",
" ttl.forward(arcLength)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Playing with Approximations\n",
"\n",
"This circle isn't really a circle. It's an approximation of a circle. \n",
"In the code above, we actually created a 30-sided figure.\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"resetTurtle()\n",
"\n",
"ttl.speed(10)\n",
"\n",
"# A function for playing with the number of sides...\n",
"def drawCircle(ttl, r, sideCount):\n",
" \n",
" # X degrees = 360 degrees / sideCount increments\n",
" arcDegree = 360 / sideCount\n",
"\n",
" # Need to calculate the circumference of a circle\n",
" # of given radius, so we know how far to move each step\n",
" circum = math.pi * 2 * r\n",
"\n",
" # How long should each step of our circle approximation be?\n",
" arcLength = circum / sideCount\n",
"\n",
" # Each iteration really corresponds to drawing a side.\n",
" # With enough iterations, this will create a circle\n",
" for i in range(sideCount):\n",
"\n",
" ttl.left(arcDegree)\n",
" ttl.forward(arcLength)\n",
"\n",
"sideCount = 4\n",
"r = 100\n",
"drawCircle(ttl, r, sideCount)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nesting\n",
"\n",
"We can use this property to see the increasing resolution of our approximations..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"resetTurtle()\n",
"\n",
"ttl.speed(10)\n",
"\n",
"for sideCount in range(3, 20):\n",
" drawCircle(ttl, r, sideCount)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 5. Draw a Circular Spiral\n",
"\n",
"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.\n",
"\n",
"End result should be similar to:\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Reset the screen\n",
"resetTurtle()\n",
"ttl.speed(10)\n",
"arcDegree = 12\n",
"r = 10\n",
"\n",
"for i in range(10):\n",
"\n",
" circum = math.pi * r\n",
" arcLength = circum / (180 / arcDegree)\n",
"\n",
" for i in range(180//arcDegree):\n",
"\n",
" ttl.left(arcDegree)\n",
" ttl.forward(arcLength)\n",
" \n",
" r += 10\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternately, you could use functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Reset the screen\n",
"resetTurtle()\n",
"ttl.speed(10)\n",
"\n",
"# Encapsulate our circle drawing function\n",
"def drawHalfCircle(ttl, r, steps):\n",
" \n",
" # We don't want to go the full 360 degrees, only half\n",
" arcDegree = 180 / steps\n",
"\n",
" # How far must we travel?\n",
" halfCircum = math.pi * r\n",
" \n",
" # And what's the arc length necessary to make semicircle of r?\n",
" arcLength = halfCircum / steps\n",
"\n",
" # Now draw the steps\n",
" for i in range(steps):\n",
" ttl.left(arcDegree)\n",
" ttl.forward(arcLength)\n",
" \n",
"# Initial radius\n",
"r = 10\n",
"\n",
"# For each iteration, increment the radius after drawing the half circle\n",
"for i in range(10):\n",
" drawHalfCircle(ttl, r, 12)\n",
" r += 10\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Extra Credit\n",
"\n",
"Try to reproduce the following images:\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"resetTurtle()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ttl.forward(100)\n",
"ttl.left(90)\n",
"ttl.forward(100)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"angle = ttl.towards((0,0))\n",
"ttl.right(angle)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}