Advertisement
THOMAS_SHELBY_18

single modulation

Mar 29th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 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 = 200;
  31. MAX_RANGE = 350;//High(Integer);
  32. var
  33. XCenter, YCenter: Word;
  34. ChordCount, LongChordCount: Integer;
  35. Side: Real;
  36. {$R *.dfm}
  37.  
  38. procedure GetRandomCoordOutsideCircle(var XPoint: Real; var YPoint: Real);
  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);
  112. var
  113. XPoint1, YPoint1, XPoint2, YPoint2, Coef: Integer;
  114. XL, XR, YL, YR, TempX, TempY, K, 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.  
  123. TempX := TempX - XCenter;
  124. TempY := TempY - YCenter;
  125.  
  126. K := (TempY - Y0)/(TempX - X0);
  127. B := Y0 - K*X0;
  128. A := 1 + K*K;
  129.  
  130. XL := (-K*B - Sqrt(K*K*RADIUS*RADIUS - B*B + RADIUS*RADIUS)) / A;
  131. XR := (-K*B + Sqrt(K*K*RADIUS*RADIUS - B*B + RADIUS*RADIUS)) / A;
  132.  
  133. YL := K*XL + B;
  134. YR := K*XR + B;
  135.  
  136. ChordLength := Sqrt(Sqr(XL - XR) + Sqr(YL-YR));
  137.  
  138. if X0 < XR then
  139. begin
  140. XPoint1 := Trunc (X0 + XCenter);
  141. YPoint1 := Trunc (Y0 + YCenter);
  142. XPoint2 := Trunc (XR + XCenter);
  143. YPoint2 := Trunc (YR + YCenter);
  144. end
  145. else
  146. begin
  147. XPoint1 := Trunc (X0 + XCenter);
  148. YPoint1 := Trunc (Y0 + YCenter);
  149. XPoint2 := Trunc (XL + XCenter);
  150. YPoint2 := Trunc (YL + YCenter);
  151. end;
  152.  
  153. with MainForm.ChordImage.Canvas do
  154. begin
  155. Pen.Color := clBlue;
  156. MoveTo(XPoint1, YPoint1);
  157. LineTo(XPoint2, YPoint2);
  158.  
  159. Pen.Color := clBlack;
  160. Ellipse(XPoint1-3, YPoint1-3, XPoint1+3, YPoint1+3);
  161.  
  162. Pen.Color := clRed;
  163. Ellipse(Trunc(TempX + XCenter)-3, Trunc(TempY+YCenter)-3, Trunc(TempX+XCenter)+3, Trunc(TempY + YCenter)+3);
  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: Real;
  182. begin
  183. with MainForm.ChordImage.Canvas do
  184. begin
  185. GetRandomCoordOutsideCircle(XPoint, YPoint);
  186. //Pen.Color := clBlack;
  187. //Ellipse(Trunc(XPoint)-2, Trunc(YPoint)-2, Trunc(XPoint)+2, Trunc(YPoint)+2);
  188.  
  189. //GetRandomCoordInCircle(XCyclePoint, YCyclePoint);
  190. //MainForm.ChordImage.Canvas.Pen.Color := clBlack;
  191. //MainForm.ChordImage.Canvas.Ellipse(Trunc(XCyclePoint)-2, Trunc(YCyclePoint)-2, Trunc(XCyclePoint)+2, Trunc(YCyclePoint)+2);
  192.  
  193. {with MainForm.ChordImage.Canvas do
  194. begin
  195. Pen.Color := clBlue;
  196. MoveTo(Trunc(XPoint), Trunc(YPoint));
  197. LineTo(Trunc(XCyclePoint), Trunc(YCyclePoint));
  198. end;}
  199.  
  200. ConnectChord(XPoint, YPoint);
  201. end;
  202. end;
  203.  
  204. procedure TMainForm.FormCreate(Sender: TObject);
  205. begin
  206. Randomize;
  207. XCenter := MainForm.ChordImage.Width Div 2;
  208. YCenter := MainForm.ChordImage.Height Div 2;
  209.  
  210. ChordCount := 0;
  211. LongChordCount := 0;
  212. Side := RADIUS * Sqrt(3);
  213. end;
  214.  
  215. procedure TMainForm.RunButtonClick(Sender: TObject);
  216. var
  217. I: Integer;
  218. begin
  219. with ChordImage do
  220. begin
  221. Picture := nil;
  222. Canvas.Pen.Color := clBlack;
  223. Canvas.Ellipse(XCenter - RADIUS, YCenter - RADIUS, XCenter + RADIUS, YCenter + RADIUS);
  224. end;
  225. I := 0;
  226. repeat
  227. DrawChord;
  228. Inc(I);
  229. until I = 1;
  230.  
  231. NumberLabel.Caption := IntToStr(LongChordCount) + '/' + IntToStr(ChordCount) + '=' + FloatToStr(LongChordCount/ChordCount);
  232. end;
  233.  
  234. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement