Advertisement
THOMAS_SHELBY_18

Fifth method (1 point + 1 point on diametr)

Mar 25th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. unit ChordUnit;
  2.  
  3. interface
  4.  
  5. uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;
  8.  
  9. type
  10. TMainForm = class(TForm)
  11. ChordImage: TImage;
  12. RunButton: TButton;
  13. AnswerLabel: TLabel;
  14. NumberLabel: TLabel;
  15. procedure RunButtonClick(Sender: TObject);
  16. procedure FormCreate(Sender: TObject);
  17. private
  18. { Private declarations }
  19. public
  20. { Public declarations }
  21. end;
  22.  
  23. var
  24. MainForm: TMainForm;
  25.  
  26. implementation
  27. uses
  28. Math;
  29. const
  30. RADIUS = 330;
  31. MAX_RANGE = High(Integer);
  32. var
  33. XCenter, YCenter: Word;
  34. ChordCount, LongChordCount: Integer;
  35. Side: Real;
  36. {$R *.dfm}
  37.  
  38. procedure GetRandomCoordOutsideCircle(var XPoint: Integer; var YPoint: Integer);
  39. var
  40. Coef: Integer;
  41. begin
  42. repeat
  43. XPoint := Random(MAX_RANGE);
  44. YPoint := Random(MAX_RANGE);
  45. until Sqr(XPoint) + Sqr(YPoint) > Sqr(RADIUS);
  46.  
  47. Coef := Random(2);
  48. if Coef = 1 then
  49. XPoint := XCenter - Abs(XPoint)
  50. else
  51. XPoint := XCenter + Abs(XPoint);
  52.  
  53. Coef := Random(2);
  54. if Coef = 1 then
  55. YPoint := YCenter - Abs(YPoint)
  56. else
  57. YPoint := YCenter + Abs(YPoint);
  58. end;
  59.  
  60. procedure GetRandomCoordInCircle(var XPoint: Real; var YPoint: Real);
  61. var
  62. Coef: Integer;
  63. begin
  64. repeat
  65. XPoint := Random(RADIUS);
  66. YPoint := Random(RADIUS);
  67. until Sqr(XPoint) + Sqr(YPoint) < Sqr(RADIUS);
  68.  
  69. Coef := Random(2);
  70. if Coef = 1 then
  71. XPoint := XCenter - Abs(XPoint)
  72. else
  73. XPoint := XCenter + Abs(XPoint);
  74.  
  75. Coef := Random(2);
  76. if Coef = 1 then
  77. YPoint := YCenter - Abs(YPoint)
  78. else
  79. YPoint := YCenter + Abs(YPoint);
  80. end;
  81.  
  82. procedure GetRandomCoordOnCircumference (var XPoint: Real; var YPoint: Real);
  83. var
  84. Coef: Integer;
  85. begin
  86. Coef := Random(2);
  87. if Coef = 1 then
  88. begin
  89. XPoint := Random(RADIUS);
  90. YPoint := Trunc(Sqrt(Sqr(RADIUS) - Sqr(XPoint)));
  91. end
  92. else
  93. begin
  94. YPoint := Random(RADIUS);
  95. XPoint := Trunc(Sqrt(Sqr(RADIUS) - Sqr(YPoint)))
  96. end;
  97.  
  98. Coef := Random(2);
  99. if Coef = 1 then
  100. XPoint := XCenter - Abs(XPoint)
  101. else
  102. XPoint := XCenter + Abs(XPoint);
  103.  
  104. Coef := Random(2);
  105. if Coef = 1 then
  106. YPoint := YCenter - Abs(YPoint)
  107. else
  108. YPoint := YCenter + Abs(YPoint);
  109. end;
  110.  
  111. procedure GetRandomCoordInDiametr(var XPoint: Real; var YPoint: Real; X0, Y0: Real);
  112. var
  113. Coef: Integer;
  114. K: Real;
  115. begin
  116. K := (Y0-YCenter)/(X0-Xcenter);
  117. K := -1/K;
  118. repeat
  119. XPoint := Random(RADIUS);
  120. Coef := Random(2);
  121. if Coef = 1 then
  122. XPoint := -Abs(XPoint)
  123. else
  124. XPoint := Abs(XPoint);
  125.  
  126. YPoint := K*XPoint;
  127. until Sqr(XPoint) + Sqr(YPoint) < Sqr(RADIUS);
  128.  
  129. XPoint := XPoint + XCenter;
  130. YPoint := YPoint + YCenter;
  131.  
  132. {Coef := Random(2);
  133. if Coef = 1 then
  134. YPoint := YCenter - Abs(YPoint)
  135. else
  136. YPoint := YCenter + Abs(YPoint);}
  137. end;
  138.  
  139. procedure ConnectChord(X0, Y0: Real; Delta: Real);
  140. var
  141. XPoint1, YPoint1, XPoint2, YPoint2, Coef: Integer;
  142. XL, XR, YL, YR, K, TempX, TempY, X1, Y1, X2, Y2, B, A, C, ChordLength: Real;
  143. IsOnArc: Boolean;
  144. begin
  145. try
  146. X0 := X0 - XCenter;
  147. Y0 := Y0 - YCenter;
  148.  
  149. GetRandomCoordInDiametr(TempX, TempY, X0, Y0);
  150. TempX := TempX - XCenter;
  151. TempY := TempY - YCenter;
  152.  
  153. K := (Y0 - TempY)/(X0-Tempx);
  154. B := Y0 - K*X0;
  155. A := 1 + K*K;
  156.  
  157. XL := (-K*B - Sqrt(K*K*RADIUS*RADIUS - B*B + RADIUS*RADIUS)) / A;
  158. XR := (-K*B + Sqrt(K*K*RADIUS*RADIUS - B*B + RADIUS*RADIUS)) / A;
  159.  
  160. YL := K*XL + B;
  161. YR := K*XR + B;
  162.  
  163. ChordLength := Sqrt(Sqr(XL - XR) + Sqr(YL-YR));
  164.  
  165. XPoint1 := Trunc (XL + XCenter);
  166. YPoint1 := Trunc (YL + YCenter);
  167. XPoint2 := Trunc (XR + XCenter);
  168. YPoint2 := Trunc (YR + YCenter);
  169.  
  170. with MainForm.ChordImage.Canvas do
  171. begin
  172. Pen.Color := clBlue;
  173. MoveTo(XPoint1, YPoint1);
  174. LineTo(XPoint2, YPoint2);
  175. end;
  176.  
  177. if ChordLength > Side then
  178. Inc(LongChordCount);
  179. Inc(ChordCount);
  180. except
  181. exit;
  182. end;
  183. end;
  184.  
  185. function CalculateAlpha(X, Y: Real): Real;
  186. begin
  187. CalculateAlpha := 2 * Arcsin(RADIUS / Sqrt((Sqr(X - XCenter) + Sqr(Y - YCenter))));
  188. end;
  189.  
  190. procedure DrawChord;
  191. var
  192. XPoint, YPoint, XCyclePoint, YCyclePoint: Integer;
  193. Alpha, Delta: Real;
  194. begin
  195. with MainForm.ChordImage.Canvas do
  196. begin
  197. GetRandomCoordOutsideCircle(XPoint, YPoint);
  198. Pen.Color := clBlack;
  199. Ellipse(XPoint-2, YPoint-2, XPoint+2, YPoint+2);
  200.  
  201. {GetRandomCoordOnCircumference(XCyclePoint, YCyclePoint);
  202. Pen.Color := clRed;
  203. Ellipse(XCyclePoint-2, YCyclePoint-2, XCyclePoint+2, YCyclePoint+2);}//первый метод
  204.  
  205. Alpha := CalculateAlpha(XPoint, YPoint);
  206. Delta := Random(Trunc(Alpha * 100000000)) / 100000000;
  207.  
  208.  
  209. ConnectChord(XPoint, YPoint, Delta);
  210. end;
  211. end;
  212.  
  213. procedure TMainForm.FormCreate(Sender: TObject);
  214. begin
  215. Randomize;
  216. XCenter := MainForm.ChordImage.Width Div 2;
  217. YCenter := MainForm.ChordImage.Height Div 2;
  218.  
  219. ChordCount := 0;
  220. LongChordCount := 0;
  221. Side := RADIUS * Sqrt(3);
  222. end;
  223.  
  224. procedure TMainForm.RunButtonClick(Sender: TObject);
  225. var
  226. I: Integer;
  227. begin
  228. with ChordImage do
  229. begin
  230. Canvas.Ellipse(XCenter - RADIUS, YCenter - RADIUS, XCenter + RADIUS, YCenter + RADIUS);
  231. Canvas.Pen.Color := clBlack;
  232. end;
  233. I := 0;
  234. repeat
  235. DrawChord;
  236. Inc(I);
  237. until I = 1000;
  238.  
  239. NumberLabel.Caption := IntToStr(LongChordCount) + '/' + IntToStr(ChordCount) + '=' + FloatToStr(LongChordCount/ChordCount);
  240. end;
  241.  
  242. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement