Advertisement
Guest User

Untitled

a guest
May 14th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.80 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  7.    System.Classes, Vcl.Graphics, Math,
  8.    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;
  9.  
  10. type
  11.    TMatrix = array[0..3] of array[0..3] of Integer;
  12.  
  13.    TForm1 = class(TForm)
  14.       imgDrawPlace: TImage;
  15.       procedure FormCreate(Sender: TObject);
  16.    private
  17.       procedure DrawCircle(const Value, Radius: Integer; const Pos: TPoint);
  18.       procedure DrawLine(const FPoint, SPoint: TPoint);
  19.       procedure DrawArrow( FPoint, SPoint: TPoint);
  20.       procedure DrawGraph(const AdjacentMatrix: TMatrix; const CentrePoint: TPoint);
  21.  
  22.       function  CountSecontPointPosByAngle(const Angle, Linelen: integer;
  23.          const FPoint: TPoint): TPoint;
  24.    public
  25.       { Public declarations }
  26.    end;
  27.  
  28. const
  29.    NodeRadius = 20;
  30.    PenWidth = 2;
  31.    PenArrowWidth = 7;
  32.  
  33.  
  34. var
  35.    Form1: TForm1;
  36.  
  37. implementation
  38.  
  39. {$R *.dfm}
  40.  
  41. procedure TForm1.DrawCircle(const Value, Radius: Integer; const Pos: TPoint);
  42. var
  43.    XTextOffset, YTextOffset: integer;
  44. begin
  45.    XTextOffset := (Radius div 5);
  46.    YTextOffset := (Radius div 5) + 5;
  47.  
  48.    imgDrawPlace.Canvas.Pen.Width := PenWidth;
  49.    imgDrawPlace.Canvas.Ellipse((Pos.X - Radius), (Pos.Y - Radius),
  50.                                (Pos.X + Radius), (Pos.Y + Radius));
  51.    imgDrawPlace.Canvas.Font.Size := 13;
  52.    imgDrawPlace.Canvas.TextOut(Pos.X - XTextOffset, Pos.Y - YTextOffset, IntToStr(Value));
  53. end;
  54.  
  55. procedure TForm1.DrawLine(const FPoint, SPoint: TPoint);
  56. begin
  57.    imgDrawPlace.Canvas.MoveTo(FPoint.X, FPoint.Y);
  58.    imgDrawPlace.Canvas.LineTo(SPoint.X, SPoint.Y);
  59. end;
  60.  
  61. procedure TForm1.DrawArrow(FPoint, SPoint: TPoint);
  62. const
  63.    ArrowLenCoefficient = 8;
  64. var
  65.    OldPenWidth: Integer;
  66.    XOffset, YOffset: Integer;
  67. begin
  68.    DrawLine(FPoint, SPoint);
  69.  
  70.    OldPenWidth := imgDrawPlace.Canvas.Pen.Width;
  71.    imgDrawPlace.Canvas.Pen.Width :=  PenArrowWidth;
  72.  
  73.    XOffset := Abs(FPoint.X - SPoint.X) div ArrowLenCoefficient;
  74.    YOffset := Abs(FPoint.Y - SPoint.Y) div ArrowLenCoefficient;
  75.  
  76.    if Max(FPoint.X, SPoint.X) = SPoint.X then
  77.       FPoint.X :=  SPoint.X - XOffset
  78.    else
  79.       FPoint.X :=  SPoint.X + XOffset;
  80.  
  81.  
  82.    if Max(FPoint.Y, SPoint.Y) = SPoint.Y then
  83.       FPoint.Y :=  SPoint.Y - YOffset
  84.    else
  85.       FPoint.Y :=  SPoint.Y + YOffset;
  86.  
  87.    DrawLine(FPoint, SPoint);
  88.    imgDrawPlace.Canvas.Pen.Width := OldPenWidth;
  89. end;
  90.  
  91. procedure TForm1.FormCreate(Sender: TObject);
  92. const
  93.    START_MATRIX2: TMatrix =
  94.    ((0, 10, 6,  0),
  95.     (10, 0, 0,   0),
  96.     (10, 10, 0,   10),
  97.     (0, 0,   0,   0));
  98. var
  99.    FPoint, a: TPoint;
  100. begin
  101.    FPoint.X := 300;
  102.    FPoint.Y := 300;
  103.  
  104.    DrawGraph(START_MATRIX2, FPoint);
  105. end;
  106.  
  107.  
  108. procedure TForm1.DrawGraph(const AdjacentMatrix: TMatrix; const CentrePoint: TPoint);
  109. var
  110.    CurAngle, AngleOffset, i, j, AdjacentMatrixLen: Integer;
  111.    CurNodePos: TPoint;
  112.    NodesPositions: array of TPoint;
  113. begin
  114.    AdjacentMatrixLen := Length(AdjacentMatrix);
  115.  
  116.    AngleOffset := (360 div AdjacentMatrixLen);
  117.    SetLength(NodesPositions, AdjacentMatrixLen);
  118.    CurAngle := 0;
  119.  
  120.    for i := 0 to High(AdjacentMatrix) do
  121.    begin
  122.       NodesPositions[i] := CountSecontPointPosByAngle(CurAngle, 200, CentrePoint);
  123.       Inc(CurAngle, AngleOffset);
  124.    end;
  125.  
  126.    for i := 0 to High(AdjacentMatrix) do
  127.       for j := 0 to High(AdjacentMatrix) do
  128.          if AdjacentMatrix[i, j] <> 0 then
  129.             DrawArrow(NodesPositions[i], NodesPositions[j]);
  130.  
  131.    for i := 0 to High(NodesPositions) do
  132.       DrawCircle(i+1, NodeRadius, NodesPositions[i]);
  133. end;
  134.  
  135.  
  136.  
  137. function TForm1.CountSecontPointPosByAngle(const Angle, Linelen: integer;
  138.    const FPoint: TPoint): TPoint;
  139. begin
  140.    Result.X := FPoint.X + Round(Linelen * Cos(Angle * Pi / 180));
  141.    Result.Y := FPoint.Y - Round(Linelen * Sin(Angle * Pi / 180));
  142. end;
  143.  
  144. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement