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"