Time to think snow (Python)

vegaseat 2 Tallied Votes 289 Views Share

Using Python module turtle to draw a Koch snow fractal.

''' turtle_KochSnowFlake_full.py
use Python's module turtle to draw a Koch snowflake
tested with Python27 and Python33  by  vegseat  16oct2013
'''

import turtle as tu

def snowflake(side, levels):
    if levels == 0:
        tu.forward(side)
        return
    side /= 3.0
    snowflake(side, levels-1)
    tu.left(60)
    snowflake(side, levels-1)
    tu.right(120)
    snowflake(side, levels-1)
    tu.left(60)
    snowflake(side, levels-1)

def full_snowflake(side, levels):
    for k in range(3):
        snowflake(side, levels)
        tu.right(120)


if __name__ == "__main__":
    tu.title("Drawing a Koch snowflake")
    tu.bgcolor('blue')
    # values for speed are 'fastest' (no delay), 'fast', (delay 5ms),
    # 'normal' (delay 10ms), 'slow' (delay 15ms), and 'slowest' (delay 20ms)
    tu.speed('fastest')
    length = 300.0
    tu.penup()
    # start drawing here to center the drawing
    tu.goto(-length//2, length//2)
    tu.pendown()
    tu.color('white')
    tu.begin_fill()    
    full_snowflake(length, 4)
    tu.end_fill()
    tu.mainloop()
TheCodeCrimson 0 Newbie Poster

Simple and elegant. Nicely done!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.