-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedgeshelper.pas
124 lines (112 loc) · 3.01 KB
/
edgeshelper.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
unit EdgesHelper;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, ExtCtrls, Graphics, CustomClasses, VerticesHelper, Math;
procedure DrawEdge(BeginVertex, EndVertex: TVertex; Graph: TImage; Edges: TList; const Weight: Integer = 1);
function EdgeNotExists(BeginVertex, EndVertex: TVertex; Edges: TList): Boolean;
function CursorOnEdge(Cursor, Start, Finish: TPoint): Boolean;
implementation
//Edge drawing
procedure DrawEdge(BeginVertex, EndVertex: TVertex; Graph: TImage; Edges: TList; const Weight: Integer);
var
x1, y1, x2, y2: Integer;
begin
case BeginVertex.Y - EndVertex.Y of
Low(Integer)..-51:
begin
x2 := 0;
y2 := -10;
if BeginVertex.X - EndVertex.X >= 150 then
begin
x1 := -10;
y1 := 0;
end
else if abs(BeginVertex.X - EndVertex.X) < 150 then
begin
x1 := 0;
y1 := 10;
end
else if BeginVertex.X - EndVertex.X <= - 150 then
begin
x1 := 10;
y1 := 0;
end;
end;
-50..50:
begin
y1 := 0;
y2 := 0;
if BeginVertex.X - EndVertex.X < -50 then
begin
x1 := 10;
x2 := -10;
end
else if BeginVertex.X - EndVertex.X > 50 then
begin
x1 := -10;
x2 := 10;
end
else
begin
x1 := 0;
x2 := 0;
if BeginVertex.Y < EndVertex.Y then
begin
y1 := 10;
y2 := -10;
end
else
begin
y1 := -10;
y2 := 10;
end;
end;
end;
51..High(Integer):
begin
x1 := 0;
y1 := -10;
if BeginVertex.X - EndVertex.X >= 150 then
begin
x2 := 10;
y2 := 0;
end
else if abs(BeginVertex.X - EndVertex.X) < 150 then
begin
x2 := 0;
y2 := 10;
end
else if BeginVertex.X - EndVertex.X <= - 150 then
begin
x2 := -10;
y2 := 0;
end;
end;
end;
Edges.Add(TEdge.Create(Edges.Count + 1, Point(BeginVertex.X + x1, BeginVertex.Y + y1),
Point(EndVertex.X + x2, EndVertex.Y + y2), 1));
Graph.Canvas.Line(BeginVertex.X + x1, BeginVertex.Y + y1, EndVertex.X + x2, EndVertex.Y + y2);
Graph.Canvas.TextOut(Round((BeginVertex.X + x1 + EndVertex.X + x2) / 2), Round((BeginVertex.Y + y1 + EndVertex.Y + y2) / 2), IntToStr(Weight));
end;
//Is the Edge already exists?
function EdgeNotExists(BeginVertex, EndVertex: TVertex; Edges: TList): Boolean;
var
i: integer;
begin
Result := True;
for i := 0 to Edges.Count - 1 do
if (PointInVertex(TEdge(Edges[i]).Start, BeginVertex) and PointInVertex(TEdge(Edges[i]).Finish, EndVertex)) or
(PointInVertex(TEdge(Edges[i]).Start, EndVertex) and PointInVertex(TEdge(Edges[i]).Finish, BeginVertex)) then
begin
Result := False;
Exit;
end;
end;
//Is cursor on edge?
function CursorOnEdge(Cursor, Start, Finish: TPoint): Boolean;
begin
Result := RoundTo((Cursor.X - Start.X) / (Finish.X - Start.X), -1) =
RoundTo((Cursor.Y - Start.Y) / (Finish.Y - Start.Y), -1);
end;
end.