Wednesday, 18 July 2012

KOCH'S SNOWFLAKE- A Mathematical Curve

     Koch's snowflake also known as Koch star is a mathematical curve and one of the earliest fractals to be described by Swedish scientist Helge Von Koch.

 How is a Koch- snowflake built?

     The snowflake starts as an equilateral triangle. Recursively each side is altered by-
  • divide the line segment into three segments of equal length.
  • draw an equilateral triangle that has the middle segment from step 1 as its base and points outward.
  • remove the line segment that is the base of the triangle from step 2.
After one iteration of this process, the resulting shape is the outline of a         hexagram as in the figure below.

Lindenmayer System Representation

Lindenmayer system makes use of alphabet-- F, and constants -- +,-.
F draws a length forward, + moves an angle of 60 degrees to the right and - moves 60 degrees to the left.
The production rules are:- F-F++F-F.

Pygame Implementation

I implemented the Koch snowflake model using pygame. It makes use of recursively calling the function till a set number of iterations. A set range is decided where the recursions are placed in a stack and the range decremented each time. After the value of the range becomes zero the function implements once. Again the stack pops the recursion and decrements the value by 1 and this continues till all the recursive calls are popped from the stack.

def koch(screen,range1,angle1):
        if range1 == 0:
                Koch_snowflake(1,angle1)
        else:
                for angle in [60, -120, 60, 0]:
                        koch(screen, range1-1, angle1)
                        right(angle)
The output of the code I  implemented using the above is as follows:


Anti Koch snowflake is also an interesting fractal, which is built similar to Koch snowflake only varying in the direction and angle.

The output looks like the figure below:-



For complete code refer to my Github id- https://github.com/anjalirmenon/koch_snowflake


No comments:

Post a Comment