Koch Curve or Koch Snowflake

Python Methods and Functions

Koch Snowflake (also known as the Koch Curve, Koch Star or Koch Island) — it is a mathematical curve and one of the earliest fractal curves described. It is based on the Koch curve, published by the Swedish mathematician Helge von Koch in a 1904 article entitled “On a continuous curve without tangents, constructed from elementary geometry.”

The progression for the snowflake region converges 8/5 times the area the original triangle, while the progression along the perimeter of the snowflake diverges to infinity. Therefore, the snowflake has a finite area bounded by an infinitely long line.


construction

Step 1:

Draw an equilateral triangle. You can draw it with a compass or a protractor, or just look at it if you don't want to spend too much time drawing a snowflake.

  • It is best if the length of the sides is divisible by 3 due to the nature of this fractal. This will become clear in the next few steps.
  • Step 2:

    Divide each side into three equal parts. This is why it is convenient to divide the sides into three. 

    Step 3:

    Draw an equilateral triangle on each middle part. Measure the length of the middle third to find out the length of the sides of these new triangles. 

    Step 4:

    Separate each outside by a third. You can see the 2nd generation of triangles cover a bit of the 1st. These three segments should not be divided into three. 

    Step 5:

    Draw an equilateral triangle on each middle section.

  • Notice how you draw each successive generation of parts that are third in relation to the mast.

  • Lindenmayer system view


    The Koch Curve can be expressed by the following rewriting system ( Lindenmayer system ):

    Alphabet : F
    Constants : + ,? 
    Axiom : F
    Production rules : F? F + FF + F

    Here F means "pull forward", — means turn right 60 °, and + means turn left 60 °. 
    To create a Koch snowflake, you can use F ++ F ++ F (equilateral triangle) as an axiom.

    To create a Koch curve:

    # Python program to print a partial Koch curve.
    # import libraries: turtle standard
    # graphical library for python

    from turtle import *

      
    # function to create snowflake or Koch curve

    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 )

     
    # main function

    if __ name__ = = "__ main__" :

     

    # determine the speed of the turtle

    speed ( 0

    length = 300.0  

      

    # Pull the handle up - no drawing while moving.

    penup () 

      

    # Move the turtle back a distance,

    # opposite the direction of the turtle

    # headed by.

      # Do not change the turtle's course.

    backward ( length / 2.0

     

    # Pull pen down - draw while moving .

    pendown () 

     

    snowflake (length, 4 )

     

    # To control the closing of turtle windows

    mainloop () 

    Output: < / p>

    To create a complete snowflake with a Koch curve, we need to repeat the same drawing three times. So let's try this.

    # Python program for printing the full Koch curve.

    from turtle import *

     
    # function to create Koch snowflake or Koch curve

    def snowflake (lengthSide, levels):

    if levels = = 0 :

    forward (lengthSide)

    return < p> 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 )

     
    # main function

    if __ name__ = = "__ main __" :

    # determine the speed of the turtle

    speed ( 0

    length = 300.0  

     

      # Pull handle up - no graphic while moving.

    # Move the turtle back the opposite distance

    # in the direction the turtle is heading

    # Do not change the turtle's course.

    penup () 

     

    backward (length / 2.0 )

      

    # Pull the handle down - draw while moving.

    pendown () 

    for i in range ( 3 ) : 

    snowflake (length, 4 )

    right ( 120 )

     

    # To control the closing of turtle windows

      mainloop () 

    Output:

         

    This article courtesy of Subhajit Saha . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

    Please post comments if you find anything wrong or if you'd like to share more information on the topic discussed above.