Post Reply 
Fractals : Von Koch snowflake
12-11-2013, 11:08 PM (This post was last modified: 12-22-2013 07:58 AM by Damien.)
Post: #1
Fractals : Von Koch snowflake
Hi everyone,

This small program draws some simple fractals.
The K parameter controls the curve complexity.
Change K value from 0 (3 segments drawn), to 6 (12288 segments drawn).

Code:

EXPORT VON_KOCH_SNOWFLAKES()
BEGIN
 LOCAL T2,T1,AA,LL,a,l,L0;
 LOCAL Xi,Yi,X1,Y1,X0,Y0;
 LOCAL XA,YA,XD,YD,A0,II;
 LOCAL x,y,NP;
// Init parameters
 HAngle:=0; // RAD
 M:=3;
 N:=4;
 K:=5; // change K from 0 to 6 (or 7 if you are very very... patient)
 NP:=208;
// Point coordinates of the starting curve
 x:={0,NP,NP/2,0};
 y:={√3/2*NP,√3/2*NP,0,√3/2*NP};
// Model segments Length
 l:={1/3,1/3,1/3,1/3};
// Segments angle with the horizontal
 a:={0,π/3,−π/3,0};
// Black screen
 RECT_P(#383838h);
// Main loop 
 FOR II:=1 TO M DO
// Calculate starting and ending point of segment number II
  XD:=x(II); YD:=y(II);
  XA:=x(II+1); YA:=y(II+1);
  X0:=XD; Y0:=YD;
  IF XA<>XD THEN 
   A0:=ATAN((YA−YD)/(XA−XD)) ;
  ELSE 
   A0:=π/2*SIGN(YA−YD); 
  END;
  IF (XA−XD)<0 THEN A0:=A0+π; END;
  L0:=√((XA−XD)²+(YA−YD)²);
// Move the pen to its starting place
  Xi:=IP(X0); Yi:=IP(Y0);
//secondary loop: each time it pass through, an elementary segment is plotted.
  FOR I:=0 TO N^K−1 DO // so there is  N^K segments to draw (M time)
   LL:=L0; AA:=A0 ; T1:=I;
    IF K<>0 THEN 
     FOR J:=K−1 DOWNTO 0 STEP 1 DO
      R:=N^J; T2:=IP(T1/R);
      AA:=AA+a(T2+1); // angle for segment number I
      LL:= LL*l(T2+1); // length for segment number I 
      T1:=T1−T2*R;
     END; // NEXT J
    END;
// Time to draw
    X0:=X0+LL*COS(AA); 
    Y0:=Y0+LL*SIN(AA);
    X1:=IP(X0); Y1:=IP(Y0);
    LINE_P(Xi,Yi,X1,Y1,RGB(240,230,0)); // orange
    Xi:=X1; Yi:=Y1;
   END; // NEXT I
  END; // NEXT II
  WAIT(-1);
END;

Now if you want the fractal to grow inside the triangle replace the line:
Code:
x:={0,NP,NP/2,NP}
with:
Code:
x:={NP,0,NP/2,NP}
and you've got another variant of the Von Koch snowflake.


For something a little different, you can remplace the init parameters with:
Code:

 M:=4; N:=4; K:=5;
 NP:=208;
 x:={0,NP,NP,0,0};
 y:={0,0,NP,NP,0};
 l:={1/(2+2*COS(.45*π)),1/(2+2*COS(.45*π)),1/(2+2*COS(.45*π)),1/(2+2*COS(.45*π))};
 a:={0,.45*π,−.45*π,0};
or
Code:

 M:=4; N:=4; K:=6;
 NP:=208;
 x:={0,NP,NP,0,0};
 y:={0,0,NP,NP,0};
 l:={1/(2+2*COS(.48*π)),1/(2+2*COS(.48*π)),1/(2+2*COS(.48*π)),1/(2+2*COS(.48*π))};
 a:={0,.48*π,−.48*π,0};

or
Code:

 M:=1; N:=4; K:=6;
 NP:=208;
 x:={0,0};
 y:={NP,-NP};
 l:={1/3,1/3,√10/9,5/9};
 a:={0,π/2,−ATAN(3),0};

Or (added 15/12/2013)
Code:

 M:=2; N:=2; K:=12;
 NP:=240;
 x:={NP/2,NP/2,NP/2};
 y:={NP/4,3*NP/4,NP/4};
 l:={1/√2,1/√2};
 a:={π/4,−π/4)};


