program hypocycloid;
{
   A hypocycloid is a figure created by rolling a circle inside another.
   this program is adapted from a program on p.97 of MS quickC manual.

   This source will produce the same program as HYPOCYCL.EXE, except
   this version will require the appropriate .BGI file.

   stephen peter 1988
}
uses Crt, Graph, Dos;
var
   Graphdriver, GraphMode      : integer;
   x0, y0, maxx, maxy, x, y, i : integer;
   colour, start_colour        : word;
   ratio, a, b, h, penpos, ang : real;
{-------------------------------------------------------------------}
begin
  GraphDriver := detect;
  initgraph (GraphDriver,GraphMode,'');

  maxx := GetMaxX;
  maxy := GetMaxY;
  x0 := trunc(maxx/2 -1);
  y0 := trunc(maxy/2 -1);

  repeat
     ClearDevice;
     randomize;
     ratio := random*10 + 1;
     penpos := random*10 +1;

     a := 0.5 * ratio * maxy / (ratio + penpos -1);
     b := a / ratio;
     h := penpos * b;

     start_colour := trunc(random*7)+1;
     ang := 0;
     while not keypressed do
         for i := 1 to 20 do
             begin
               ang := ang + 2*pi/100;
               x := trunc (x0 + (a-b)*cos(ang) + h*cos(ang*(a-b)/b));
               y := trunc (y0 - (a-b)*sin(ang) + h*sin(ang*(a-b)/b));
               colour := getpixel (x,y);
               if colour <> 0 then
                   begin
                     inc (colour);
                     if colour = 15 then colour := 1;
                   end
               else  colour := start_colour;
               putpixel (x,y,colour);
             end;
   until ReadKey in ['Q','q',#27];
   closegraph;
end.
