-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainunit.pas
158 lines (141 loc) · 4.2 KB
/
mainunit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
unit MainUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
ExtCtrls, Grids, Menus, Buttons, VerticesHelper, EdgesHelper, CustomClasses;
type
{ TMainForm }
TMainForm = class(TForm)
ImageList: TImageList;
PageControl: TPageControl;
GraphicalView: TTabSheet;
Graph: TImage;
MatrixView: TTabSheet;
MatrixGrid: TStringGrid;
DirectionType: TRadioGroup;
Devider2: TToolButton;
ClearGraph: TToolButton;
ToolButton1: TToolButton;
WeightType: TRadioGroup;
ToolBar: TToolBar;
AddVertex: TToolButton;
AddEdge: TToolButton;
Devider1: TToolButton;
DeleteVertex: TToolButton;
DeleteEdge: TToolButton;
procedure ClearGraphClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure GraphMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure GraphMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
);
procedure MatrixGridColRowInserted(Sender: TObject; IsColumn: Boolean;
sIndex, tIndex: Integer);
procedure ToolButton1Click(Sender: TObject);
private
{ private declarations }
Edges: TList;
Vertices: TList;
BeginVertex: TVertex;
public
{ public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.lfm}
{ TMainForm }
//Setting of all requirements
procedure TMainForm.FormCreate(Sender: TObject);
begin
Vertices := TList.Create;
Edges := TList.Create;
Graph.Canvas.Rectangle(Graph.BoundsRect);
Graph.Canvas.Clear;
BeginVertex := Nil;
end;
procedure TMainForm.ClearGraphClick(Sender: TObject);
begin
Graph.Canvas.Clear;
Vertices.Clear;
Edges.Clear;
MatrixGrid.ColCount := 1;
MatrixGrid.RowCount := 1;
BeginVertex := Nil;
end;
//MouseClick on Graph handler
procedure TMainForm.GraphMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
TempVertex: TVertex;
Weight: Integer;
begin
TempVertex := TVertex.Create(Vertices.Count + 1, X, Y);
if AddVertex.Down and VerticesNotIntersect(TempVertex, Vertices) then
begin
DrawVertex(TempVertex, Graph, Vertices);
MatrixGrid.ColCount := Vertices.Count + 1;
MatrixGrid.RowCount := Vertices.Count + 1;
MatrixGrid.Cells[0, TempVertex.Id] := IntToStr(TempVertex.Id);
MatrixGrid.Cells[TempVertex.Id, 0] := IntToStr(TempVertex.Id);
end;
TempVertex := FindVertex(X, Y, Vertices);
if AddEdge.Down then
begin
if BeginVertex <> Nil then
begin
if CheckVertex(TempVertex, Vertices) and (BeginVertex <> TempVertex) then
begin
if EdgeNotExists(BeginVertex, TempVertex, Edges)then
begin
Weight := 1;
if WeightType.ItemIndex = 1 then
Weight := StrToInt(InputBox('Grapher3000', 'Input edge weight:', '1'));
DrawEdge(BeginVertex, TempVertex, Graph, Edges, Weight);
MatrixGrid.Cells[BeginVertex.Id, TempVertex.Id] := IntToStr(Weight);
MatrixGrid.Cells[TempVertex.Id, BeginVertex.Id] := IntToStr(Weight);
end;
BeginVertex := Nil;
end;
end
else if CheckVertex(TempVertex, Vertices) then
BeginVertex := TempVertex;
end;
if DeleteVertex.Down then
if CheckVertex(TempVertex, Vertices) then
begin
RemoveVertex(TempVertex, Vertices, Edges, Graph);
end;
end;
procedure TMainForm.GraphMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
i: Integer;
begin
Graph.Cursor := crDefault;
if DeleteEdge.Down then
for i := 0 to Edges.Count - 1 do
if CursorOnEdge(Point(X, Y), TEdge(Edges[i]).Start, TEdge(Edges[i]).Finish) then
begin
Graph.Cursor := crHandPoint;
Break;
end;
end;
procedure TMainForm.MatrixGridColRowInserted(Sender: TObject;
IsColumn: Boolean; sIndex, tIndex: Integer);
var
i: Integer;
begin
if IsColumn then
for i := 1 to Vertices.Count - 1 do
MatrixGrid.Cells[Vertices.Count, i] := '0'
else
for i := 1 to Vertices.Count do
MatrixGrid.Cells[i, Vertices.Count] := '0';
end;
procedure TMainForm.ToolButton1Click(Sender: TObject);
begin
ShowMessage('Vertices: ' + IntToStr(Vertices.Count) + Char(#10) + 'Edges: ' + IntToStr(Edges.Count));
end;
end.