(added 16/12/2013)
Now lets try to cut corners. The segments drawing will now be replaced by arcs of ellipse.
The basic program is the same, with only the addition of few extra lines of code.
The S parameter controls the smoothness of the rounding, the K parameter controls the complexity degree of the curve.
Code:

EXPORT Rounded_Fractal()
BEGIN
LOCAL T2,T1,AA,LL,a,l,L0;
LOCAL Xi,Yi,X1,Y1,X0,Y0;
LOCAL XA,YA,XD,YD,A0,II;
LOCAL x,y,NP,X2,Y2,VX,VY;
LOCAL WX,WY,K4,XQ,YQ,AN;
HAngle:=0; // RAD 
// init parameters
 M:=2; N:=4; K:=6; S:=3;
 NP:=240;
 x:={0,NP,0};
 y:={NP,0,NP};
 l:={1/2,.45,.45,.5};
 a:={0,π/2,−π/2,0};
// Clear screen
 RECT_P(#383838h);
// main loop 
 FOR II:=1 TO M DO
  XD:=x(II); YD:=y(II);
  XA:=x(II+1); YA:=y(II+1);
  X0:=XD; Y0:=YD; X1:=X0; Y1:=Y0;
  X2:=X0; Y2:=Y0; 
   IF XA<>XD THEN 
   A0:=ATAN((YA−YD)/(XA−XD)) ;
  ELSE 
   A0:=π/2*SIGN(YA−YD); 
  END;
  IF (XA−XD)<0 THEN A0:=A0+π; END;
   L0:=√((XA−XD)²+(YA−YD)²);
   FOR I:=0 TO N^K−1 DO
    LL:=L0; AA:=A0 ; T1:=I;
    IF K<>0 THEN 
     FOR J:=K−1 DOWNTO 0 STEP 1 DO
      R:=N^J; T2:=IP(T1/R);
      AA:=AA+a(T2+1); LL:= LL*l(T2+1);
      T1:=T1−T2*R;
     END; // NEXT J
    END;
    X0:=X1; Y0:=Y1; X1:=X2; Y1:=Y2;
    X2:=X2+LL*COS(AA);Y2:=Y2+LL*SIN(AA);
    VX:=X1-X0; VY:=Y1-Y0;
    WX:=X2-X1; WY:=Y2-Y1; 
    FOR K4:=0 TO S DO
     AN:=π/2*K4/S;
     XQ:=(X0+X2-COS(AN)*WX+SIN(AN)*VX)/2;
     YQ:=(Y0+Y2-COS(AN)*WY+SIN(AN)*VY)/2;
     IF I==0 THEN  
      Xi:=IP(X0); Yi:=IP(Y0);
     END;
     IF I>0 THEN
      LINE_P(Xi,Yi,IP(XQ),IP(YQ),RGB(240,230,0)); 
      Xi:=IP(XQ); Yi:=IP(YQ); 
    END;
   END; // NEXT K4
  END; // NEXT I
 END; // NEXT II
 WAIT(-1);
END;

(added 18/12/13)
Lets change some init parameters like so:
Code:

 M:=1; N:=13; K:=3; S:=4;
 NP:=240;
 x:={0,NP};
 y:={NP,0};
 l:={.4,.4,.2,.2,.2,.2,.4,.4,.2,.2,.2,.2,.2};
 a:={0,π/2,0,−π/2,0,−π/2,-π,−π/2,0,π/2,0,π/2,0};

Cheers,

Damien.
Find all posts by this user
Quote this message in a reply
12-14-2013, 09:03 PM
Post: #2
RE: Fractales : Von Koch snowflake
Downloading...... Wink

Thanks!
Find all posts by this user
Quote this message in a reply
12-14-2013, 11:01 PM (This post was last modified: 12-18-2013 10:15 PM by Damien.)
Post: #3
RE: Fractals : Von Koch snowflake
You're welcome ! Have Fun !

regards,

Damien.
Find all posts by this user
Quote this message in a reply
12-15-2013, 06:14 PM
Post: #4
RE: Fractales : Von Koch snowflake
Nice work !

http://mic.nic.free.fr - Youtube - Facebook
Find all posts by this user
Quote this message in a reply
12-15-2013, 07:07 PM (This post was last modified: 12-18-2013 10:16 PM by Damien.)
Post: #5
RE: Fractals : Von Koch snowflake
And more to come very soon!

Cheers,

Damien.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 3 Guest(s)