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 = High(Integer);
- 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 GetRandomCoordInCircle(var XPoint: Real; var YPoint: Real);
- var
- Coef: Integer;
- begin
- repeat
- XPoint := Random(RADIUS);
- YPoint := Random(RADIUS);
- 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: Real; var YPoint: Real);
- 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 GetRandomCoordInDiametr(var XPoint: Real; var YPoint: Real; X0, Y0: Real);
- var
- Coef: Integer;
- K: Real;
- begin
- K := (Y0-YCenter)/(X0-Xcenter);
- K := -1/K;
- repeat
- XPoint := Random(RADIUS);
- Coef := Random(2);
- if Coef = 1 then
- XPoint := -Abs(XPoint)
- else
- XPoint := Abs(XPoint);
- YPoint := K*XPoint;
- until Sqr(XPoint) + Sqr(YPoint) < Sqr(RADIUS);
- XPoint := XPoint + XCenter;
- YPoint := YPoint + YCenter;
- {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, TempX, TempY, X1, Y1, X2, Y2, B, A, C, ChordLength: Real;
- IsOnArc: Boolean;
- begin
- try
- X0 := X0 - XCenter;
- Y0 := Y0 - YCenter;
- GetRandomCoordInDiametr(TempX, TempY, X0, Y0);
- TempX := TempX - XCenter;
- TempY := TempY - YCenter;
- K := (Y0 - TempY)/(X0-Tempx);
- 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