There are several ways to the draw it. I posted this one on the Rosetta page where I only use the functions rotate() and translate(). Another way is by using sin()/cos() to rotate, like with
this code
float x, y, side = 300, angle, yi, xi;
void setup() {
size(400, 300);
background(255);
strokeWeight(2.5);
}
void draw() {
for (int d = 0; d < 5; d++) {
if (angle < 901) {
background(255);
koch(side, d);
angle += 225;
}
}
angle = 0;
x = 50-xi;
y = 220+yi;
yi += 2.8;
xi += 2;
side += 4;
if(y > 700) restart();
}
void koch(float side, float depth) {
float dx, dy;
if (depth == 0) {
dx = cos(angle*PI/180)*side;
dy = sin(angle*PI/180)*side;
line(x, y, x+dx, y+dy);
x += dx;
y += dy;
} else {
koch(side/3, depth-1);
angle += 60;
koch(side/3, depth-1);
angle -= 120;
koch(side/3, depth-1);
angle += 60;
koch(side/3, depth-1);
}
}
void restart(){
x = 0; y = 0; side = 300; angle = 0; yi = 0; xi = 0;
}
where zooming is possible, but incrementing a depth value more difficult. (As is with the first code.)
The best way is to use a Kochline class where you see and modify the construction of the curve more easily. You can read an excellent (as usual) explanation of the curve by Daniel here. (Somewhere in the middle of the page - part 4) This way I made the movie of my first attempt, posted above. I will leave a code beneath without depth increment but with the area of interest within an arrayList. It remains to know how to zoom with it.
The GLSL fractal examples @monkstone posted, use scaling. So I tried this as well, drawing a vertex shape, incrementing the scale() in draw. The problem with this method is that the strokeWidth also amplifies. And trying another idea, namely to resize/amplify the curve as an image, the lines blur.
My final goal is having a continuously fractal zooming of lines, instead of texture shapes.
ArrayList<KochLine> koch_lines_a = new ArrayList<KochLine>();
ArrayList next = new ArrayList<KochLine>();
PVector start, end;
float yh = 147;
int c, x, xe, z, depth = 6;
KochLine kl;
void setup() {
size(500, 500);
background(255);
xe = width;
for (int i = 0; i < 135; i++) {
start = new PVector(x, yh);
end = new PVector(xe, yh);
koch_lines_a = new ArrayList<KochLine>();
koch_lines_a.add(new KochLine(start, end));
for (int j = 0; j < depth; j++) {
generate();
}
x -= 3;
xe += 3;
yh += 1.73;
}
koch_lines_a.subList(0, 112).clear();
koch_lines_a.subList(koch_lines_a.size()-112, koch_lines_a.size()).clear();
}
void draw() {
background(255);
for (KochLine l : koch_lines_a) {
l.display();
}
}
void generate() {
ArrayList next = new ArrayList<KochLine>();
for (int i = 0; i < koch_lines_a.size(); i++) {
KochLine l = koch_lines_a.get(i);
PVector a = l.koch_a();
PVector b = l.koch_b();
PVector c = l.koch_c();
PVector d = l.koch_d();
PVector e = l.koch_e();
next.add(new KochLine(a, b));
next.add(new KochLine(b, c));
next.add(new KochLine(c, d));
next.add(new KochLine(d, e));
}
koch_lines_a = next;
}
class KochLine {
PVector start;
PVector end;
KochLine(PVector a, PVector b) {
start = a.get();
end = b.get();
}
PVector koch_a() {
return start.get();
}
PVector koch_b() {
PVector temp = PVector.sub(end, start);
temp.div(3);
temp.add(start);
return temp;
}
PVector koch_c() {
PVector a = start.get();
PVector temp = PVector.sub(end, start);
temp.div(3);
a.add(temp);
temp.rotate(-radians(60));
a.add(temp);
return a;
}
PVector koch_d() {
PVector temp = PVector.sub(end, start);
temp.mult(2/3.0);
temp.add(start);
return temp;
}
PVector koch_e() {
return end.get();
}
void display() {
stroke(0);
line(start.x, start.y, end.x, end.y);
}
}