program TOPE2;
{ Turbo Pascal v3.0 implementation }
{ Turbo Graphix Toolbox implementation of TOPE}

{$I TYPEDEF.SYS}
{$I GRAPHIX.SYS}
{$I KERNEL.SYS}

label
  newview, terminate;
var
  projected : array[1..29,1..2] of real;
  sine,cosine,perspective,xmax,ymax: real;
  direction : integer;
  firstedge, lastedge, firstvertex, lastvertex : byte;
  angle,distance : real;
  letter, polytope, parameter : char;
const
  vertices  : array[1..29,1..4] of real                 {Initial vertices }
      =(( 1.0, 1.0, 1.0, 1.0),   ( 1.0, 1.0, 1.0,-1.0), {of the tessaract,}
        ( 1.0, 1.0,-1.0,-1.0),   ( 1.0,-1.0,-1.0,-1.0), { vertices 1-16. }
        (-1.0,-1.0,-1.0,-1.0),   (-1.0,-1.0, 1.0,-1.0),
        ( 1.0,-1.0, 1.0,-1.0),   ( 1.0,-1.0, 1.0, 1.0),
        (-1.0,-1.0, 1.0, 1.0),   (-1.0,-1.0,-1.0, 1.0),
        ( 1.0,-1.0,-1.0, 1.0),   ( 1.0, 1.0,-1.0, 1.0),
        (-1.0, 1.0,-1.0, 1.0),   (-1.0, 1.0, 1.0, 1.0),
        (-1.0, 1.0, 1.0,-1.0),   (-1.0, 1.0,-1.0,-1.0),

    (-1.5811388, -0.912871,  -0.6454972, -0.5000000), { of the simplex, }
    ( 1.5811388, -0.912871,  -0.6454972, -0.5000000), { vertices 17-21. }
    ( 0.0,        1.8257419, -0.6454972, -0.5000000),
    ( 0.0,        0.0,        1.9364917, -0.5000000),
    ( 0.0,        0.0,        0.0,        2.0000000),

        ( 2.0, 0.0, 0.0, 0.0),  ( 0.0, 2.0, 0.0, 0.0),  { of the hexadec, }
        ( 0.0, 0.0, 2.0, 0.0),  ( 0.0, 0.0, 0.0, 2.0),  { vertices 22-29. }
        (-2.0, 0.0, 0.0, 0.0),  ( 0.0,-2.0, 0.0, 0.0),
        ( 0.0, 0.0,-2.0, 0.0),  ( 0.0, 0.0, 0.0,-2.0));

  draw_order : array[1..69] of byte             {Used in DISPLAY proc.}
         =(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,5,  {tessaract 1..33}
          10,13,16,3,12,1,8,11,4,7,2,15,6,9,14,1,

          17,18,19,20,21,17,19,21,18,20,17,          {simplex 34..44}

          22,23,24,25,26,27,28,29,23,28,22,27,25,
          28,26,24,27,29,24,22,25,23,26,29,22);      {hexadec 45..69}


Procedure GetScreenSize;
   {Inquires about physical screen dimensions to calculate aspect ratio   }
   { and adjust world coordinates so the projected image fills the screen.}

  var
    width, height: real ;

  begin
    width := 12.0;		{Default: 15 inch diagonal 4:3}
    height:= 9.0;
    write(^J^J,'What are the dimensions (width height) of your screen? [',
            width:5:1,height:5:1,'] ');
    readln(width,height);
    if height<width then
      begin
      ymax := 2.1;
      xmax := ymax * width/height;
      end
    else
      begin
      xmax := 2.1;
      ymax := xmax * height/width;
      end;
  end; {GetScreenSize}


