program drehrech;
{Rotating rectangle/square with filled circle in background}
uses crt, graph;

var gd, gm, i, j, farbe, radius, dreh, anz : integer;
    recht : array[1..4] of record            {Saved points of square}
                             xv, yv : integer;
                             x, y : integer;
                             winkel : real;
                            end;

procedure bild;
  begin
    setcolor(farbe);                         {Draw filled square}
    line(recht[1].x, recht[1].y, recht[2].x, recht[2].y);
                                             {with precalculated points}
    line(recht[2].x, recht[2].y, recht[3].x, recht[3].y);
    line(recht[3].x, recht[3].y, recht[4].x, recht[4].y);
    line(recht[4].x, recht[4].y, recht[1].x, recht[1].y);
    setcolor(farbe); setfillstyle(1, farbe);
    floodfill(320, 240, farbe);
  end;

procedure bild2;
  begin                                      {Draw simple square}
    line(recht[1].xv, recht[1].yv, recht[2].xv, recht[2].yv);
                                             {with precalculated points}
    line(recht[2].xv, recht[2].yv, recht[3].xv, recht[3].yv);
    line(recht[3].xv, recht[3].yv, recht[4].xv, recht[4].yv);
    line(recht[4].xv, recht[4].yv, recht[1].xv, recht[1].yv);
  end;

procedure einstell(x: integer);

  var y : integer;

  begin
    {if x>100 then x:=round(x/100);}
    y:=x;                                    {square: width = height}
    recht[1].x:=320-x;                       {calculate square points from}
    recht[1].y:=240-y;                       {center of screen}
    recht[3].x:=320+x;
    recht[3].y:=240+y;
    recht[2].x:=recht[3].x;
    recht[2].y:=recht[1].y;
    recht[4].x:=recht[1].x;
    recht[4].y:=recht[3].y;
    for i:=1 to 4 do recht[i].winkel:=Pi/4+Pi/2*(i-1);
                                             {calculate start angle(45, 135...}
    radius:=round(x*sqrt(2));                {Radius for calculation}
  end;

procedure drehenberech;

  var wink : real;

  begin
    for i:=1 to 4 do
      begin                                  {Rotate in 1 degree steps}
        recht[i].winkel:=recht[i].winkel+Pi/180;
        wink:=recht[i].winkel;
        recht[i].xv:=recht[i].x;             {Save old points}
        recht[i].yv:=recht[i].y;
        recht[i].x:=320+round(cos(wink)*radius);
                                             {Rotate new points}
        recht[i].y:=240+round(sin(wink)*radius);
      end;
  end;

begin
  dreh:=0;                                   {Init rotation count}
  randomize;                                 {Init random generator}
  detectgraph(gd, gm);                       {Start graphic driver}
  initgraph(gd, gm, 'd:\tp\bgi');
  anz:=1;                                    {Start count for rectangles}
  einstell(round(exp(anz*ln(0.65))*240));    {First size of rectangle}
  farbe:=random(15)+1;                       {First color for rctangle}
  setcolor(farbe);
  repeat
    bild;                                    {Draw rectangle}
    delay(50);                               {Waiting time}
    drehenberech;                            {calculate rotation}
    setcolor(0);                             {Background color}
    inc(dreh);                               {Count rotations}
    bild2;                                   {Delete old rectangle}
    setcolor(farbe);                         {Set draw color after deletion}
    if (dreh=360) then anz:=anz+1;           {After 1 full turn count squares}
    if (dreh=360) and (anz=11) then          {After 11 rectangles start new}
      begin
        anz:=1;                              {Init rectangle count}
        delay(100);                          {Waiting time}
        cleardevice;                         {Clear screen}
      end;
    if dreh=360 then einstell(round(exp(anz*ln(0.65))*240));
                                             {Next size of rectangle (smaller)}
    if dreh=360 then farbe:=random(15)+1;    {Next color of rectangle}
    if dreh=360 then dreh:=0;                {Init rotation count}
  until keypressed;                          {End with any key}
  closegraph;                                {Back to text mode}
end.
