Advertisement
THOMAS_SHELBY_18

Chords, first method (1 point + 1 point)

Mar 23rd, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 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. const
  28. RADIUS = 330;
  29. MAX_RANGE = High(Integer);
  30. var
  31. XCenter, YCenter: Word;
  32. ChordCount, LongChordCount: Integer;
  33. Side: Real;
  34. {$R *.dfm}
  35.  
  36. procedure GetRandomCoordOutsideCircle(var XPoint: Integer; var YPoint: Integer);
  37. var
  38. Coef: Integer;
  39. begin
  40. repeat
  41. XPoint := Random(MAX_RANGE);
  42. YPoint := Random(MAX_RANGE);
  43. until Sqr(XPoint) + Sqr(YPoint) > Sqr(RADIUS);
  44.  
  45. Coef := Random(2);
  46. if Coef = 1 then
  47. XPoint := XCenter - Abs(XPoint)
  48. else
  49. XPoint := XCenter + Abs(XPoint);
  50.  
  51. Coef := Random(2);
  52. if Coef = 1 then
  53. YPoint := YCenter - Abs(YPoint)
  54. else
  55. YPoint := YCenter + Abs(YPoint);
  56. end;
  57.  
  58. procedure GetRandomCoordOnCircumference (var XPoint: Integer; var YPoint: Integer);
  59. var
  60. Coef: Integer;
  61. begin
  62. Coef := Random(2);
  63. if Coef = 1 then
  64. begin
  65. XPoint := Random(RADIUS);
  66. YPoint := Trunc(Sqrt(Sqr(RADIUS) - Sqr(XPoint)));
  67. end
  68. else
  69. begin
  70. YPoint := Random(RADIUS);
  71. XPoint := Trunc(Sqrt(Sqr(RADIUS) - Sqr(YPoint)))
  72. end;
  73.  
  74. Coef := Random(2);
  75. if Coef = 1 then
  76. XPoint := XCenter - Abs(XPoint)
  77. else
  78. XPoint := XCenter + Abs(XPoint);
  79.  
  80. Coef := Random(2);
  81. if Coef = 1 then
  82. YPoint := YCenter - Abs(YPoint)
  83. else
  84. YPoint := YCenter + Abs(YPoint);
  85. end;
  86.  
  87. procedure ConnectChord(X0, Y0, X1, Y1: Integer);
  88. var
  89. XL, XR, X, Y, Coef: Integer;
  90. K, C, A, ChordLength: Real;
  91. begin
  92. X0 := X0 - XCenter;
  93. Y0 := Y0 - YCenter;
  94. X1 := X1 - XCenter;
  95. Y1 := Y1 - YCenter;
  96.  
  97. if X1-X0 <> 0 then
  98. begin
  99. K := (Y1-Y0)/(X1-X0);
  100. C := Y0 - K*X0;
  101. A := 1 + K*K;
  102.  
  103. XL := Trunc((-K*C - Sqrt(K*K*RADIUS*RADIUS - C*C + RADIUS*RADIUS)) / A);
  104. XR := Trunc((-K*C + Sqrt(K*K*RADIUS*RADIUS - C*C + RADIUS*RADIUS)) / A);
  105.  
  106. if X1 > 0 then
  107. X := XL
  108. else
  109. X := XR;
  110.  
  111. Y := Trunc(Sqrt(RADIUS*RADIUS - X*X));
  112. Coef := Random(2);
  113. if Coef = 1 then
  114. Y := - Abs(Y)
  115. else
  116. Y := Abs(Y);
  117.  
  118. ChordLength := Sqrt(Sqr(X - X1) + Sqr(Y-Y1));
  119.  
  120. X := X + XCenter;
  121. Y := Y + YCenter;
  122. X1 := X1 + XCenter;
  123. Y1 := Y1 + YCenter;
  124.  
  125. with MainForm.ChordImage.Canvas do
  126. begin
  127. Pen.Color := clBlue;
  128. MoveTo(X, Y);
  129. LineTo(X1, Y1);
  130. end;
  131.  
  132. if ChordLength > Side then
  133. Inc(LongChordCount);
  134. Inc(ChordCount);
  135. end;
  136. end;
  137.  
  138. procedure DrawChord;
  139. var
  140. XPoint, YPoint, XCyclePoint, YCyclePoint: Integer;
  141. begin
  142. with MainForm.ChordImage.Canvas do
  143. begin
  144. GetRandomCoordOutsideCircle(XPoint, YPoint);
  145. Pen.Color := clBlack;
  146. Ellipse(XPoint-2, YPoint-2, XPoint+2, YPoint+2);
  147.  
  148. GetRandomCoordOnCircumference(XCyclePoint, YCyclePoint);
  149. Pen.Color := clRed;
  150. Ellipse(XCyclePoint-2, YCyclePoint-2, XCyclePoint+2, YCyclePoint+2);
  151.  
  152.  
  153. ConnectChord(XPoint, YPoint, XCyclePoint, YCyclePoint);
  154. end;
  155. end;
  156.  
  157. procedure TMainForm.FormCreate(Sender: TObject);
  158. begin
  159. Randomize;
  160. XCenter := MainForm.ChordImage.Width Div 2;
  161. YCenter := MainForm.ChordImage.Height Div 2;
  162.  
  163. ChordCount := 0;
  164. LongChordCount := 0;
  165. Side := RADIUS * Sqrt(3);
  166. end;
  167.  
  168. procedure TMainForm.RunButtonClick(Sender: TObject);
  169. var
  170. I: Integer;
  171. begin
  172. with ChordImage do
  173. begin
  174. Canvas.Ellipse(XCenter - RADIUS, YCenter - RADIUS, XCenter + RADIUS, YCenter + RADIUS);
  175. Canvas.Pen.Color := clBlack;
  176. end;
  177. I := 0;
  178. repeat
  179. DrawChord;
  180. Inc(I);
  181. until I = 1000;
  182.  
  183. NumberLabel.Caption := IntToStr(LongChordCount) + '/' + IntToStr(ChordCount) + '=' + FloatToStr(LongChordCount/ChordCount);
  184. end;
  185.  
  186. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement