3 - Programming the Koch's curve

  a - Intuitive approach

The first idea is to calculate the coordinates of each point.
One iteration
As a matter of fact, it is quite easy to find the coordinates of the 3 new points C, D and E when you know the one of A and B.

But if you want to calculate more than one or to iterations, it quickly becomes awful. It is impossible to store the coordinates in such an order that you know which one follows which one, you have problems too when you want to insert the new points between the other ones...
So, it seems to be impossible to draw the snowflake when thinking this way.

What is the good approach?

  b - Recursion


Let us remember what we have said in the part - 2 - b -. The Koch snowflake is an iterated function system.
How can we describe what we are doing when iterating one time.

A first algorithm is.

length=one
draw a line (length/3) long
turn left 60°
draw a line (length/3) long
turn right 120°
draw a line (length/3) long
turn left 60°
draw a line (length/3) long

Describing the pattern
- the first iteration -

Now, if we call this small sequence of instruction a function: Function flacon(length,n) where n is the number of iteration, the algorithm for 2 iteration will be:

length=one
flacon(length/3,n-1)
turn left 60°
flacon (length/3,n-1)
turn right 120°
flacon (length/3,n-1)
turn left 60°
flacon (length/3,n-1)

This algorithm will be translated into Fortran. So we will only calculate the coordinates of the points, and afterwards, we will use Matlab so as to visualize the results of our calculations.
Consequently, the instruction "draw a line (lenght/3) long" will become a "write xa, ya and xb, yb in a file".
We take an equilateral triangle as initial pattern, so as to obtain the whole Koch's snowflake at the end.

One problem is to determine the coordinates of the point E, knowing the coordinates of the points A and B. The calculations are detailed in annex. To sum up, we write that this point is the intersection of to circles, which radius equal d(A,B)/3.
Note that the test concerning the sign of the difference between XA and XB is aimed at choosing which point is the good one. As a matter of fact, we gain two points with this method, and it is not obvious to choose the right one.

Here is the listing of the program:

Program
Program
- listing of the Fortran program to generate the Koch's snowflake -
Here is a link toward the source of the fortran code.
Just click and a page will open where you can cut and copy in a FILE.F. Then you just have to compile it and launch the FILE.EXE that will be created.
Normally, the files X.TXT and Y.TXT will be created in the directory where you are currently launching the code.
If you experience some problem, the KOCH.F file is in the directory of the html pages. Just copy it on your disk and compile!
Then, just type in Matlab command window:
    load x.txt
    load y.txt
    plot (x,y)

You can remark that the subroutine flacon calls itself inside. This is very specific, and this is why we have to precise "recursive subroutine". Omitting this word will cause the compiler to generate an error.

Now that we have typed those lines, we can run the koch.exe program generated by the compiler and use Matlab so as to visualize the points stocked in the two files x.txt and y.txt.

Next