'''Program demonstrating a recursive image using turtles.''' import turtle # initial side length of the triangle SIDE_LEN = 250 # draw a fractal of this order FRACTAL_ORDER = 4 def draw_side(size, order): '''Draw a koch fractal with the given side length and fractal order.''' if order == 0: # base case: an order 0 fractal is a straight line turtle.forward(size) else: # recursive case: # draw four different (order - 1) fractals next_size = size / 3 next_order = order - 1 draw_side(next_size, next_order) turtle.left(60) draw_side(next_size, next_order) turtle.right(120) draw_side(next_size, next_order) turtle.left(60) draw_side(next_size, next_order) def main(): '''Draw an order FRACTAL_ORDER koch fractal.''' turtle.speed('fastest') turtle.up() turtle.goto(-SIDE_LEN/2, SIDE_LEN/2) # center the triangle turtle.down() for i in range(3): # draw three sides of the triangle draw_side(SIDE_LEN, FRACTAL_ORDER) turtle.right(120) main()