#176 The Koch snowflake using python turtle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import turtle # Set up Constants ANGLES = [60, -120, 60, 0] SIZE_OF_SNOWFLAKE = 300 def get_input_depth(): depth = 9 return depth def setup_screen(title, background='white', screen_size_x=640, screen_size_y=320, tracer_size=800): print('Set up Screen') turtle.title(title) turtle.setup(screen_size_x, screen_size_y) turtle.hideturtle() turtle.penup() turtle.backward(240) # Batch drawing to the screen for faster rendering turtle.tracer(tracer_size) turtle.bgcolor(background) # Set the background colour of the screen def draw_koch(size, depth): if depth > 0: for angle in ANGLES: draw_koch(size / 3, depth - 1) turtle.left(angle) else: turtle.forward(size) depth = get_input_depth() setup_screen('Koch Snowflake (depth ' + str(depth) + ')', background='white', screen_size_x=420, screen_size_y=420) # Set foreground colours turtle.color('red') # Ensure snowflake is centred turtle.penup() turtle.setposition(-180,0) turtle.left(30) turtle.pendown() # Draw three sides of snowflake for _ in range(3): draw_koch(SIZE_OF_SNOWFLAKE, depth) turtle.right(120) # Ensure that all the drawing is rendered turtle.update() print('Done') turtle.done() |
Recent Comments