(1 votes, average: 5.00 out of 5)
Loading...

Firstly, what is a Koch snowflake anyway?

Let us take a line segment of unit length and call it E(1). If we remove the middle third, replace it with the other two sides of the equilateral triangle based on the removed segment, we get something like _/\_ and call it E(2). Let us now take the straight lines that remain and repeat the process on them. Thus in more mathematical terms: E(k) comes from replacing  the middle third of each straight line segment of E(k-1) by the other two sides of and equilateral triangle.

When k is large, the curves E(k-1) and E(k) differ only slightly, and if we send that k to infinity, E(k) approaches a limiting curve we call the Koch curve.

Now that we have the Koch curve under our belt, all there is left to do is to make three Koch curves, whose E(1) curves form an equilateral triangle.

The following is Java code for drawing a Koch snowflake on a JPanel. The snowflake dynamically resizes, as you change the size of the window.

The algorithm is as following:

Given 2 Points (X1, Y1) and (X5, Y5), we assign following variables:

deltaX = X5 – X1,
deltaY = Y5 – Y1
X2 = x1 + deltaX / 3,
Y2 = Y1 + deltaY / 3

X3 = 0.5 * (X1 + X5) + sqrt(3) * (Y1 – Y5) / 6,
Y3 = 0.5 * (Y1 + Y5) + sqrt(3) * (X5 – X1) / 6
X4= X1 + 2 * deltaX / 3,
Y4 = Y1 + 2 * deltaY / 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.awt.Graphics;
 
public class KochSnowflake extends javax.swing.JPanel {
 
   public KochSnowflake() {
        initComponents();
    }
 
    int height;
    int width;
    int level = 3;
    @Override
    public void paint(Graphics g){
        height = this.getHeight() - this.getHeight()/4;
        width = this.getWidth();
        int xStart = width/2 - height/2;
        drawSnow(g, level, xStart + 20,             height - 20,   xStart + height - 20, height - 20);
        drawSnow(g, level, xStart + height - 20,    height - 20,   xStart + height/2,    20);
        drawSnow(g, level, xStart + height/2,       20,            xStart + 20,          height - 20);
 
    }
 
    private void drawSnow (Graphics g, int lev, int x1, int y1, int x5, int y5){
          int deltaX, deltaY, x2, y2, x3, y3, x4, y4;
 
          if (lev == 0){
              g.drawLine(x1, y1, x5, y5);
          }
          else{
                deltaX = x5 - x1;
                deltaY = y5 - y1;
 
                x2 = x1 + deltaX / 3;
                y2 = y1 + deltaY / 3;
 
                x3 = (int) (0.5 * (x1+x5) + Math.sqrt(3) * (y1-y5)/6);
                y3 = (int) (0.5 * (y1+y5) + Math.sqrt(3) * (x5-x1)/6);
 
                x4 = x1 + 2 * deltaX /3;
                y4 = y1 + 2 * deltaY /3;
 
                drawSnow (g,lev-1, x1, y1, x2, y2);
                drawSnow (g,lev-1, x2, y2, x3, y3);
                drawSnow (g,lev-1, x3, y3, x4, y4);
                drawSnow (g,lev-1, x4, y4, x5, y5);
            }
        }
}

Sources:
1) http://aisha91.hubpages.com/hub/Java-Source-Code-Recursive-Snow-Flakes
2) Falconer: Fractal Geometry

Your email is kept private. Required fields are marked *