program D24pro;

uses crt,graph,dos,dira;

var t:byte;
    x0,y0,z0,xx,yy,i,j:integer;
    si:shortint;
    fi:file of shortint;
    pk:array [1..80,1..3] of shortint;   {pontok koordinatai}
    pkk:array [1..80,1..3] of integer;
    os:array [1..200] of byte;      {oldalak kotesi sorrendje}
    xp,xz,ypz,f,g,al,x,y,z,r:real;
    s:string;
    rg:registers;


procedure t3d(x,y,z:real);
const a=70;       {szemek tavolsaga}
      d=600;      {tavolsag a kepernyotol}

var v:real;

begin
   v:=z*a/2;
   xp:=(d*x+v)/(d-z);
   xz:=(d*x-v)/(d-z);
   ypz:=(d*y)/(d-z);
end;


procedure szam(f,g:real;x0,y0,z0:integer);

const xt=640/260; {transzformacio x iranyban}
      yt=350/200; {               y         }

var sgd,snf,csf,sng,csg,snh,csh:real;
    
begin
      snf:=sin(f);csf:=cos(f);
      sng:=sin(g);csg:=cos(g);
   
   j:=1;
   repeat
      x:=pk[j,1];
      y:=pk[j,2];
      z:=pk[j,3];

      sgd:=csf*x-snf*z;z:=csf*z+snf*x;x:=sgd;

      sgd:=csg*y-sng*z;z:=csg*z+sng*y;y:=sgd;


      t3d(x+x0,y+y0,z+z0);
      pkk[j,1]:=trunc(xp*xt+xx);
      pkk[j,2]:=trunc(xz*xt+xx);
      pkk[j,3]:=trunc(yy-ypz*yt);
      inc(j);
   until pk[j,3]=127;

end;


procedure rajz(f,g:real;x0,y0,z0:integer);

begin
   szam(f,g,x0,y0,z0);

   j:=1;setcolor(green);
   repeat
      if os[j]=0 then begin inc(j);moveto(pkk[os[j],1],pkk[os[j],3]);inc(j); end;
      lineto(pkk[os[j],1],pkk[os[j],3]);
      inc(j);
   until os[j]=127;

   j:=1;setcolor(red);
   repeat
      if os[j]=0 then begin inc(j);moveto(pkk[os[j],2],pkk[os[j],3]);inc(j); end;
      lineto(pkk[os[j],2],pkk[os[j],3]);
      inc(j);
   until os[j]=127;
end;







{////////////////////////////////////////////////////////////////////////////}

begin
repeat
   dirf(s,'3');if s='' then begin  clrscr ;halt; end;

   assign(fi,s);
   reset(fi);
   t:=1;
   repeat for i:=1 to 3 do begin read(fi,si);pk[t,i]:=si;end;inc(t); until si=127;
   t:=1;repeat read(fi,si);os[t]:=si; inc(t);until si=127;
   close(fi);


   detectgraph(i,j);
   j:=1;
   initgraph(i,j,'');
   setrgbpalette(0,0,0,0);
   setrgbpalette(1,255,0,0);
   setrgbpalette(2,0,255,0);
   setrgbpalette(3,255,255,0);
   setwritemode(1);
   xx:=getmaxx;yy:=getmaxy;
   xx:=trunc(xx/2);yy:=trunc(yy/2);

   rg.ax:=0;
   intr($33,rg);

   rg.cx:=xx;rg.dx:=yy;
   rg.ax:=4; intr($33,rg);
   
   
   f:=0.0;g:=0.0;x0:=0;y0:=0;z0:=0;
   t:=0;
   repeat
      setactivepage(t);setvisualpage(1-t);t:=1-t;cleardevice;
      rg.ax:=3;
      intr($33,rg);
      case rg.bx of
         1:begin f:=f-pi/180*(rg.cx-xx);
                     g:=g+pi/180*(rg.dx-yy); end;
         0:begin x0:=x0+trunc((rg.cx-xx)/2);
                     y0:=y0-trunc((rg.dx-yy)/2); end;
         2:      z0:=z0+(rg.dx-yy);  
      end;

      rg.cx:=xx;rg.dx:=yy;
      rg.ax:=4; intr($33,rg);

      if f>=2*pi then f:=f-2*pi;
      if g>=2*pi then g:=g-2*pi;
      
      rajz(f,g,x0,y0,z0);
   until keypressed;
   closegraph;
until 1=2;
end.
