Date: 2013-01-27

Things you should know how to do before starting this lab...

- How to use a command line interface.
- How to run the python interpretter.
- How to create and run a python script.
- How to find documentation on functions and modules.

Goals for this laboratory...

- To learn how to draw geometric shapes with the turtle
- To draw figures with rotational symmetry.

The LOGO computer langauge was used to teach computer programming on Apple IIe's in the 1980's. In sharp contrast to the built-in BASIC language, LOGO was based on the drawing computer graphics using a simulated automata called a "turtle". Basically, you tell the turtle how to move, and it drew things on the screen for you, sort-of like a sup'd up etch-a-sketch. Some variants of the LOGO are around, including StarLogo, which is still actively use for education and computational modelling projects.

The computational geometry concepts introduced by LOGO have been built into
python in a module called `turtle`

. Hint: You can use the up arrow in the
interpretter to recall previously entered commands.

```
>>> from turtle import *
>>>
>>> forward(50)
>>> right(90)
>>> forward(50)
>>> right(90)
>>> forward(50)
>>> right(90)
>>> forward(50)
>>> right(90)
```

The first line here "import"'s all the functions from the turtle module that we want to use. We need to use it at the start of any script or interpretter session where we want to use turtles.

These functions to command the turtle usually have 2-letter abbreviations. Here are some more...

- fd() == forward
- bk() == backward
- rt() == right
- lt() == left
- pu() == penup
- pd() == pendown
- ht() == hideturtle
- st() == showturtle
- clear() == clear screen
- home() == go straight home to the origin

A method with fewer lines of code. Try saving the following to a script and running the script.

```
from turtle import *
def printscreen(filename):
scr = getscreen()
scr.getcanvas().postscript(file=filename+'.eps')
n, x = 3, 50;
for i in range(n):
fd(x)
lt(360./n)
printscreen('triangle')
raw_input("Can I quit now? ")
```

Then, try the following script out...

```
from turtle import *
import random
def get_randcolor():
r = random.randint
t = tuple([tuple([ r(0,255) for i in [1,2,3]]) for j in [1,2]])
return t
def g():
x = random.randint(20,50)
z = 250
p,q = random.gauss(0,z/2),random.gauss(0,z/2)
goto(p,q)
pd()
r = random.randint(3,9)
s = float(random.randint(1,r-1)*(random.randint(0,1)*2-1))
t = (180 + s*180./r)
rt(random.randint(1,360))
for i in range(r*2):
fd(x)
rt(t)
pu()
home()
return
speed(0)
colormode(255)
pu()
ht()
for i in range(30):
color(get_randcolor()[0])
g()
raw_input('Done? ')
```

Study the following program to see how it creates a fractal-like drawing...

```
import turtle
def Sierpinski3(size = 400, xmin = 4 ):
turtle.reset()
turtle.forward(size/2)
turtle.right(180)
turtle.clear()
def A(x):
if xmin > x:
turtle.forward(x)
return
B(x/2)
turtle.left(60)
A(x/2)
turtle.left(60)
B(x/2)
def B(x):
if xmin > x:
turtle.forward(x)
return
A(x/2)
turtle.left(-60)
B(x/2)
turtle.left(-60)
A(x/2)
A(size)
turtle.tracer(100,0)
Sierpinski3(size=300,xmin=5)
turtle.ht()
raw_input("Hit return to close.")
```

- Now, write your own program to draw a 5-pointed star.
- Use python's turtle module to draw pictures of 4 different 11-pointed stars.
- Use python's turtle module to draw your own unique rotationally symmetric design that has no reflection symmetries. Be sure to identify it's symmetry.
- Use python's turtle module to draw your own unique fractal.