Thursday, October 7, 2010

Drawing the Koch Curve in Turtle


I love this fractal because it's so simple! I barely need to obfuscate the code and its definition still fits in 3 fairly neat lines. It's simple enough that I won't even bother giving an explanation so as not to insult your intelligence. If you're having trouble understanding the t.forward line, I explain that construction in my explanation for the Dragon Curve Fractal in Turtle (my first post).

import turtle
t = turtle.Pen()

def koch(dist):
    for angle in (60, -120, 60, 0):
        (t.forward if dist <= 10 else koch)(dist / 3.0)  # 10 is the break case. 3.0 is the line:subline ratio
        t.left(angle)

koch(400)

No comments:

Post a Comment