Skip to content

Instantly share code, notes, and snippets.

@Ivoah
Created March 25, 2016 15:23
Show Gist options
  • Save Ivoah/7fc4173940eca415e352 to your computer and use it in GitHub Desktop.
Save Ivoah/7fc4173940eca415e352 to your computer and use it in GitHub Desktop.
import turtle, sys, math
def dragon_curve(t, n, l):
sqrt2over2 = math.sqrt(2)/2
i = [1 for i in range(n)]
def r(n, l):
if n == 0:
t.forward(l)
else:
_ = 1
if i[n-1]%2 == 0: _ = -1
t.left(_*45)
r(n - 1, l*sqrt2over2)
t.right(_*90)
r(n - 1, l*sqrt2over2)
t.left(_*45)
i[n-1] += 1
r(n, l)
def koch_curve(t, n, l):
def r(n, l):
if n == 0:
t.forward(l)
else:
r(n - 1, l/3)
t.left(60)
r(n - 1, l/3)
t.right(120)
r(n - 1, l/3)
t.left(60)
r(n - 1, l/3)
r(n, l)
def sierp_tri(t, n, l):
i = [0 if i%2 == 0 else 1 for i in range(n)]
def r(n, l):
if n == 0:
t.forward(l)
else:
_ = 1
if i[n-1]%2 == 0: _ = -1
t.left(_*60)
r(n - 1, l/2)
t.right(_*60)
r(n - 1, l/2)
t.right(_*60)
r(n - 1, l/2)
t.left(_*60)
i[n-1] += 1
r(n, l)
t = turtle.Turtle()
t.speed(100000)
n = int(sys.argv[1])
#for i in range(1, int(n + 1)):
# t.home()
# t.color(1 - i/n, 1 - i/n, 1 - i/n)
t.penup()
t.setx(-250/2)
t.pendown()
dragon_curve(t, n, 250)
turtle.done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment