Advertisement
THOMAS_SHELBY_18

fourth method (1 point and 1 point in cycle)

Mar 24th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 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 ConnectChord(X0, Y0: Real; Delta: Real);
  112. var
  113. XPoint1, YPoint1, XPoint2, YPoint2, Coef: Integer;
  114. XL, XR, YL, YR, K, TempX, TempY, X1, Y1, X2, Y2, B, A, C, ChordLength: Real;
  115. IsOnArc: Boolean;
  116. begin
  117. try
  118. X0 := X0 - XCenter;
  119. Y0 := Y0 - YCenter;
  120.  
  121. GetRandomCoordInCircle(TempX, TempY);
  122. TempX := TempX - XCenter;
  123. TempY := TempY - YCenter;
  124.  
  125. K := (TempY - Y0)/(TempX - X0);
  126. B := Y0 - K*X0;
  127. A := 1 + K*K;
  128.  
  129. XL := (-K*B - Sqrt(K*K*RADIUS*RADIUS - B*B + RADIUS*RADIUS)) / A;
  130. XR := (-K*B + Sqrt(K*K*RADIUS*RADIUS - B*B + RADIUS*RADIUS)) / A;
  131.  
  132. YL := K*XL + B;
  133. YR := K*XR + B;
  134.  
  135. ChordLength := Sqrt(Sqr(XL - XR) + Sqr(YL-YR));
  136.  
  137. XPoint1 := Trunc (XL + XCenter);
  138. YPoint1 := Trunc (YL + YCenter);
  139. XPoint2 := Trunc (XR + XCenter);
  140. YPoint2 := Trunc (YR + YCenter);
  141.  
  142. with MainForm.ChordImage.Canvas do
  143. begin
  144. Pen.Color := clBlue;
  145. MoveTo(XPoint1, YPoint1);
  146. LineTo(XPoint2, YPoint2);
  147. end;
  148.  
  149. if ChordLength > Side then
  150. Inc(LongChordCount);
  151. Inc(ChordCount);
  152. except
  153. exit;
  154. end;
  155. end;
  156.  
  157. function CalculateAlpha(X, Y: Real): Real;
  158. begin
  159. CalculateAlpha := 2 * Arcsin(RADIUS / Sqrt((Sqr(X - XCenter) + Sqr(Y - YCenter))));
  160. end;
  161.  
  162. procedure DrawChord;
  163. var
  164. XPoint, YPoint, XCyclePoint, YCyclePoint: Integer;
  165. Alpha, Delta: Real;
  166. begin
  167. with MainForm.ChordImage.Canvas do
  168. begin
  169. GetRandomCoordOutsideCircle(XPoint, YPoint);
  170. Pen.Color := clBlack;
  171. Ellipse(XPoint-2, YPoint-2, XPoint+2, YPoint+2);
  172.  
  173. {GetRandomCoordOnCircumference(XCyclePoint, YCyclePoint);
  174. Pen.Color := clRed;
  175. Ellipse(XCyclePoint-2, YCyclePoint-2, XCyclePoint+2, YCyclePoint+2);}//первый метод
  176.  
  177. Alpha := CalculateAlpha(XPoint, YPoint);
  178. Delta := Random(Trunc(Alpha * 100000000)) / 100000000;
  179.  
  180.  
  181. ConnectChord(XPoint, YPoint, Delta);
  182. end;
  183. end;
  184.  
  185. procedure TMainForm.FormCreate(Sender: TObject);
  186. begin
  187. Randomize;
  188. XCenter := MainForm.ChordImage.Width Div 2;
  189. YCenter := MainForm.ChordImage.Height Div 2;
  190.  
  191. ChordCount := 0;
  192. LongChordCount := 0;
  193. Side := RADIUS * Sqrt(3);
  194. end;
  195.  
  196. procedure TMainForm.RunButtonClick(Sender: TObject);
  197. var
  198. I: Integer;
  199. begin
  200. with ChordImage do
  201. begin
  202. Canvas.Ellipse(XCenter - RADIUS, YCenter - RADIUS, XCenter + RADIUS, YCenter + RADIUS);
  203. Canvas.Pen.Color := clBlack;
  204. end;
  205. I := 0;
  206. repeat
  207. DrawChord;
  208. Inc(I);
  209. until I = 1000;
  210.  
  211. NumberLabel.Caption := IntToStr(LongChordCount) + '/' + IntToStr(ChordCount) + '=' + FloatToStr(LongChordCount/ChordCount);
  212. end;
  213.  
  214. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement