{Jozef Sivek ml. december 2004
rekurzivne fraktali
}

program mnohouholnik;
uses graph,crt;

type pole_v=array[1..2] of real;
     vrcholi=array[1..1000] of pole_v;

var
 gd, gm: Integer;
 m:vrcholi;
 n:word;
 d:real;
 g_color,g_color_txt,g_color_actual:byte;
 lim:longint;{iteracna medza}

function generuj(n:word):word;
         var c:real;

         begin

         c:=random*n;
         generuj:=(trunc(c)+1);

         end;

procedure move_point(var point,destination:pole_v; d:real);
          var move: pole_v;

          begin
          move[1]:=(destination[1] - point[1]) * (1-1/d);
          move[2]:=(destination[2] - point[2]) * (1-1/d);

            point[1]:=point[1] + move[1];
            point[2]:=point[2] + move[2];
          end;

procedure draw(point:pole_v);
          begin
          putpixel(round(point[1]),round(point[2]),g_color_actual);
          end;

procedure write_onscreen(n:integer;d:real);
          var ch,ch2:string;
          begin
          setcolor(g_color_txt);
          str(n,ch);
          ch:=ch+'-uhol.;koef=';
          str((1/d):1:5,ch2);
          ch:=ch+ch2;
          outtextxy(400,470,ch);
          end;


procedure iteruj(n:word; d:real);
          var I:integer;
              no:word;
              point:pole_v;
              g:longint;

          begin
          if n>=1000 then n:=999;

          for I:= 1 to n do begin
                            m[I][1]:=getmaxx/2  + sin( ((2*pi)/n) * I ) * (getmaxy/2);
                            m[I][2]:=getmaxy/2  - cos( ((2*pi)/n) * I ) * (getmaxy/2);
                            end;

          point[1]:=m[1][1];
          point[2]:=m[1][2];

          g:=0;
          repeat
          inc(g);
            no:=generuj(n);
            g_color_actual:=g_color+no;
            if g_color_actual mod 16 = 0 then inc(g_color_actual,5);
            move_point(point,m[no],d);
            draw(point);
          until g=lim;

          end;

begin
 gd := Detect;
 InitGraph(gd, gm,'c:/bp/bgi');
 if GraphResult <> grOk then
   Halt(1);

randomize;{dajte pozor kam strkate tuto proceduru, raz som ju casto
          volal a program bol neodolatelne pomaly;}

n:=6;
d:=3;
g_color:=4;
g_color_txt:=15;
lim:=400000;

repeat
randomize;
 n:=3+trunc(random*6);
 d:=1+random*n;
 g_color:=1+trunc(random*15);

 write_onscreen(n,d);

iteruj(n,d);

delay(0);
clearviewport;

until keypressed;

 CloseGraph;
end.