Procedure Project;
   { Projects [x,y,z,w] onto [x,y,z], then calculates the image [x',y']  }
   { seen by an observer at distance PERSPECTIVE from the xy plane.      }
   { The apparent coordinates [x',y'] are given by the equations:        }
   {     x'=x*perspective/(perspective-z)                                }
   {     y'=y*perspective/(perspective-z)                                }

  var
    vertex : integer;
    factor : real;

  begin
    for vertex := firstvertex to lastvertex do
      begin
      factor := perspective / (perspective - vertices[vertex,3]);
      projected[vertex,1] := factor * vertices[vertex,1];
      projected[vertex,2] := factor * vertices[vertex,2]
      end
  end {Project};


Procedure Display;
   {Display the projected hypercube on the screen edge-by-edge, following }
   {an efficient route: the first 16 edges are a consecutive path through }
   {vertices 1 to 16, while the last 16 edges of the 32-edge hypercube's  }
   {1-skeleton are a consecutive path through the vertices in DRAW_ORDER. }
   {First, the real-valued coordinates in PROJECTED must be scaled to fit }
   {into the graphics mode window. This is done by the proc. DefineWorld  }
   {in the main program. The projected hypercube (in 3-dimensional space) }
   {has no dimension larger than 2, and no coordinates higher than (1,1,1)}
   {The initialization procedure GetScreenSize assures that squares are   }
   {square on all types of monitors. World coordinates are chosen so that }
   {the square is half as tall as the screen. 1 in. = 1 on a  Zenith 170 .}

  var
    edge : byte ;
    x1,x2,y1,y2: real ;

  begin
    SelectScreen(2);
    ClearScreen;
    for edge := firstedge to lastedge do
    begin
      x1:= projected[draw_order[edge],1];
      y1:= projected[draw_order[edge],2];
      x2:= projected[draw_order[edge+1],1];
      y2:= projected[draw_order[edge+1],2];
      DrawLine(x1,y1,x2,y2);      {Draw the edge, white on black }
    end;
    CopyScreen;                   {Present the results}
  end{Display};


Procedure Rotate;
    {Rotate the polytope in one of the 6 directions available from SO(4) }

  var
    i,j,vertex : integer;
    itemp,jtemp : real;

  begin
    case direction of
      1: begin i:=1; j:=2  end;
     -1: begin i:=1; j:=2  end;
      2: begin i:=1; j:=3  end;
     -2: begin i:=1; j:=3  end;
      3: begin i:=1; j:=4  end;
     -3: begin i:=1; j:=4  end;
      4: begin i:=2; j:=3  end;
     -4: begin i:=2; j:=3  end;
      5: begin i:=2; j:=4  end;
     -5: begin i:=2; j:=4  end;
      6: begin i:=3; j:=4  end;
     -6: begin i:=3; j:=4  end
  end;
  if direction>0 then
    for vertex := firstvertex to lastvertex do
      begin
      itemp:= cosine * vertices[vertex,i] +   sine * vertices[vertex,j];
      jtemp:=  -sine * vertices[vertex,i] + cosine * vertices[vertex,j];
      vertices[vertex,i] := itemp;
      vertices[vertex,j] := jtemp
      end
  else
    for vertex := firstvertex to lastvertex do
      begin
      itemp:= cosine * vertices[vertex,i] +  -sine * vertices[vertex,j];
      jtemp:=   sine * vertices[vertex,i] + cosine * vertices[vertex,j];
      vertices[vertex,i] := itemp;
      vertices[vertex,j] := jtemp
      end
  end {Rotate};

Procedure SetDefaults;
  begin
    angle := 5.0;
    distance :=  4.0;
    polytope:= 'T'{tessaract};
    firstedge := 1;
    lastedge := 32;    {Default: draw edges of tessaract.}
    firstvertex := 1;
    lastvertex := 16; {Default: use vertices of tessaract.}
  end;

Procedure Adjust;
    {   Adjust angle of each rotation and viewer's perspective.    }

  label
    ReadAngle, ReadDistance, ReadPolytope;
  const
    radians_per_degree : real = 1.745329252E-2 ;

  begin
    LeaveGraphic;
  ReadAngle:
    Write('How many degrees per rotation (0-90)? [',angle:4:1,'] ');
    Readln(angle);
    if (angle <= 0) or (angle >= 90) then goto ReadAngle;
    sine  := sin(angle * radians_per_degree);
    cosine:= cos(angle * radians_per_degree);

  ReadDistance:
    Write('What distance to the object (0.0-100.0)? [',
					distance:5:1,'] ');
    Readln(distance);
    if (distance <= 0) or (distance > 100) then goto ReadDistance;
    perspective := 2.01 + distance;

  ReadPolytope:
    Write('Choose Tesseract, Simplex, or Hexadec (T,S,H)? [',polytope,'] ');
    Readln(parameter);
    if parameter <> '' then
      case parameter of
        's','S': polytope:='S';
        't','T': polytope:='T';
	'h','H': polytope:='H'
        else     goto ReadPolytope;
      end;
    case polytope of
      'T': begin
           firstvertex:=1;  lastvertex:=16;  firstedge:=1;  lastedge:=32
           end;
      'S': begin
           firstvertex:=17; lastvertex:=21;  firstedge:=34; lastedge:=43
           end;
      'H': begin
           firstvertex:=22; lastvertex:=29;  firstedge:=45; lastedge:=68
           end;
    end;
    EnterGraphic;
  end {Adjust};




{  ***************  MAIN PROGRAM    ****************}

begin

  ClrScr;
  Writeln(^m^j^j,'                      ROTATING POLYTOPES IN '+
                                                          '4 DIMENSIONS');
  Writeln(^m^j^j,'                           M. Victor Wickerhauser');
  Writeln(^j^j^j,' This program displays three regular 4-dimensional'+
                                                 ' polytopes, projected');
  Writeln(' into 3-dimensional space and presented on the screen'+
                                                   ' with perspective.');
  Writeln(' At the prompts, select the increment of rotation and the'+
                                                   ' viewing distance.');
  Writeln(' Choose the polytope by typing T(essaract), S(implex) or'+
                                                          ' H(exadec).');
  Writeln(' Default values are displayed in [brackets]. When the polytope'+
                                                    ' is displayed,');
  Writeln(' type a,s,d,f,g, or h to rotate the polytope by the increment,'+
                                                            ' using the');
  Writeln(' six elementary generators of the rotation group of'+
                                                   ' 4-dimensional space,');
  Writeln(' namely SO(4).  Type A,S,D,F,G, or H to rotate by the' +
                                                        ' inverse of the');
  Writeln(' rotation. Type <return> to repeat a rotation. Type p'+
                                                  ' to adjust the increment');
  Writeln(' of rotation, the viewing distance, or to select a new polytope.');
  Writeln(' Type q to quit the program.');
  Writeln(^j,'Press any key to begin...');
  repeat until keypressed;

  GetScreenSize;
  InitGraphic;

  DefineWorld(1,-xmax,ymax,xmax,-ymax);
  SelectWorld(1);
  SelectWindow(1);

  SetDefaults; 
  Adjust;

newview:
  Project;
  Display;

  Read(Kbd,letter);
  case letter of
    'a':direction:=-1;
    's':direction:=-2;
    'd':direction:=-3;
    'f':direction:=-4;
    'g':direction:=-5;
    'h':direction:=-6;
    'p':              Adjust;
    'A':direction:=1;
    'S':direction:=2;
    'D':direction:=3;
    'F':direction:=4;
    'G':direction:=5;
    'H':direction:=6;
    'q':  goto       terminate;
    else;   {Retain last value of DIRECTION}
  end;
  Rotate;
  goto newview;
terminate:
   LeaveGraphic;

end {Main program}.
