Math 450 Laboratory 2: Python's turtles and geometric models

Date: 2013-01-27


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

Goals for this laboratory...

Turtle graphics and the LOGO language

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.

Drawing a regular polygon

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...

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

draw_square.py


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? ")

The turtle-based spirograph

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? ')

Fractals with turtles

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.")

Assignment

  1. Now, write your own program to draw a 5-pointed star.
  2. Use python's turtle module to draw pictures of 4 different 11-pointed stars.
  3. 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.
  4. Use python's turtle module to draw your own unique fractal.