# file: problemset5.py # # author: Bob Muller # date: October 23, 2007 # # This file contains a few simple python functions that use the # turtle graphics module to solve some simple problems. import turtle # This is simply a setup routine that calls turtle.setup # def setup(w, h): turtle.setup(width=w, height=h) turtle.degrees() turtle.color(.25, .5, .5) turtle.width(15) # rightTriangle is problem 5.1. # def rightTriangle(x, y, width, height): turtle.up() turtle.goto(x, y) turtle.down() turtle.forward(width) turtle.backward(width) turtle.left(90) turtle.forward(height) turtle.goto(x+width, y) # rectangle is a simple utility routine that draws # a rectangle at Cartesian coordinates (x,y). # def rectangle(x, y, width, height): turtle.up() turtle.goto(x, y) turtle.down() turtle.goto(x + width, y) turtle.goto(x + width, y + height) turtle.goto(x, y + height) turtle.goto(x, y) # cube is problem 5.2. NB that it is a little funky # in the way it treats depth. # def cube(x, y, width, height, depth): rectangle(x, y, width, height) rectangle(x+depth, y+depth, width, height) turtle.goto(x, y) turtle.goto(x, y+height) turtle.goto(x+depth, y+height+depth) turtle.goto(x+depth+width, y+height+depth) turtle.goto(x+width, y+height) turtle.goto(x+width, y) turtle.goto(x+width+depth, y+depth) # starOfDavid is problem 5.3. # def startOfDavid(x, y, side): turtle.up() turtle.goto(x,y) turtle.down() turtle.forward(side) turtle.right(120) turtle.forward(side) turtle.right(120) turtle.forward(side) turtle.up() turtle.goto(x + side / 2, y + side / 4.0) turtle.down() turtle.backward(side) turtle.forward(side) turtle.left(120) print turtle.heading() turtle.forward(side) turtle.right(240) turtle.forward(side)