Hi friends,
I am trying to write a program for drawing a Koch curve in Python. The Pseduocode is given as follows and the hint is to use recursion:


To draw and Koch curve with length 'x' all you have to do is:
1. Draw Koch curve with length x/3
2. Turn left 60degrees.
3. Draw Koch curve with length x/3
4. Turn right 120 degrees.
5. Draw Koch curve with length x/3.
6. Turn left 60 degrees.
7. Draw Koch curve with length x/3.
The only exception is that if x is less than 2. In that case, you can just draw a straight line with length x.

I am using the TurtleWorld module(www.allendowny.com/swampy) which has been imported into my project.
The following is the Python Script I have written to generate the curve:

from TurtleWorld import *
TurtleWorld()

def Koch(t,length):
    t=Turtle()
    t.delay=0.01
    if length<=2 :
        fd(t,length)
        return
    Koch(t,length/3)
    lt(t,60)
    Koch(t,length/3)
    rt(t,120)
    Koch(t,length/3)
    lt(t,60)
    Koch(t,length/3)
    wait_for_user()

Koch("bob",540)

The curve is simply not drawn. I tried to dry run the code but I found no mistakes.
Can you friends please help me out ?

Thank You.
PS: The example is the Exercise5.2 from section 5.14 taken from the book "Think like a Computer Scientist"

Just a guess; I don't know Turtles, but it seems to me you should move the t=turtle() line from inside Koch() and do it separately. Otherwise you get a new turtle() instance every time you re-enter Koch().

Replace

Koch("bob",540)

with

t=turtle() # And remove from inside Koch()
t.delay=0.01 # And remove from inside Koch()
Koch(t, 540)

, although I don't know what "bob" would think of that.

Hi,
Thank You for the kind reply. It was a silly mistake which somehow I couldn't notice. I did the necessary changes and I didn't hurt bob's sentiments too.

I have also written an extra function called "snowflake" which draws three Koch curves to make the outline of the snowflake. I hope it's correct. Please let me know if any changes required.
Thanks once again !

from TurtleWorld import *
TurtleWorld()

def Koch(t,length):
    if length<=2 :
        fd(t,length)
        return
    Koch(t,length/3)
    lt(t,60)
    Koch(t,length/3)
    rt(t,120)
    Koch(t,length/3)
    lt(t,60)
    Koch(t,length/3)
   
def snowflake(length):
    bob=Turtle()
    bob.delay=0.01
    Koch(bob,length)
    bob=Turtle()
    bob.delay=0.01
    Koch(bob,length)
    bob=Turtle()
    bob.delay=0.01
    Koch(bob,length)

    
snowflake(540)
wait_for_user()

Remember that when dividing by an integer in Python (in this case dividing by 3) you get an integer as a result. Change 3 to 3.0 and you will see it works. Hope this helps.

Its turtleworld your own written module or what.
;)

It might be simpler to draw a Koch fractal using the module turtle that comes with your Python installation ...

import turtle as tu

def Koch(length):
    """draw a Koch fractal curve recursively"""
    if length <= 2 :
        tu.fd(length)
        return
    Koch(length/3)
    tu.lt(60)
    Koch(length/3)
    tu.rt(120)
    Koch(length/3)
    tu.lt(60)
    Koch(length/3)


tu.speed(0)
length = 300.0
# move to starting position
tu.penup()
tu.backward(length/2.0)
tu.pendown()
tu.title('Koch fractal curve')
Koch(length)
# keep showing until window corner x is clicked
tu.done()

RP770

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.