program g3 (input, output);

%include '786pas.inc'

const
   pi = 3.1415927;

type
   vector = array [1..4] of real;
   m4x4 = array [1..4] of vector;
   polytype = array [1..4] of integer;

var
   xrot, yrot, zrot, Tper, Ttrans, Trot, mtemp: m4x4;
   xa, ya, za, xtrans, ytrans, ztrans, d, scale: real;
   p: array [1..390] of vector;
   polys: array [1..360] of polytype;
   vis: array [1..360] of boolean;
   i, j: integer;
   c: char;
   data: text;


procedure MVmult (m: m4x4; v: vector; var vr: vector);

var
   i, j: integer;

begin
   for i := 1 to 4 do
   begin
      vr [i] := 0;
      for j := 1 to 4 do
         vr [i] := vr [i] + m [i, j] * v [j]
   end
end;


procedure MMmult (m1, m2: m4x4; var mr: m4x4);

var
   i, j, k: integer;

begin
   for i := 1 to 4 do
      for j := 1 to 4 do
      begin
         mr [i, j] := 0;
         for k := 1 to 4 do
            mr [i, j] := mr [i, j] + m1 [i, k] * m2 [k, j]
      end
end;


procedure mout (m: m4x4);

var
   i, j: integer;

begin
   for i := 1 to 4 do
   begin
      for j := 1 to 4 do
         write (m [i, j]: 10: 4);
      writeln
   end
end;


procedure vout (v: vector);

var
   j: integer;

begin
   for j := 1 to 4 do
   begin
      write (v [j]: 10: 4);
      writeln
   end
end;


procedure readfile;

var
   i, j, line, vert: integer;

begin
   reset (data, 'gpract1');
   readln (data);
   readln (data);
   for i := 1 to 390 do
   begin
      read (data, line);
      read (data, c);
      for j := 1 to 3 do
         read (data, p [i, j]);
      p [i, 4] := 1;
      readln (data)
   end;

   for i := 1 to 3 do
      readln (data);

   for i := 1 to 360 do
   begin
      read (data, c);
      read (data, vert);
      for j := 1 to 4 do
      begin
         read (data, c);
         read (data, c);
         read (data, polys [i, j])
      end;
      readln (data)
   end;

   for i := 1 to 3 do
      readln (data);

   read (data, xtrans);
   read (data, ytrans);
   read (data, d);

   read (data, xa);
   read (data, ya);
   read (data, za)

end;


procedure setline (var m: m4x4; l: integer; a, b, c, d: real);

begin
   m [l, 1] := a; m [l, 2] := b; m [l, 3] := c; m [l, 4] := d
end;


procedure norm (var v: vector);

var
   i: integer;

begin
   for i := 1 to 3 do
      v [i] := v [i] / v [4];
   v [4] := 1
end;


procedure drawpoly (poly: polytype);

var
   i: integer;
   a,b:integer;

begin
        a:=round(p[poly[4],1]);
        b:=round(p[poly[4],2]);
        absmov(a,b);
        linerel(round(p[poly[1],1])-round(p[poly[4],1]),
             round(p[poly[1],2])-round(p[poly[4],2]));
   for i := 2 to 4 do
         linerel(round (p [poly [i], 1] - p [poly [i - 1], 1]),
         round (p [poly [i], 2] - p [poly [i - 1], 2]));
end;

   
function dot_prod (v1, v2: vector): real;

begin
   dot_prod := v1 [1] * v2 [1] + v1 [2] * v2 [2] + v1 [3] * v2 [3]
end;


procedure cross_prod (v1, v2: vector; var cross: vector);

begin
   cross [1] := v1 [2] * v2 [3] - v1 [3] * v2 [2];
   cross [2] := v1 [3] * v2 [1] - v1 [1] * v2 [3];
   cross [3] := v1 [1] * v2 [2] - v1 [2] * v2 [1];
   cross [4] := 1
end;


function face_away (poly: polytype): boolean;

var
   i: integer;
   edge1, edge2, cross, pos_vec2: vector;

begin
   for i := 1 to 3 do
   begin
      edge1 [i] := p [poly [2], i] - p [poly [1], i];
      edge2 [i] := p [poly [2], i] - p [poly [3], i]
   end;
   pos_vec2 [1] := p [poly [2], 1];
   pos_vec2 [2] := p [poly [2], 2];
   pos_vec2 [3] := p [poly [2], 3] + d;
   cross_prod (edge2, edge1, cross);
   face_away := (dot_prod (cross, pos_vec2) <= 0)
end;


begin
   write ('Scale');
   readln (scale);
   readfile;

   xa := xa/360*2*pi;
   ya := ya/360*2*pi;
   za := za/360*2*pi;

   setline (xrot, 1, 1, 0, 0, 0);
   setline (xrot, 2, 0, cos (xa), -sin (xa), 0);
   setline (xrot, 3, 0, sin (xa), cos (xa), 0);
   setline (xrot, 4, 0, 0, 0, 1);

   setline (yrot, 1, cos (ya), 0, sin (ya), 0);
   setline (yrot, 2, 0, 1, 0, 0);
   setline (yrot, 3, -sin (ya), 0, cos (ya), 0);
   setline (yrot, 4, 0, 0, 0, 1);

   setline (zrot, 1, cos (za), -sin (za), 0, 0);
   setline (zrot, 2, sin (za), cos (za), 0, 0);
   setline (zrot, 3, 0, 0, 1, 0);
   setline (zrot, 4, 0, 0, 0, 1);

   setline (Tper, 1, 1, 0, 0, 0);
   setline (Tper, 2, 0, 1, 0, 0);
   setline (Tper, 3, 0, 0, 0, 0);
   setline (Tper, 4, 0, 0, -1/d, 1);

   setline (Ttrans, 1, scale, 0, 0, xtrans);
   setline (Ttrans, 2, 0, scale, 0, ytrans);
   setline (Ttrans, 3, 0, 0, scale, 0);
   setline (Ttrans, 4, 0, 0, 0, 1);

   MMmult (yrot, xrot, mtemp);
   MMmult (zrot, mtemp, Trot);

   for i := 1 to 390 do
   begin
      MVmult (Trot, p [i], p [i]);
      MVmult (Tper, p [i], p [i]);
      norm (p [i])
   end;

   for i := 1 to 360 do
      vis [i] := not face_away (polys [i]);

   for i := 1 to 390 do
      MVmult (Ttrans, p [i], p [i]);

   gpinit;  {initialise 82786}

opengcmbmain;
   for i := 1 to 360 do
      if vis [i] and (i <> 118) then drawpoly (polys [i]);
closegcmbmain;

end.
