Advertisement
THOMAS_SHELBY_18

second method (1 point and угол с касательной)

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