HP Prime for All
English
Русский
Name | Von Koch Fractals |
Description | Generates Von Koch snowflake fractals. |
Author | Damien Unknown |
Source code formatted by website engine
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;