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
- 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 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, X1, Y1: Integer);
- var
- XL, XR, X, Y, Coef: Integer;
- K, C, A, ChordLength: Real;
- begin
- X0 := X0 - XCenter;
- Y0 := Y0 - YCenter;
- X1 := X1 - XCenter;
- Y1 := Y1 - YCenter;
- if X1-X0 <> 0 then
- begin
- K := (Y1-Y0)/(X1-X0);
- C := Y0 - K*X0;
- A := 1 + K*K;
- XL := Trunc((-K*C - Sqrt(K*K*RADIUS*RADIUS - C*C + RADIUS*RADIUS)) / A);
- XR := Trunc((-K*C + Sqrt(K*K*RADIUS*RADIUS - C*C + RADIUS*RADIUS)) / A);
- if X1 > 0 then
- X := XL
- else
- X := XR;
- Y := Trunc(Sqrt(RADIUS*RADIUS - X*X));
- Coef := Random(2);
- if Coef = 1 then
- Y := - Abs(Y)
- else
- Y := Abs(Y);
- ChordLength := Sqrt(Sqr(X - X1) + Sqr(Y-Y1));
- X := X + XCenter;
- Y := Y + YCenter;
- X1 := X1 + XCenter;
- Y1 := Y1 + YCenter;
- with MainForm.ChordImage.Canvas do
- begin
- Pen.Color := clBlue;
- MoveTo(X, Y);
- LineTo(X1, Y1);
- end;
- if ChordLength > Side then
- Inc(LongChordCount);
- Inc(ChordCount);
- end;
- end;
- procedure DrawChord;
- var
- XPoint, YPoint, XCyclePoint, YCyclePoint: Integer;
- 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);
- ConnectChord(XPoint, YPoint, XCyclePoint, YCyclePoint);
- 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