program G3d;
{$path "ram:include/","pascal:include/"}
{$incl "graphics.lib","intuition.lib"}

 const
  pi    = 3.14159;
  width = 320;
  height= 256;
  filename = 'pascal:work/collage.dat';
  nmax = 5
 type
  point = record
            x,y : real;
          end;
 var
  rec,rec1:array[1..10000] of point;
  points  :array[-70..70,-70..70] of boolean;
  A       :array [1..5,1..2,1..3] of real;
  plane:boolean;
  i,j,n:integer;
  x,y:real;
  p:string;
  t1:IntuiText;
  Scr1,Scr2: ^Screen;
  Win1,Win2: ^Window;
  Rp1,Rp2,Rp: ^RastPort;
  pointer : ^byte;
  key : byte;

procedure Init;
 var f: file of char;
     i,j:integer;
 begin
   OpenLib(GfxBase,'graphics.library',0);
   OpenLib(IntBase,'intuition.library',0);
   Scr1:=Open_Screen(0,0,320,256,1,0,1,16384,'');
   Scr2:=Open_Screen(0,0,320,256,1,0,1,16384,'');
   Win1:=Open_Window(0,0,320,256,1,0,$800,Nil,Scr1,320,256,320,256);
   Win2:=Open_Window(0,0,320,256,1,0,$800,Nil,Scr2,320,256,320,256);
   SetRGB4(^Scr1^.ViewPort,0,0,0,3);
   SetRGB4(^Scr1^.ViewPort,1,15,15,15);
   SetRGB4(^Scr2^.ViewPort,0,0,0,3);
   SetRGB4(^Scr2^.ViewPort,1,15,15,15);
   Rp1:=Win1^.RPort;
   Rp2:=Win2^.Rport;
   Rp:=Rp1;
   plane:=true;
   Reset (f,FileName);
   if eof(f) then write ('File not found: ',FileName);
   for n:=1 to nmax do
     for i:=1 to 2 do
       for j:=1 to 3 do read(f,A[n,i,j]);
   for i:=-50 to 50 do
     for j:=-50 to 50 do points[i,j]:=false;
   points[0,0]:=true;
   for i:=1 to 999 do
     begin
       rec[i].x:=138*random-69;
       rec[i].y:=138*random-69;
     end;
   rec[1].x:=-40;rec[1].y:=-40;
   rec[2].x:=-1000;
 end;

procedure Close;
 begin
   Close_Window(Win1);
   Close_Window(Win2);
   Close_Screen(Scr1);
   Close_Screen(Scr2);
 end;


procedure Step;
  var i:integer;
  begin
    SetAPen(Rp,0);
    rectfill(Rp,0,0,width-1,height-1);
    SetAPen(Rp,1);
    i:=1;
    while rec[i].x<>-1000 do
      begin
        move(rp,round(rec[i].x)+160,127-round(rec[i].y));
        draw(rp,round(rec[i].x)+160,127-round(rec[i].y));
        i:=i+1;
      end;

    for i:=-70 to 70 do
      for j:=-70 to 70 do
        points[i,j]:=false;
    i:=1;j:=1;
    while rec[i].x<>-1000 do
      begin
        for n:=1 to nmax do
          begin
            x:=A[n,1,1]*rec[i].x+A[n,1,2]*rec[i].y+A[n,1,3];
            y:=A[n,2,1]*rec[i].x+A[n,2,2]*rec[i].y+A[n,2,3];
            if not points[round(x),round(y)] then
              begin
                rec1[j].x:=x;rec1[j].y:=y;j:=j+1;
                points[round(x),round(y)]:=true;
              end;
          end;
        i:=i+1;
      end;
    rec1[j].x:=-1000;
    i:=0;
    repeat
      i:=i+1;
      rec[i].x:=rec1[i].x;
      rec[i].y:=rec1[i].y;
    until rec1[i].x=-1000;
    if plane then begin
                    ScreenToFront(Scr1);
                    Rp:=Rp2;
                  end
             else begin
                    ScreenToFront(Scr2);
                    Rp:=Rp1;
                  end;
    plane:=not plane;
  end;

begin
 Init;
 pointer:=ptr($bfec01);
 repeat
  step;
  key:=pointer^;
 until key=126;
 Close;
end.








