program QUBE;
{ Turbo Graphix Toolbox implementation of CUBE}

{$I TYPEDEF.SYS}
{$I GRAPHIX.SYS}
{$I KERNEL.SYS}


label
  newview, terminate;
var
  projected : array[1..16,1..2] of real;
  sine,cosine,perspective,xmax,ymax: real;
  direction : integer;
  letter : char;
const
  draw_order : array[1..33] of byte                 {Used in DISPLAY proc.}
                  =(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
                    5,10,13,16,3,12,1,8,11,4,7,2,15,6,9,14,1);
  lastedge : byte = 32;
  lastvertex : byte = 16;

  angle : real = 5.0;
  distance: real = 4.0;  {Initial default values for ADJUST procedure.}

  vertices  : array[1..16,1..4] of real                 {Initial vertices}
      =(( 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.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));






  Procedure GetScreenSize;
   {Inquires about physical screen dimensions to calculate aspect ratio   }
   { and adjust world coordinates so the projected image fills the screen.}

  const
    width : real= 9.5;
    height: real= 4.2;

  begin
    write(^J^J,'What are the dimensions (width height) of your screen? [',
            width:4: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 : byte;
      factor : real;

    begin
    for vertex := 1 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 := 1 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 hypercube in one of the 6 directions available from SO(4) }

    var
      i,j,vertex : byte;
      i_temp,j_temp : 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 := 1 to lastvertex do
        begin
        i_temp:= cosine * vertices[vertex,i] +   sine * vertices[vertex,j];
        j_temp:=  -sine * vertices[vertex,i] + cosine * vertices[vertex,j];
        vertices[vertex,i] := i_temp;
        vertices[vertex,j] := j_temp;
        end
    else
      for vertex := 1 to lastvertex do
        begin
        i_temp:= cosine * vertices[vertex,i] +  -sine * vertices[vertex,j];
        j_temp:=   sine * vertices[vertex,i] + cosine * vertices[vertex,j];
        vertices[vertex,i] := i_temp;
        vertices[vertex,j] := j_temp;
        end;
  end {Rotate};




  Procedure Adjust;
    {   Adjust angle of each rotation and viewer's perspective.    }

  var
    int_angle, i_dist, f_dist : integer;
  const
    radians_per_degree : real = 1.7E-2 ;

  begin
    LeaveGraphic;

    int_angle:=Round(angle);
    Write('How many degrees per rotation (1-359)? [',int_angle,'] ');
    Readln(angle);
    sine  := sin(angle * radians_per_degree);
    cosine:= cos(angle * radians_per_degree);
    i_dist:= Trunc(distance);
    f_dist:= Round( 10 * distance ) - 10 * i_dist;

    Write('What distance to the object (0.0-20.0)? [',
                                             i_dist,'.',f_dist,'] ');
    Readln(distance);
    perspective:=2.01 + distance;

    EnterGraphic;
  end {Adjust};



{  ***************  MAIN PROGRAM    ****************}

begin
  ClrScr;
  Writeln(^m^j^j,'                       THE HYPERCUBE AND ITS ROTATIONS');
  Writeln(^m^j^j,'                          By M. Victor Wickerhauser');
  Writeln(^j^j^j,' This program displays the cube in 4-dimensional space,'+
                                                            ' projected');
  Writeln(' into 3-dimensional space and presented on the screen'+
                                                   ' with perspective.');
  Writeln(' Type a,s,d,f,g, or h to rotate the cube by a fixed amount,'+
                                                             ' 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 <CR> to repeat a rotation. Type p'+
                                                  ' to change the amount');
  Writeln(' of rotation and the perspective parameter. 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);

  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}.
