…is always light

Tag Archives: koch curve

It’s over midnight here.. Since the source code in one of my videos is not very readable, I decided to publish the Python source code of the video in this blog post.

import turtle


def draw_curve():  
   window = turtle.Screen()  
   turt=turtle.Turtle()
   turt.setpos(-300,-100)
   turt.clear()
   turt.speed('fastest')
   turt.hideturtle()
    
   vonKoch(turt,5,500)
   turt.left(120)
   vonKoch(turt,5,500)
   turt.left(120)
   vonKoch(turt,5,500)
   turt.left(120)
   vonKoch(turt,5,500)
   window.exitonclick()  

def vonKoch(turt,depth,length):
    if depth == 0: turt.forward(length)
    else:
        vonKoch(turt,depth-1,length/3)
        turt.right(60)
        vonKoch(turt,depth-1,length/3)
        turt.left(120)
        vonKoch(turt,depth-1,length/3)
        turt.right(60)
        vonKoch(turt,depth-1,length/3)
        



draw_curve()

That’s it for now. Time to go to sleep.

Advertisements

Back to the world of fractals…

One of the famous fractals is the Koch curve, described by Helge von Koch in the early 1900s.

The basic idea of this fractal is as follows:

  1. Take an equliteral triangle, build another triangle in the middle of each side of the shape, the new triangle having a base length of 1/3 of the length of the side.
  2. Repeat ad infinitum.

Turtle graphics are very handy in the implementation. The needed functions or methods are Forward: Draw a line in given amount of units forwards to the current drawing angle. The second is Turn: Turns the drawing angle in given amount of degrees.

With recursion we are now ready to go to build a snowflake based on the Koch curve.

Below is two videos of my implemantation:

..and the same with more iterations: