{Program that handles drawing triangles, squares, and circles
on a standard VGA display.  Must include EGAVGA.BGI file with
program for it to function.  Circle and Square portions fully
notated for easy reference.}
{Written by Jason J Schwarz on Sept. 14,1993}


USES CRT, GRAPH; {Libraries program uses}
VAR choice : STRING[1]; {Define choice as a character}
CONST
     SIZE=100;

PROCEDURE DrawPic(side1,side2,hyp : INTEGER);
VAR
  gd, gm : INTEGER;
  BX1,Y1,BX2,Y2,X1,X2 : INTEGER;
  length1,length2 : REAL;
  ArcCoords : ARCCOORDSTYPE;
begin
     CLRSCR;
     WRITELN(Output,'Press ENTER to draw triangle and ENTER when done viewing it.');
     READLN(Input);
     length1:=(size/sin(hyp*(pi/180)))*sin(side1*(pi/180));
     length2:=(size/sin(hyp*(pi/180)))*sin(side2*(pi/180));
     Gd := Detect;
     InitGraph(Gd, Gm, '');
     if GraphResult <> grOk then Halt(1);
     setcolor(7);
     Bx1:=TRUNC((640-size)/2);
     Bx2:=TRUNC((640-size)/2)+size;
     Line(Bx1,400,Bx2,400);
     setcolor(0);
     arc(Bx1,400,0,side2,TRUNC(length1));
     GetArcCoords(ArcCoords);
     setcolor(7);
     with ArcCoords do Line(Bx1,400, Xend, Yend);
     with ArcCoords do Line(Bx2,400, Xend, Yend);
     readln;
     CloseGraph;
end;{DrawTri}

PROCEDURE GetAngle(VAR hyp,side1,side2 : INTEGER);
VAR toobig : boolean;
    length1,length2 : REAL;
BEGIN
     REPEAT
           CLRSCR;
           toobig:=false;
           WRITELN(output,'Please enter the integer angle of one of the sides.');
           READLN(input,hyp);
           WRITELN(output,'Please enter the integer angle of one of the sides.');
           READLN(input,side1);
           side2:=(180-(hyp+side1));
           length1:=(size/sin(hyp*(pi/180)))*sin(side1*(pi/180));
           length2:=(size/sin(hyp*(pi/180)))*sin(side2*(pi/180));
     UNTIL ((size<(length1+length2)) AND (length1<(size+length2)) AND (length2<(size+length1)));
END;

PROCEDURE DRAWTRI;

VAR hyp,side1,side2 : INTEGER; {Defines hyp,side1, & side2 as integers}

BEGIN
           side1:=0;
           side2:=0;
           hyp:=0;
           GetAngle(hyp,side1,side2);
           drawpic(side1,side2,hyp);
END;


PROCEDURE DrawRect; {Define procedure DrawSquare}

VAR {Define variable of procedure DrawSquare}
  gd,gm,sizex,sizey,x,y : INTEGER; {Define gd,gm,sizex,sizey,x, & y as integers}

BEGIN {Begin procedure DrawSquare}
     REPEAT {Enter REPEAT Loop}
            CLRSCR; {Clear the screen}
            WRITELN(OUTPUT,'What length do you want a x side to be (max 639)?'); {Write out message}
            READLN(INPUT,sizex); {Read in sizex from keyboard}
     UNTIL ((sizex>0) AND (sizex<640)); {Repeat until 0<sizex<640}

     REPEAT {Enter REPEAT Loop}
            CLRSCR; {Clear the screen}
            WRITELN(OUTPUT,'What length do you want a y side to be (max 479)?'); {Write out message}
            READLN(INPUT,sizey); {Read in sizey from keyboard}
     UNTIL ((sizey>0) AND (sizey<480)); {Repeat until 0<sizey<480}

     REPEAT {Enter REPEAT Loop}
            WRITELN(OUTPUT,'Please enter the center x coordinate of the square in the range of '); {Write out message}
            WRITELN(OUTPUT,1+TRUNC(sizex/2),' to ',639-TRUNC(sizex/2)); {Write out message}
            READLN(INPUT,x); {Read in x from keyboard}
     UNTIL ((x>=(1+TRUNC(sizex/2))) AND (x<=(639-TRUNC(sizex/2)))); {REPEAT until (1+TRUNC(sizex/2)<x<(639-TRUNC(sizex/2))}

     REPEAT {Enter REPEAT Loop}
            WRITELN(OUTPUT,'Please enter the center y coordinate of the square in the range of '); {Write out message}
            WRITELN(OUTPUT,1+TRUNC(sizey/2),' to ',479-TRUNC(sizey/2)); {Write out message}
            READLN(INPUT,y); {Read in y from keyboard}
     UNTIL ((y>=(1+TRUNC(sizey/2))) AND (y<=(479-TRUNC(sizey/2)))); {REPEAT until (1+TRUNC(sizey/2)<y<(479-TRUNC(sizey/2))}

     CLRSCR; {Clear the screen}
     WRITELN(OUTPUT,'Press ENTER to view drawing and ENTER when done viewing'); {Write out message}
     READLN(INPUT); {Get ENTER from keyboard}
     gd := DETECT; {Detect graphics adapter}
     INITGRAPH(gd, gm, ''); {Initalize graphics adapter}
     IF GRAPHRESULT <> grOk THEN HALT(1); {If error the halt}
     RECTANGLE((x+TRUNC(sizex/2)),(y+TRUNC(sizey/2)),(x-TRUNC(sizex/2)),(y-TRUNC(sizey/2))); {Draw a rectangle}
     READLN(INPUT); {Get ENTER from Keyboard}
     CLOSEGRAPH; {Exit graphics mode}
END; {End procedure DrawRect}


PROCEDURE DrawCircle; {Define a procedure called DrawCircle}

VAR {Variable of Procedure DrawCircle}
  gd,gm,radius,x,y : INTEGER; {Define gd,gm,radius,x, & y as integers}

BEGIN {Beginning of Procedure DrawCircle}
     REPEAT {Enter REPEAT Loop}
           radius:=0; {Let the radius be 0}
           CLRSCR; {Clear the screen}
           WRITELN(OUTPUT,'Please enter the integer radius of the circle (max 239)'); {Write out message}
           READLN(INPUT,radius); {Read in new radius from keyboard}
     UNTIL ((radius>0) AND (radius<240)); {Repeat loop until 0<radius<240}

     REPEAT {Enter REPEAT Loop}
           x:=0; {Let x be 0}
           WRITELN(OUTPUT,'Please enter the center X coordinate in the range of'); {Write out message}
           WRITELN(OUTPUT,1+radius,' to ',639-radius); {Write out message}
           READLN(INPUT,x); {Read in new x from keyboard}
     UNTIL ((x>=(1+radius)) AND (x<=(639-radius))); {Repeat loop until  1+radius<x<639-radius}

     REPEAT {Enter REPEAT Loop}
           y:=0; {Let y be 0}
           WRITELN(OUTPUT,'Please enter the center Y coordinate in the range of'); {Write out message}
           WRITELN(OUTPUT,1+radius,' to ',479-radius); {Write out message}
           READLN(INPUT,y); {Read in new y from keyboard}
     UNTIL ((y>=(1+radius)) AND (y<=(479-radius))); {Repeat loop until 1+radius<y<479-radius}

     CLRSCR; {Clear the screen}
     WRITELN(OUTPUT,'Press ENTER to draw the circle and ENTER when done viewing it'); {Write out message}
     READLN(INPUT); {Get ENTER from keyboard}
     gd := DETECT; {Detect graphics adapter}
     INITGRAPH(gd, gm, ''); {Initilize graphics adapter}
     IF GRAPHRESULT <> grOk THEN HALT(1); {If there is an error then halt}
     ARC(x,y,0,360,radius); {Draw a 360 degree arc with center x,y and radius radius}
     READLN(INPUT); {GET ENTER from keyboard}
     CLOSEGRAPH; {Exit graphics mode}
END; {End of Procedure DrawCircle}

BEGIN {Begin Main Program}
      REPEAT {Enter REPEAT Loop}
            CHOICE:=' '; {Set choice=CHR(32)}
            CLRSCR; {Clear the screen}
            WRITELN(OUTPUT,'1 to draw a circle'); {Write out message}
            WRITELN(OUTPUT,'2 to draw a rectangle'); {Write out message}
            WRITELN(OUTPUT,'3 to draw a triangle'); {Write out message}
            WRITELN(OUTPUT,'9 to exit program'); {Write out message}
            WRITELN(OUTPUT); {Write out blank line}
            READLN(INPUT,choice); {Read in choice from keyboard}
            CASE choice[1] OF {Check the result of choice}
                 '1' : DRAWCIRCLE; {If choice=1 then drawcircle}
                 '2' : DRAWRECT; {If choice=2 then drawrect}
                 '3' : DRAWTRI; {If choice=3 then drawtri}
            END;{Done checking choice}
      UNTIL choice='9'; {REPEAT until choice=9}
END. {End of Main Program}
