#!/usr/bin/env python """ draw a Koch curve with the Turtle """ from turtle import Turtle def koch(length, depth, max_depth, turtle): """ Recursively draw the Koch curve. """ if depth >= max_depth: turtle.move(length) else: koch(length/3., depth+1, max_depth, turtle) turtle.turn(60) koch(length/3., depth+1, max_depth, turtle) turtle.turn(-120) koch(length/3., depth+1, max_depth, turtle) turtle.turn(60) koch(length/3., depth+1, max_depth, turtle) size_across = 1000 size_high = 350 starting_length = size_across - 100 starting_depth = 0 depth_limits = (0, 6) prompt = "Koch curve from depth=0 to ? (%i..%i) " % depth_limits max_depth = input(prompt) if max_depth < depth_limits[0]: max_depth = depth_limits[0] if max_depth > depth_limits[1]: max_depth = depth_limits[1] t = Turtle(width=size_across, height=size_high) t.x = -size_across/2 + 50 t.y = -size_high/2 + 50 koch(starting_length, starting_depth, max_depth, t) wait = raw_input("Hit return to quit.")