CENMKEY.PAS 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. Uses Crt,Graph;
  2. Const
  3. MaxPoints = 10;
  4. Path = '';
  5. Type TPoints = record
  6. x,y,massa : Word;
  7. end;
  8. Var
  9. points : array[1..MaxPoints] of ^TPoints;
  10. center : PointType;
  11. circles : array [1..9] of Pointer;
  12. Size : array [1..9] of Word;
  13. CenterM :Pointer;
  14. SizeC : Word;
  15. mod_num : Word;
  16. num_of_point : byte;
  17. ch : char;
  18. i : Integer;
  19. {-------------------------------------------}
  20. Function TestPos(x1,y1,m1:Integer) : boolean;
  21. Var
  22. i:integer;
  23. begin
  24. TestPos := true;
  25. for i := 1 to MaxPoints do
  26. with Points[i]^ do if (x1=x) and (y1=y) then TestPos:=false;
  27. end;
  28. {----------------------------------------------}
  29. Procedure FindCenter;
  30. Var
  31. i:Integer;
  32. summaX,SummaY,smas:real;
  33. Begin
  34. SummaX:=0;
  35. SummaY:=0;
  36. Smas:=0;
  37. for i:= 1 to MaxPoints do
  38. begin
  39. summaX := SummaX + points[i]^.massa * points[i]^.x;
  40. summaY := SummaY + points[i]^.massa * points[i]^.Y;
  41. smas := smas + points[i]^.massa;
  42. end;
  43. center.x := round(summaX/smas);
  44. center.y := round(summaY/smas);
  45. End;{FindCenter}
  46. {--------------------------------------------}
  47. Procedure SetPoints;
  48. Var
  49. i,x1,y1,massa1 : Integer;
  50. Begin
  51. for i := 1 to MaxPoints do
  52. begin
  53. New(points[i]);
  54. repeat
  55. begin
  56. Y1 := random(400)+40;
  57. X1 := random(560)+40;
  58. massa1 := random(8)+1;
  59. end;
  60. until TestPos(x1,y1,massa1);
  61. with points[i]^ do
  62. begin
  63. x := x1;
  64. y := y1;
  65. massa := massa1;
  66. end;
  67. end;
  68. End;{SetPoints}
  69. {-----------------------------------------}
  70. Procedure InitG;
  71. Var
  72. Gm,Gd : Integer;
  73. Begin
  74. Gd := VGA; Gm := VGAhi;
  75. InitGraph(Gd, Gm,Path);
  76. if GraphResult <> grOk then
  77. begin
  78. WriteLn(GraphErrorMsg(GraphResult));
  79. Halt(1);
  80. end
  81. else
  82. begin
  83. SetColor(blue);
  84. Line(40,440,600,440);
  85. Line(40,40,40,440);
  86. Line(35,340,45,340);
  87. Line(140,435,140,445);
  88. SetColor(White);
  89. SetTextStyle(SmallFont,0,4);
  90. SetTextJustify(Centertext,TopText);
  91. OutTextXY(140,448,'100');
  92. OutTextXY(590,450,'X');
  93. OutTextXY(35,450,'0');
  94. SetTextJustify(RightText,CenterText);
  95. OutTextXY(38,52,'Y');
  96. OutTextXY(33,340,'100');
  97. SetColor(White);
  98. for i := 1 to 9 do
  99. begin
  100. Circle(100,100,i);
  101. Size[i] := ImageSize(100-i,100-i,100+i,100+i);
  102. GetMem(circles[i],Size[i]);
  103. GetImage(100-i,100-i,100+i,100+i,circles[i]^);
  104. PutImage(100-i,100-i,circles[i]^,XORput);
  105. end;
  106. SetFillStyle(SolidFill,Red);
  107. FillEllipse(100,100,4,4);
  108. SizeC := ImageSize(96,96,104,104);
  109. GetMem(CenterM,SizeC);
  110. GetImage(96,96,104,104,CenterM^);
  111. PutImage(96,96,CenterM^,XORPut);
  112. end;
  113. end;{InitG}
  114. {--------------------------------------------}
  115. Procedure MovePoint(n,dir:byte);
  116. Begin
  117. PutImage(points[n]^.x-points[n]^.massa,points[n]^.y-points[n]^.massa,
  118. Circles[points[n]^.massa]^,XORPut);
  119. case dir of
  120. 1 : begin
  121. inc(Points[n]^.x);
  122. if Points[n]^.x + Points[n]^.massa > 601 then Points[n]^.x := 600 - Points[n]^.massa;
  123. PutImage(points[n]^.x-points[n]^.massa,points[n]^.y-points[n]^.massa,
  124. circles[points[n]^.massa]^,XORPut);
  125. End;
  126. 2 : begin
  127. inc(Points[n]^.y);
  128. if Points[n]^.y + Points[n]^.massa > 441 then Points[n]^.Y := 440 - Points[n]^.massa;
  129. PutImage(points[n]^.x-points[n]^.massa,points[n]^.y-points[n]^.massa,
  130. circles[points[n]^.massa]^,XORPut);
  131. end;
  132. 3 : begin
  133. dec(Points[n]^.x);
  134. if Points[n]^.x - Points[n]^.massa < 40 then Points[n]^.x := 40 + Points[n]^.massa;
  135. PutImage(points[n]^.x-points[n]^.massa,points[n]^.y-points[n]^.massa,
  136. circles[points[n]^.massa]^,XORPut);
  137. end;
  138. 4 : begin
  139. dec(Points[n]^.y);
  140. if Points[n]^.y - Points[n]^.massa < 40 then Points[n]^.Y := 40 + Points[n]^.massa;
  141. PutImage(points[n]^.x-points[n]^.massa,points[n]^.y-points[n]^.massa,
  142. circles[points[n]^.massa]^,XORPut);
  143. end;
  144. end;
  145. PutImage(center.x-4,center.y-4, CenterM^,XORPut);
  146. PutPixel(Center.x,Center.Y,Yellow);
  147. FindCenter;
  148. PutImage(center.x-4,center.y-4, CenterM^,XORPut);
  149. End;{MovePoint}
  150. {-----------------------------------------}
  151. Begin
  152. Num_OF_point :=1;
  153. Randomize;
  154. InitG;
  155. SetPoints;
  156. for i := 1 to MaxPoints do PutImage(points[i]^.x-points[i]^.massa,
  157. points[i]^.y-points[i]^.massa,
  158. Circles[points[i]^.massa]^,XorPut);
  159. FindCenter;
  160. PutImage(center.X-4,center.Y-4,CenterM^,XORPut);
  161. repeat
  162. ch := readkey;
  163. if ch = #0 then
  164. begin
  165. ch := readkey;
  166. case ch of
  167. #77 : MovePoint(num_of_point,1);
  168. #80 : MovePoint(num_of_point,2);
  169. #75 : MovePoint(num_of_point,3);
  170. #72 : MovePoint(num_of_point,4);
  171. end;
  172. end;
  173. if ch = #32 then
  174. begin
  175. inc(Num_of_point);
  176. if num_of_point = 11 then num_of_point := 1;
  177. end;
  178. if ch = #13 then
  179. begin
  180. for i := 1 to MaxPoints do PutImage(points[i]^.x-points[i]^.massa,
  181. points[i]^.y-points[i]^.massa,
  182. Circles[points[i]^.massa]^,XorPut);
  183. FindCenter;
  184. PutImage(center.X-4,center.Y-4,CenterM^,XORPut);
  185. for i := 1 to MaXPoints do with points[i]^ do
  186. begin
  187. X:=0;
  188. Y:=0;
  189. massa := 0;
  190. end;
  191. SetFillStyle(solidfill,black);
  192. Bar(45,41,600,430);
  193. SetPoints;
  194. FindCenter;
  195. for i := 1 to MaxPoints do PutImage(points[i]^.x-points[i]^.massa,
  196. points[i]^.y-points[i]^.massa,
  197. Circles[points[i]^.massa]^,XorPut);
  198. FindCenter;
  199. PutImage(center.X-4,center.Y-4,CenterM^,XORPut);
  200. end;
  201. until ch = #27;
  202. For i := 1 to MaxPoints do Dispose(Points[i]);
  203. For i := 1 to 9 do FreeMem(circles[i],size[i]);
  204. FreeMem(CenterM,SizeC);
  205. CloseGraph;
  206. End.