Advertisement
THOMAS_SHELBY_18

third method (1 point + 1point on arc)

Mar 24th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 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 = 5000;
  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 GetRandomCoordOnCircumference (var XPoint: Real; var YPoint: Real);
  61. var
  62. Coef: Integer;
  63. begin
  64. Coef := Random(2);
  65. if Coef = 1 then
  66. begin
  67. XPoint := Random(RADIUS);
  68. YPoint := Trunc(Sqrt(Sqr(RADIUS) - Sqr(XPoint)));
  69. end
  70. else
  71. begin
  72. YPoint := Random(RADIUS);
  73. XPoint := Trunc(Sqrt(Sqr(RADIUS) - Sqr(YPoint)))
  74. end;
  75.  
  76. Coef := Random(2);
  77. if Coef = 1 then
  78. XPoint := XCenter - Abs(XPoint)
  79. else
  80. XPoint := XCenter + Abs(XPoint);
  81.  
  82. Coef := Random(2);
  83. if Coef = 1 then
  84. YPoint := YCenter - Abs(YPoint)
  85. else
  86. YPoint := YCenter + Abs(YPoint);
  87. end;
  88.  
  89. procedure ConnectChord(X0, Y0: Real; Delta: Real);
  90. var
  91. XPoint1, YPoint1, XPoint2, YPoint2, Coef: Integer;
  92. XL, XR, YL, YR, K, K1, K2, TempX, TempY, X1, Y1, X2, Y2, B, A, C, ChordLength: Real;
  93. IsOnArc: Boolean;
  94. begin
  95. try
  96. X0 := X0 - XCenter;
  97. Y0 := Y0 - YCenter;
  98.  
  99. TempX := (X0*Sqr(RADIUS) - RADIUS*Sqrt(Sqr(X0*RADIUS) - (Sqr(X0)+Sqr(Y0))*(Sqr(RADIUS) - Sqr(Y0))))/(Sqr(X0)+Sqr(Y0));
  100. TempY := Sqrt(Sqr(RADIUS) - Sqr(TempX));
  101. K1 := (TempY - Y0)/(TempX - X0);
  102. if ((K1 * TempY/TempX) > -0.99) Or ((K1 * TempY/TempX) < -1.01) then
  103. begin
  104. TempY := -Sqrt(Sqr(RADIUS) - Sqr(TempX));
  105. K1 := (TempY - Y0)/(TempX - X0);
  106. end;
  107.  
  108. X1 := TempX;
  109. Y1 := TempY;
  110.  
  111. TempX := (X0*Sqr(RADIUS) + RADIUS*Sqrt(Sqr(X0*RADIUS) - (Sqr(X0)+Sqr(Y0))*(Sqr(RADIUS) - Sqr(Y0))))/(Sqr(X0)+Sqr(Y0));
  112. TempY := Sqrt(Sqr(RADIUS) - Sqr(TempX));
  113. K2 := (TempY - Y0)/(TempX - X0);
  114. if ((K2 * TempY/TempX) > -0.99) Or ((K2 * TempY/TempX) < -1.01) then
  115. begin
  116. TempY := -Sqrt(Sqr(RADIUS) - Sqr(TempX));
  117. K2 := (TempY - Y0)/(TempX - X0);
  118. end;
  119.  
  120. X2 := TempX;
  121. Y2 := TempY;
  122.  
  123. repeat
  124. IsOnArc := False;
  125. GetRandomCoordOnCircumference(TempX, TempY);
  126.  
  127. MainForm.ChordImage.Canvas.Pen.Color := clRed;
  128. MainForm.ChordImage.Canvas.Ellipse(Trunc(TempX)-2, Trunc(TempY)-2, Trunc(TempX)+2, Trunc(TempY)+2);
  129.  
  130. TempX := TempX - XCenter;
  131. TempY := TempY - YCenter;
  132.  
  133. if (Sqrt(Sqr(TempX-X0) + Sqr(TempY - Y0)) < (Sqrt(Sqr(X1-X0) + Sqr(Y1 - Y0)))) then
  134. IsOnArc := True;
  135. until IsOnArc;
  136. {if Y0 < 0 then
  137. K := Tan(Arctan(K1) - Delta)
  138. else
  139. K := Tan(Arctan(K1) + Delta);}
  140.  
  141.  
  142. K := (TempY - Y0)/(TempX - X0);
  143. B := Y0 - K*X0;
  144. A := 1 + K*K;
  145.  
  146. XL := (-K*B - Sqrt(K*K*RADIUS*RADIUS - B*B + RADIUS*RADIUS)) / A;
  147. XR := (-K*B + Sqrt(K*K*RADIUS*RADIUS - B*B + RADIUS*RADIUS)) / A;
  148.  
  149. YL := K*XL + B;
  150. YR := K*XR + B;
  151.  
  152. ChordLength := Sqrt(Sqr(XL - XR) + Sqr(YL-YR));
  153.  
  154. XPoint1 := Trunc (XL + XCenter);
  155. YPoint1 := Trunc (YL + YCenter);
  156. XPoint2 := Trunc (XR + XCenter);
  157. YPoint2 := Trunc (YR + YCenter);
  158.  
  159. with MainForm.ChordImage.Canvas do
  160. begin
  161. Pen.Color := clBlue;
  162. MoveTo(XPoint1, YPoint1);
  163. LineTo(XPoint2, YPoint2);
  164. end;
  165.  
  166. if ChordLength > Side then
  167. Inc(LongChordCount);
  168. Inc(ChordCount);
  169. except
  170. exit;
  171. end;
  172. end;
  173.  
  174. function CalculateAlpha(X, Y: Real): Real;
  175. begin
  176. CalculateAlpha := 2 * Arcsin(RADIUS / Sqrt((Sqr(X - XCenter) + Sqr(Y - YCenter))));
  177. end;
  178.  
  179. procedure DrawChord;
  180. var
  181. XPoint, YPoint, XCyclePoint, YCyclePoint: Integer;
  182. Alpha, Delta: Real;
  183. begin
  184. with MainForm.ChordImage.Canvas do
  185. begin
  186. GetRandomCoordOutsideCircle(XPoint, YPoint);
  187. Pen.Color := clBlack;
  188. Ellipse(XPoint-2, YPoint-2, XPoint+2, YPoint+2);
  189.  
  190. {GetRandomCoordOnCircumference(XCyclePoint, YCyclePoint);
  191. Pen.Color := clRed;
  192. Ellipse(XCyclePoint-2, YCyclePoint-2, XCyclePoint+2, YCyclePoint+2);}//первый метод
  193.  
  194. Alpha := CalculateAlpha(XPoint, YPoint);
  195. Delta := Random(Trunc(Alpha * 100000000)) / 100000000;
  196.  
  197.  
  198. ConnectChord(XPoint, YPoint, Delta);
  199. end;
  200. end;
  201.  
  202. procedure TMainForm.FormCreate(Sender: TObject);
  203. begin
  204. Randomize;
  205. XCenter := MainForm.ChordImage.Width Div 2;
  206. YCenter := MainForm.ChordImage.Height Div 2;
  207.  
  208. ChordCount := 0;
  209. LongChordCount := 0;
  210. Side := RADIUS * Sqrt(3);
  211. end;
  212.  
  213. procedure TMainForm.RunButtonClick(Sender: TObject);
  214. var
  215. I: Integer;
  216. begin
  217. with ChordImage do
  218. begin
  219. Canvas.Ellipse(XCenter - RADIUS, YCenter - RADIUS, XCenter + RADIUS, YCenter + RADIUS);
  220. Canvas.Pen.Color := clBlack;
  221. end;
  222. I := 0;
  223. repeat
  224. DrawChord;
  225. Inc(I);
  226. until I = 1000;
  227.  
  228. NumberLabel.Caption := IntToStr(LongChordCount) + '/' + IntToStr(ChordCount) + '=' + FloatToStr(LongChordCount/ChordCount);
  229. end;
  230.  
  231. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement