"""File: koch_curve.py Purpose: Use TurtleWorld to draw a Koch curve Run: python koch_curve.py Input: The ``length'' of the curve. Output: A TurtleWorld window with a Koch curve. Note: This is a slightly modified version of the solution to the Koch curve exercise in Think Python: An Introduction to Software Design, Allen B. Downey Note: The ``stretch'' argument can be used to enlarge the curve when n is small. """ from TurtleWorld import * def koch(t, n, stretch): """Draw a Koch curve having "length" n with turtle t stretch is used to increase the actual length of the curve when n is small""" if n < 3: fd(t, stretch) return m = n/3.0 koch(t, m, stretch/3.0) lt(t, 60) koch(t, m, stretch/3.0) rt(t, 120) koch(t, m, stretch/3.0) lt(t, 60) koch(t, m, stretch/3.0) def to_lower_left_corner(t): """Move t to the lower left corner of the window without drawing""" pu(t) rt(t) fd(t,200) rt(t) fd(t,200) rt(t) rt(t) pd(t) #---------------------------------------------------------------------- # Main program world = TurtleWorld() bob = Turtle() bob.delay = 0 to_lower_left_corner(bob) n = int(raw_input("How long should the curve be?\n ")) koch(bob, n, 400) wait_for_user()