function VonKoch(r)
  %Generates a Von Koch curve with scaling factor r on the line [0,1]. It is recommended to choose a value of r between 1/4 and 1/2
  
x = [0:0.01:1];
y = zeros(1,101);

X = [x;y];

MID = (1-2*r)/2;    %Length to midpoint of central triangle
COSX = MID/r;    %Cosine of rotation angle
SINX = sqrt(1-COSX^2);    %Sine of rotation Angle
TRANSY = sqrt(r^2 - MID^2);    %Factor to translate f3 in y-direction based on height of triangle with base MID and hypoteneuse r

f1 = @(a) r*a;    %Scale
f2 = @(a) r*[COSX,-SINX;SINX,COSX]*a + [r;0];    %Scale, rotate counterclockwise, translate
f3 = @(a) r*[COSX,SINX;-SINX,COSX]*a + [1/2;TRANSY];    %Scale, rotate clockwise, translate
f4 = @(a) r*a + [(1-r);0];    %Scale, translate

kmax = 8;


for k = 1:kmax
  x1 = f1(X);
  x2 = f2(X);
  x3 = f3(X);
  x4 = f4(X);
  X = [x1,x2,x3,x4];
end

plot(X(1,:),X(2,:))