program rueckwaerts;
uses crt, graph;

var i, ri, gas : shortint;
    x, y : integer;
    ch : char;
    gd, gm : integer;
    radius1, radius2 : integer;
    winkel : real;
    winkelseh : real;
    ende : boolean;
    recht : array[1..6] of record
                             xv, yv : integer;
                             x, y : integer;
                             winkel : real;
                            end;

procedure autozeich(farbe, mx, my: integer);
  begin
    setcolor(farbe);
    line(recht[1].x, recht[1].y, recht[2].x, recht[2].y);
    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(mx, my+1, farbe);
    setcolor(0);
    line(recht[5].x, recht[5].y, recht[6].x, recht[6].y);
  end;

procedure auto(mx, my, farbe: integer; winkel2: real);

  var x, x2, y, y2, i : integer;
      wink            : real;

  begin
    x:=50; y:=36;
    recht[1].x:=mx-x;                  {links oben}
    recht[1].y:=my-4*y;
    recht[3].x:=mx+x;                  {rechts unten}
    recht[3].y:=my+y;
    recht[2].x:=recht[3].x;            {links unten}
    recht[2].y:=recht[1].y;
    recht[4].x:=recht[1].x;            {rechts oben}
    recht[4].y:=recht[3].y;
    recht[5].x:=mx+round(3*y*cos(winkel2-Pi/2));
    recht[5].y:=my+round(3*y*sin(winkel2-Pi/2));
    recht[6].x:=round(mx+10*cos(winkelseh)+3*y*cos(winkel2-Pi/2));
    recht[6].y:=round(my+10*sin(winkelseh)+3*y*sin(winkel2-Pi/2));
    recht[2].winkel:=arctan(y/x);
    recht[3].winkel:=2*Pi-arctan(4*y/x);
    recht[1].winkel:=Pi-recht[2].winkel;
    recht[4].winkel:=-recht[3].winkel+3*Pi;
    radius1:=round(sqrt(x*x+4*y*4*y));
    radius2:=round(sqrt(x*x+y*y));
    for i:=1 to 4 do
      begin
        recht[i].winkel:=recht[i].winkel+winkel2;
        wink:=recht[i].winkel;
        recht[i].xv:=recht[i].x;
        recht[i].yv:=recht[i].y;
        if ((i=1) or (i=2)) then recht[i].x:=mx+round(cos(wink)*radius2)
                            else recht[i].x:=mx+round(cos(wink)*radius1);
        if ((i=1) or (i=2)) then recht[i].y:=my+round(sin(wink)*radius2)
                            else recht[i].y:=my+round(sin(wink)*radius1);
      end;
    autozeich(farbe, mx, my);
  end;

begin
  gd:=9; gm:=2;
  initgraph(gd, gm, 'd:\tp\bgi');
  cleardevice;
  setcolor(2); setfillstyle(1, 2);
  bar(0, 0, 639, 99);
  setcolor(8); setfillstyle(1, 8);
  bar(0, 100, 639, 479);
  (*auto(20, 320, 9, Pi/2);           {L„ngs}
  auto(580, 320, 3, Pi/2);
  auto(150, 320, 4, Pi/2);
  auto(440, 320, 5, Pi/2);*)

  auto(20, 443, 9, 0);        {Quer}
  auto(620, 443, 3, 0);
  auto(170, 443, 4, 0);
  auto(470, 443, 5, 0);

  setlinestyle(0, 0, 3);
  setcolor(15);
  line(95, 300, 95, 480);
  line(90, 300, 100, 300);
  line(245, 300, 245, 480);
  line(240, 300, 250, 300);
  line(395, 300, 395, 480);
  line(390, 300, 400, 300);
  line(545, 300, 545, 480);
  line(540, 300, 550, 300);
  setlinestyle(0, 0, 0);
  winkel:=0;
  ende:=false;
  x:=0;
  y:=0;
  gas:=0;
  ri:=1;
  ch:=chr(1);
  repeat
      if keypressed then ch:=readkey;
      if ch=chr(0) then
       begin
         ch:=readkey;
         if (ch=chr(72)) and (gas<10) then gas:=gas+1;
         if (ch=chr(80)) and (gas>0) then gas:=gas-1;
         if (ch=chr(75)) then winkel:=winkel-Pi/72;
         if (ch=chr(77)) then winkel:=winkel+Pi/72;
       end
       else begin
         if (ch='8') then winkelseh:=3*Pi/2;
         if (ch='4') then winkelseh:=Pi;
         if (ch='2') then winkelseh:=Pi/2;
         if (ch='6') then winkelseh:=0;
            end;
      if ch='r' then ri:=-1;
      if ch='v' then ri:=1;
      x:=x+round(gas*ri*cos(winkel));
      y:=y+round(gas*ri*sin(winkel));
      if (x>620) or (x<-20) then x:=x-round(gas*ri*sin(winkel));
      if (y<-220) or (y>260) then y:=y-round(gas*ri*cos(winkel));
      auto(20+x, 220+y, 6, winkel+Pi/2);
      delay(200);
      auto(20+x, 220+y, 8, winkel+Pi/2);
      if ch=chr(27) then ende:=true;
  until ende=true;
  delay(100);
  closegraph;
end.