Hey I'm very new to the programming world, and what i am trying to do is produce a Koch Snowflake via turtle from python2.5.
this is what I have so far...

from turtle import *

def snowflake(lengthSide, levels):
    if levels == 0:
        return forward(lengthSide), right(120), forward(lengthSide), right(120), forward(lengthSide)
    
    forward(lengthSide)
    left(60)
    forward(lengthSide)
    right(60)
    right(60)
    forward(lengthSide)
    left(60)
    forward(lengthSide)
    right(120)
    snowflake(lengthSide, levels)

as you can see i've only solved for levels 0 and 1... the problem is i dont understand how to recursively redraw it from a certain location. If anyone has any ideas or tip, it would be helpful.

I suggest this

from turtle import *

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


if __name__ == "__main__":
    speed(0)
    length = 300.0
    penup()
    backward(length/2.0)
    pendown()
    snowflake(length, 4)

Thank you very much, this helped alot!

how do you modify the code so that it can make a full snowflake?

how do you modify the code so that it can make a full snowflake?

Simply define

def full_snowflake(lengthside, levels):
    for i in range(3):
        snowflake(lengthside, levels)
        right(120)

And put at the end of program:

mainloop()

where do you put the code in the program? I've tried to put it at the end but it doesn't work.

from turtle import *

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

def full_snowflake(lengthside, levels):
    for i in range(3):
        snowflake(lengthside, levels)
        right(120)

if __name__ == "__main__":
    speed(0)
    length = 300.0
    penup()
    backward(length/2.0)
    pendown()
    full_snowflake(length, 4)
    mainloop()

Here is a snowflakes containing printable characters

from turtle import *
import random, string

fact = (2.0/(3.0*sqrt(3.0)), sqrt(3.0)/2.0, 0.5)

def writers(cx, cy, lengthside, levels):
    if levels == 0:
        penup()
        goto(cx, cy)
        pendown()
        write(random.choice(string.printable))
        return
    r = lengthside * fact[0]
    a = r * fact[1]
    b = r * fact[2]
    centers = [(cx, cy), (cx, cy+r), (cx, cy-r), (cx+a, cy+b), (cx+a, cy-b), (cx-a, cy+b), (cx-a, cy-b)]
    for ux, uy in centers:
        writers(ux, uy, lengthside/3.0, levels-1)

if __name__ == "__main__":
    speed(0)
    length = 500.0
    writers(0.0, 0.0, length, 4)
    mainloop()

Thank you. That really helped!!

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.