Skip to content

Instantly share code, notes, and snippets.

@IanMcT
Created November 8, 2016 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanMcT/df6a58e5cc962c9de201a3df5b39e3a7 to your computer and use it in GitHub Desktop.
Save IanMcT/df6a58e5cc962c9de201a3df5b39e3a7 to your computer and use it in GitHub Desktop.
Koch fractals
import turtle
def koch(t, order, size):
"""
Make turtle t draw a Koch fractal of 'order' and 'size'.
Leave the turtle facing the same direction.
"""
if order == 0: # The base case is just a straight line
t.forward(size)
else:
koch(t, order-1, size/3) # Go 1/3 of the way
t.left(60)
koch(t, order-1, size/3)
t.right(120)
koch(t, order-1, size/3)
t.left(60)
koch(t, order-1, size/3)
wn = turtle.Screen() # creates a graphics window
alex = turtle.Turtle() # create a turtle named alex
koch(alex,4,300)
wn.exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment