Uses
 Crt, VGraph, Mouse;

Type
 Point = record
	  x,y,fx,fy: Real;
	  f: Boolean
	 end;

Var
 p: Array [1..1000] of Point;
 r,m,k,z,mg: Real;
 i,v,mc,kf,j,len,n,rad: Integer;
 mr: Boolean;
 ch: Char;
 f: Text;


Procedure DrawRope;
 var
  i: Byte;
 begin
  SetColor(1);
   for i:=1 to n-1
   do Line(Round(p[i].x),Round(p[i].y),Round(p[i+1].x),Round(p[i+1].y));
  for i:=1 to n
  do begin
       if (p[i].f)
       then SetColor(2)
       else SetColor(1);
      FillEllipse(Round(p[i].x),Round(p[i].y),rad,rad)
     end
 end; {DrawRope}

Procedure kx(var a,b: Point);
 var
  af,bf,t,l,x,y: Real;
  px,py: ShortInt;
 begin
   l:=sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
   if (a.x-b.x) <> 0
   then px:=Round(abs(a.x-b.x)/(a.x-b.x))
   else px:=0;
    if (a.y-b.y) <> 0
    then py:=Round(abs(a.y-b.y)/(a.y-b.y))
    else py:=0;
  x:=sqrt(sqr(a.x-b.x)+sqr(a.y-b.y)) - r;
  t:=k*x/2/m;
  if l = 0
  then begin
	x:=1;
	y:=1;
	l:=1
       end
  else begin
	x:=abs(a.x-b.x);
	y:=abs(a.y-b.y)
       end;
  a.fx:=a.fx - px*(x/l)*t;
  a.fy:=a.fy - py*(y/l)*t;
  b.fx:=b.fx + px*(x/l)*t;
  b.fy:=b.fy + py*(y/l)*t
 end; {kx}

{****************************************************************************}

Begin
 Assign(f,'config.txt');
 Reset(f);
 ReadLn(f,len);
 ReadLn(f,n);
 ReadLn(f,rad);
 ReadLn(f,m);
 ReadLn(f,k);
 ReadLn(f,z);
 ReadLn(f,mg);
 ReadLn(f,kf);
 for i:=1 to kf
 do begin
     Read(f,j);
     Read(f,p[j].x);
     ReadLn(f,p[j].y);
     p[j].f:=true
    end;
 Close(f);

 LimitX(0,639);
 LimitY(0,479);

 InitVesa(V640X480X256);
 v:=0;
 SetVisualPage(0);

 r:=len/n;
 mr:=true;


 REPEAT
  Inc(v);
   if v > 5
   then v:=1;
  SetActivePage(v);

  ClearDevice;

  SetColor(15);
  SetTextStyle(2,0,5);
  OutTextXY(470,460,'Made by Brave General');

  DrawRope;
  Scan;
  SetColor(4);
  Line(mouse.x,mouse.y-3,mouse.x,mouse.y+3);
  Line(mouse.x-3,mouse.y,mouse.x+3,mouse.y);

  SetVisualPage(v);

  for i:=1 to n
  do p[i].fy:=p[i].fy + mg/n;

  for i:=1 to n-1
  do kx(p[i],p[i+1]);

  for i:=1 to n
  do if p[i].f
     then begin
	   p[i].fx:=0;
	   p[i].fy:=0
	  end;

  for i:=1 to n
  do begin
      p[i].fx:=z*p[i].fx;
      p[i].fy:=z*p[i].fy;
      Scan;

      if not((mouse.left)or(mouse.right)or(mouse.both))
      then mr:=true;

      if (sqrt(sqr(mouse.x-p[i].x)+sqr(mouse.y-p[i].y)) <= rad)
      then if mouse.left
	   then if mc = 0
		then mc:=i
		else
	   else if (mouse.right)and(mr)
		then begin
		      p[i].f:=not(p[i].f);
		       if p[i].f
		       then Inc(kf)
		       else Dec(kf);
		      mouse.right:=false;
		      mr:=false
		     end;

      if mc = i
      then begin
	    p[i].x:=mouse.x;
	    p[i].y:=mouse.y;
	    p[i].fx:=0;
	    p[i].fy:=0
	   end
      else begin
	    p[i].x:=p[i].x+p[i].fx;
	    p[i].y:=p[i].y+p[i].fy;

	    if p[i].x < rad
	    then p[i].x:=rad;
	    if p[i].x > 639-rad
	    then p[i].x:=639-rad;
	    if p[i].y < rad
	    then p[i].y:=rad;
	    if p[i].y > 479-rad
	    then p[i].y:=479-rad;
	   end;

      if not(mouse.left)
      then mc:=0;

     end;

  if KeyPressed
  then ch:=ReadKey;

  if UpCase(ch) = 'Q'
  then for i:=1 to n
       do p[i].f:=false;

{  if UpCase(ch) = 'S'
  then begin
	Assign(f,'3.txt');
	ReWrite(f);
	WriteLn(f,len);
	WriteLn(f,n);
	WriteLn(f,rad);
	WriteLn(f,m);
	WriteLn(f,k);
	WriteLn(f,z);
	WriteLn(f,mg);
	WriteLn(f,kf);
	 for i:=1 to n
	 do if p[i].f
	    then WriteLn(f,i,' ',Round(p[i].x),' ',Round(p[i].y));
	Close(f)
       end;}

 UNTIL ch = #27;

 CloseVesa
End.