Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics, Math,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;
- type
- TMatrix = array[0..3] of array[0..3] of Integer;
- TForm1 = class(TForm)
- imgDrawPlace: TImage;
- procedure FormCreate(Sender: TObject);
- private
- procedure DrawCircle(const Value, Radius: Integer; const Pos: TPoint);
- procedure DrawLine(const FPoint, SPoint: TPoint);
- procedure DrawArrow( FPoint, SPoint: TPoint);
- procedure DrawGraph(const AdjacentMatrix: TMatrix; const CentrePoint: TPoint);
- function CountSecontPointPosByAngle(const Angle, Linelen: integer;
- const FPoint: TPoint): TPoint;
- public
- { Public declarations }
- end;
- const
- NodeRadius = 20;
- PenWidth = 2;
- PenArrowWidth = 7;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.DrawCircle(const Value, Radius: Integer; const Pos: TPoint);
- var
- XTextOffset, YTextOffset: integer;
- begin
- XTextOffset := (Radius div 5);
- YTextOffset := (Radius div 5) + 5;
- imgDrawPlace.Canvas.Pen.Width := PenWidth;
- imgDrawPlace.Canvas.Ellipse((Pos.X - Radius), (Pos.Y - Radius),
- (Pos.X + Radius), (Pos.Y + Radius));
- imgDrawPlace.Canvas.Font.Size := 13;
- imgDrawPlace.Canvas.TextOut(Pos.X - XTextOffset, Pos.Y - YTextOffset, IntToStr(Value));
- end;
- procedure TForm1.DrawLine(const FPoint, SPoint: TPoint);
- begin
- imgDrawPlace.Canvas.MoveTo(FPoint.X, FPoint.Y);
- imgDrawPlace.Canvas.LineTo(SPoint.X, SPoint.Y);
- end;
- procedure TForm1.DrawArrow(FPoint, SPoint: TPoint);
- const
- ArrowLenCoefficient = 8;
- var
- OldPenWidth: Integer;
- XOffset, YOffset: Integer;
- begin
- DrawLine(FPoint, SPoint);
- OldPenWidth := imgDrawPlace.Canvas.Pen.Width;
- imgDrawPlace.Canvas.Pen.Width := PenArrowWidth;
- XOffset := Abs(FPoint.X - SPoint.X) div ArrowLenCoefficient;
- YOffset := Abs(FPoint.Y - SPoint.Y) div ArrowLenCoefficient;
- if Max(FPoint.X, SPoint.X) = SPoint.X then
- FPoint.X := SPoint.X - XOffset
- else
- FPoint.X := SPoint.X + XOffset;
- if Max(FPoint.Y, SPoint.Y) = SPoint.Y then
- FPoint.Y := SPoint.Y - YOffset
- else
- FPoint.Y := SPoint.Y + YOffset;
- DrawLine(FPoint, SPoint);
- imgDrawPlace.Canvas.Pen.Width := OldPenWidth;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- const
- START_MATRIX2: TMatrix =
- ((0, 10, 6, 0),
- (10, 0, 0, 0),
- (10, 10, 0, 10),
- (0, 0, 0, 0));
- var
- FPoint, a: TPoint;
- begin
- FPoint.X := 300;
- FPoint.Y := 300;
- DrawGraph(START_MATRIX2, FPoint);
- end;
- procedure TForm1.DrawGraph(const AdjacentMatrix: TMatrix; const CentrePoint: TPoint);
- var
- CurAngle, AngleOffset, i, j, AdjacentMatrixLen: Integer;
- CurNodePos: TPoint;
- NodesPositions: array of TPoint;
- begin
- AdjacentMatrixLen := Length(AdjacentMatrix);
- AngleOffset := (360 div AdjacentMatrixLen);
- SetLength(NodesPositions, AdjacentMatrixLen);
- CurAngle := 0;
- for i := 0 to High(AdjacentMatrix) do
- begin
- NodesPositions[i] := CountSecontPointPosByAngle(CurAngle, 200, CentrePoint);
- Inc(CurAngle, AngleOffset);
- end;
- for i := 0 to High(AdjacentMatrix) do
- for j := 0 to High(AdjacentMatrix) do
- if AdjacentMatrix[i, j] <> 0 then
- DrawArrow(NodesPositions[i], NodesPositions[j]);
- for i := 0 to High(NodesPositions) do
- DrawCircle(i+1, NodeRadius, NodesPositions[i]);
- end;
- function TForm1.CountSecontPointPosByAngle(const Angle, Linelen: integer;
- const FPoint: TPoint): TPoint;
- begin
- Result.X := FPoint.X + Round(Linelen * Cos(Angle * Pi / 180));
- Result.Y := FPoint.Y - Round(Linelen * Sin(Angle * Pi / 180));
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement