Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit ChordUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;
- type
- TMainForm = class(TForm)
- ChordImage: TImage;
- RunButton: TButton;
- AnswerLabel: TLabel;
- NumberLabel: TLabel;
- procedure RunButtonClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- implementation
- uses
- Math;
- const
- RADIUS = 330;
- MAX_RANGE = 5000;
- var
- XCenter, YCenter: Word;
- ChordCount, LongChordCount: Integer;
- Side: Real;
- {$R *.dfm}
- procedure GetRandomCoordOutsideCircle(var XPoint: Integer; var YPoint: Integer);
- var
- Coef: Integer;
- begin
- repeat
- XPoint := Random(MAX_RANGE);
- YPoint := Random(MAX_RANGE);
- until Sqr(XPoint) + Sqr(YPoint) > Sqr(RADIUS);
- Coef := Random(2);
- if Coef = 1 then
- XPoint := XCenter - Abs(XPoint)
- else
- XPoint := XCenter + Abs(XPoint);
- Coef := Random(2);
- if Coef = 1 then
- YPoint := YCenter - Abs(YPoint)
- else
- YPoint := YCenter + Abs(YPoint);
- end;
- procedure GetRandomCoordOnCircumference (var XPoint: Integer; var YPoint: Integer);
- var
- Coef: Integer;
- begin
- Coef := Random(2);
- if Coef = 1 then
- begin
- XPoint := Random(RADIUS);
- YPoint := Trunc(Sqrt(Sqr(RADIUS) - Sqr(XPoint)));
- end
- else
- begin
- YPoint := Random(RADIUS);
- XPoint := Trunc(Sqrt(Sqr(RADIUS) - Sqr(YPoint)))
- end;
- Coef := Random(2);
- if Coef = 1 then
- XPoint := XCenter - Abs(XPoint)
- else
- XPoint := XCenter + Abs(XPoint);
- Coef := Random(2);
- if Coef = 1 then
- YPoint := YCenter - Abs(YPoint)
- else
- YPoint := YCenter + Abs(YPoint);
- end;
- procedure ConnectChord(X0, Y0: Real; Delta: Real);
- var
- XPoint1, YPoint1, XPoint2, YPoint2, Coef: Integer;
- XL, XR, YL, YR, K, K1, K2, TempX1, TempY1, B, A, C, ChordLength: Real;
- begin
- try
- X0 := X0 - XCenter;
- Y0 := Y0 - YCenter;
- TempX1 := (X0*Sqr(RADIUS) - RADIUS*Sqrt(Sqr(X0*RADIUS) - (Sqr(X0)+Sqr(Y0))*(Sqr(RADIUS) - Sqr(Y0))))/(Sqr(X0)+Sqr(Y0));
- TempY1 := Sqrt(Sqr(RADIUS) - Sqr(TempX1));
- K1 := (TempY1 - Y0)/(TempX1 - X0);
- if ((K1 * TempY1/TempX1) > -0.99) Or ((K1 * TempY1/TempX1) < -1.01) then
- begin
- TempY1 := -Sqrt(Sqr(RADIUS) - Sqr(TempX1));
- K1 := (TempY1 - Y0)/(TempX1 - X0);
- end;
- TempX1 := (X0*Sqr(RADIUS) + RADIUS*Sqrt(Sqr(X0*RADIUS) - (Sqr(X0)+Sqr(Y0))*(Sqr(RADIUS) - Sqr(Y0))))/(Sqr(X0)+Sqr(Y0));
- TempY1 := Sqrt(Sqr(RADIUS) - Sqr(TempX1));
- K2 := (TempY1 - Y0)/(TempX1 - X0);
- if ((K2 * TempY1/TempX1) > -0.99) Or ((K2 * TempY1/TempX1) < -1.01) then
- begin
- TempY1 := -Sqrt(Sqr(RADIUS) - Sqr(TempX1));
- K2 := (TempY1 - Y0)/(TempX1 - X0);
- end;
- if Y0 < 0 then
- K := Tan(Arctan(K1) - Delta)
- else
- K := Tan(Arctan(K1) + Delta);
- B := Y0 - K*X0;
- A := 1 + K*K;
- XL := (-K*B - Sqrt(K*K*RADIUS*RADIUS - B*B + RADIUS*RADIUS)) / A;
- XR := (-K*B + Sqrt(K*K*RADIUS*RADIUS - B*B + RADIUS*RADIUS)) / A;
- YL := K*XL + B;
- YR := K*XR + B;
- ChordLength := Sqrt(Sqr(XL - XR) + Sqr(YL-YR));
- XPoint1 := Trunc (XL + XCenter);
- YPoint1 := Trunc (YL + YCenter);
- XPoint2 := Trunc (XR + XCenter);
- YPoint2 := Trunc (YR + YCenter);
- with MainForm.ChordImage.Canvas do
- begin
- Pen.Color := clBlue;
- MoveTo(XPoint1, YPoint1);
- LineTo(XPoint2, YPoint2);
- end;
- if ChordLength > Side then
- Inc(LongChordCount);
- Inc(ChordCount);
- except
- exit;
- end;
- end;
- function CalculateAlpha(X, Y: Real): Real;
- begin
- CalculateAlpha := 2 * Arcsin(RADIUS / Sqrt((Sqr(X - XCenter) + Sqr(Y - YCenter))));
- end;
- procedure DrawChord;
- var
- XPoint, YPoint, XCyclePoint, YCyclePoint: Integer;
- Alpha, Delta: Real;
- begin
- with MainForm.ChordImage.Canvas do
- begin
- GetRandomCoordOutsideCircle(XPoint, YPoint);
- Pen.Color := clBlack;
- Ellipse(XPoint-2, YPoint-2, XPoint+2, YPoint+2);
- {GetRandomCoordOnCircumference(XCyclePoint, YCyclePoint);
- Pen.Color := clRed;
- Ellipse(XCyclePoint-2, YCyclePoint-2, XCyclePoint+2, YCyclePoint+2);}//первый метод
- Alpha := CalculateAlpha(XPoint, YPoint);
- Delta := Random(Trunc(Alpha * 100000000)) / 100000000;
- ConnectChord(XPoint, YPoint, Delta);
- end;
- end;
- procedure TMainForm.FormCreate(Sender: TObject);
- begin
- Randomize;
- XCenter := MainForm.ChordImage.Width Div 2;
- YCenter := MainForm.ChordImage.Height Div 2;
- ChordCount := 0;
- LongChordCount := 0;
- Side := RADIUS * Sqrt(3);
- end;
- procedure TMainForm.RunButtonClick(Sender: TObject);
- var
- I: Integer;
- begin
- with ChordImage do
- begin
- Canvas.Ellipse(XCenter - RADIUS, YCenter - RADIUS, XCenter + RADIUS, YCenter + RADIUS);
- Canvas.Pen.Color := clBlack;
- end;
- I := 0;
- repeat
- DrawChord;
- Inc(I);
- until I = 1000;
- NumberLabel.Caption := IntToStr(LongChordCount) + '/' + IntToStr(ChordCount) + '=' + FloatToStr(LongChordCount/ChordCount);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement