Week 12, Monday: More Classes

Let's draw some pretty pictures today!

test turtle

koch curve

spirograph

the ZTurtle class

Each of the above pictures is easier to draw if you think of a pen on a piece of paper, and you give it commands such as: move forward, turn left, move forward some more, turn right, and so on.

Historically, this mode of graphics is called "Turtle Graphics", as the pen is replaced with a turtle walking around on a sandy beach, dragging it's tail in the sand. By telling the turtle to "move forward", it creates a line in the sand. You can even tell the turtle to lift it's tail up while moving, if you don't want to create a line.

Today we are stilreplaced with a turtle walking around on a sandy beach, dragging it's tail in the sand. By telling the turtle to "move forward", it creates a line in the sand. You can even tell the turtle to lift it's tail up while moving, if you don't want to create a line.

Today we are still going to use Zelle graphics, but we will create a ZTurtle class that acts like the "Turtle Graphics" described above.

Here is the starting code for the ZTurtle class. I have already written the forward() method, as that involves a little trigonometry. The rest is up to you. Write a method and then test it out. Once you have all methods written, try running the testturtle.py program to make sure it all works.

"""
turtle class using zelle graphics

Fall 2015
"""

from graphics import *
from math import *

###############################################################

class ZTurtle(object):
  """Zelle Graphics Turtle"""

  # you need to write:
  #
  # __init__    construct new turtle, given x,y coords and graphics win
  #
  #             hint...use these instance variables:
  #              self.x          current x position
  #              self.y          current y position
  #              self.heading    current heading (0 means East,90 North)
  #              self.tailup     True if tail is up, False if down
  #              self.window     the graphics window for drawing
  #              self.color      color used for drawing
  #
  # __str__     return string with x,y,heading, and tail up or down
  # setColor    change color of pen
  # setHeading  set turtle direction
  # turn        alter turtle direction by certain angle
  # down        put tail down
  # up          lift tail up
  # moveto      magically move turtle to location x,y (no drawing)
  # dot         drop a visible marker at current location


  def forward(self, ds):
    """move forward a distance ds, draw if tail is down"""
    currpt = Point(self.x, self.y)
    theta = radians(self.heading)
    dx = ds*cos(theta)
    dy = ds*sin(theta)
    nx = self.x + dx
    ny = self.y + dy
    newp = Point(nx, ny)
    if not self.tailup:
      L = Line(currpt, newp)
      L.draw(self.window)
      L.setFill(self.color)
    self.x = nx
    self.y = ny

###############################################################

if __name__ == "__main__":

  gw = GraphWin("zturtle test", 500, 500)
  gw.setBackground("gray")
  t = ZTurtle(100,100,gw)
  print(t)
  t.down()
  t.forward(100)
  gw.getMouse()

the Koch curve

Once you have a working ZTurtle class, see if you can get koch.py to work. This is a recursive graphics program that either moves forward a given amount, or calculates a smaller (1/3rd) length and recurs. Here are the first few Koch curves for low values of n. Can you see the pattern?

koch curves