Fall2006.CSCI199Homework2Template History

Hide minor edits - Show changes to output

Changed line 4 from:
* Assignment: Homework 1 \\
to:
* Assignment: Homework 2 \\
Deleted lines 13-134:

(:source lang=Python tabwidth=3 -trim :)

# l-system.tree.py Version 1.0 23-sep-06 (bzm)
#
# Demonstrates how to draw a fractal tree using L-systems.
#

from lsystem import * # for recursive system modeling
from xturtle import * # for drawing
from stack import * # for storing things

# define the axiom and rules
model = LSystem('A',
[
('A', 'FF[-A][+A]FA'),
('F', 'FF')
])

model.depth = 8 # number of times to apply rules
model.length = 0.5 # length of first branch
model.angle = 90 # initial orientation (12 o'clock)
model.tilt = 180.0/7 # angle change between main branch and sub-branches
model.origin = (-135, -192) # starting position (root)
model.updateFrequency = 20 # how often to update screen (set to 'False' for fastest)

#print repr(model)

def interpet(model):
"Defines the meaning of each production symbol."

turtle = Pen()
stack = Stack()

# setup drawing model
initializeTurtle(turtle, model)

# define rules
def F():
"Draw one length in current orientation"
turtle.down()
turtle.forward(model.length)
#print "down, forward", model.length

def f():
"Move (no draw) one length in current orientation."
turtle.up()
turtle.forward(model.length)
#print "up, forward", model.length

def push():
"Save current position, heading, length."
stack.push( (turtle.pos(), turtle.heading(), model.length) )
#print "push, new length", model.length, "heading", turtle.heading()

def pop():
"Restore saved position, heading, length."
position, model.angle, model.length = stack.pop()
turtle.up()
turtle.goto(position)
turtle.setheading(model.angle)
#print "pop, new length", model.length, "heading", turtle.heading()

def left():
"Tilt to the left."
turtle.left(model.tilt)
#print "turn left, new orientation", model.angle

def right():
"Tilt to the right."
turtle.right(model.tilt)
#print "turn right, new orientation", turtle.heading()

def A():
"Do nothing."
#print "do nothing"

def error():
"Unrecognized symbol."
raise ValueError(symbol) # let them know

# create a dictionary of symbol meanings
dictionary = {}
dictionary['F'] = F
dictionary['f'] = f
dictionary['['] = push
dictionary[']'] = pop
dictionary['-'] = left
dictionary['+'] = right
dictionary['A'] = A

# interpet symbols
for symbol in model.string:
#print symbol
dictionary.get(symbol, error)()

def initializeTurtle(turtle, model):
"Sets up turtle graphics for drawing."

turtle.speed(0)
turtle.hideturtle()
turtle.tracer(model.updateFrequency)
turtle.up()
turtle.goto(model.origin)
turtle.left(model.angle)
turtle.down()

def main():

# generate productions
while not model.done and model.generation < model.depth:
#print model.generation, model.string
model.step()

# interpret latest production
#print "Current directions", model.string
interpet(model)

if __name__ == '__main__':
main()

(:sourcend:)
Added lines 1-135:
/**
* Author: {Your Name} \\
* Course: CSCI 199, Section 03 \\
* Assignment: Homework 1 \\
* Due Date: {The assignment's due date} \\
* \\
* Certification of Authenticity {include one of the following}: \\
* \\
* I certify that this work is entirely my own. \\
* \\
* I certify that this work is my own, but I received \\
* some assistance from: {Name(s), References, etc.} \\
*/

(:source lang=Python tabwidth=3 -trim :)

# l-system.tree.py Version 1.0 23-sep-06 (bzm)
#
# Demonstrates how to draw a fractal tree using L-systems.
#

from lsystem import * # for recursive system modeling
from xturtle import * # for drawing
from stack import * # for storing things

# define the axiom and rules
model = LSystem('A',
[
('A', 'FF[-A][+A]FA'),
('F', 'FF')
])

model.depth = 8 # number of times to apply rules
model.length = 0.5 # length of first branch
model.angle = 90 # initial orientation (12 o'clock)
model.tilt = 180.0/7 # angle change between main branch and sub-branches
model.origin = (-135, -192) # starting position (root)
model.updateFrequency = 20 # how often to update screen (set to 'False' for fastest)

#print repr(model)

def interpet(model):
"Defines the meaning of each production symbol."

turtle = Pen()
stack = Stack()

# setup drawing model
initializeTurtle(turtle, model)

# define rules
def F():
"Draw one length in current orientation"
turtle.down()
turtle.forward(model.length)
#print "down, forward", model.length

def f():
"Move (no draw) one length in current orientation."
turtle.up()
turtle.forward(model.length)
#print "up, forward", model.length

def push():
"Save current position, heading, length."
stack.push( (turtle.pos(), turtle.heading(), model.length) )
#print "push, new length", model.length, "heading", turtle.heading()

def pop():
"Restore saved position, heading, length."
position, model.angle, model.length = stack.pop()
turtle.up()
turtle.goto(position)
turtle.setheading(model.angle)
#print "pop, new length", model.length, "heading", turtle.heading()

def left():
"Tilt to the left."
turtle.left(model.tilt)
#print "turn left, new orientation", model.angle

def right():
"Tilt to the right."
turtle.right(model.tilt)
#print "turn right, new orientation", turtle.heading()

def A():
"Do nothing."
#print "do nothing"

def error():
"Unrecognized symbol."
raise ValueError(symbol) # let them know

# create a dictionary of symbol meanings
dictionary = {}
dictionary['F'] = F
dictionary['f'] = f
dictionary['['] = push
dictionary[']'] = pop
dictionary['-'] = left
dictionary['+'] = right
dictionary['A'] = A

# interpet symbols
for symbol in model.string:
#print symbol
dictionary.get(symbol, error)()

def initializeTurtle(turtle, model):
"Sets up turtle graphics for drawing."

turtle.speed(0)
turtle.hideturtle()
turtle.tracer(model.updateFrequency)
turtle.up()
turtle.goto(model.origin)
turtle.left(model.angle)
turtle.down()

def main():

# generate productions
while not model.done and model.generation < model.depth:
#print model.generation, model.string
model.step()

# interpret latest production
#print "Current directions", model.string
interpet(model)

if __name__ == '__main__':
main()

(:sourcend:)