############################################################################### # turtlelab.py: Programming lab for turtle fractal trees. # # CTY Lancaster 2012 Session I, DATA # # Author: Jon Brandvein # # Modified: 06/28/12 # ############################################################################### ############################################################################### # YOUR NAME: ############################################################################### from turtle import * from random import random, randint # The function random() returns a random value from the range [0, 1). # The function randint(lo, hi) returns a random integer from the range # [lo, hi]. t = Turtle() # Uncomment to have colors take the range [0, 255) instead of [0, 1) #colormode(255) # Change the speed. Higher is faster. 0 is as fast as possible. t.speed(5) # Skip steps to draw even faster. 1 is normal. #tracer(10) # drawpolygon() and drawtriangle() functions, from class. def drawpolygon(n, side_length): extangle = float(360)/n for i in range(n): t.forward(side_length) t.left(extangle) def drawtriangle(side_length): drawpolygon(3, side_length) # This draws the simplified fern we had in class. def drawfern(size): # Return if we're too small to be noticeable. if size < 5: return # The sub-fern sizes are divided by this factor. # It should be greater than 1. (Why?) factor = 2 # One quarter of the stem size, rounded down quarter_size = size/4 # The exact amount remaining after going two quarter sizes, # accounting for rounding error. remaining_size = size - (2 * quarter_size) # Draw the stem. t.forward(size) # Back up to draw the right leaf. t.backward(quarter_size) t.right(90) # Draw the right leaf. drawfern(size / factor) # Head back to draw the left leaf. t.right(90) t.forward(quarter_size) t.right(90) # Draw the left leaf. drawfern(size / factor) # Go back to where we started and correct our orientation. t.left(90) t.forward(remaining_size) t.right(180) def drawtree(size): if size <= 20: return t.color('red') t.forward(size) t.left(30) drawtree(size / 3 * 2) t.right(60) drawtree(size / 3 * 2) t.left(30) t.color('blue') t.backward(size) # Code that is not in a function is executed when the program starts, # like main(). # Setup initial position. t.left(90) t.penup() t.backward(200) t.pendown() drawtree(150) #drawfern(300) mainloop()