-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuMain.pas
More file actions
311 lines (260 loc) · 7.9 KB
/
uMain.pas
File metadata and controls
311 lines (260 loc) · 7.9 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
unit uMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, TeeGenericTree, Vcl.StdCtrls,
Vcl.ExtCtrls, Math, Generics.Collections;
type NodeInformation = packed record
Name: String;
LocalX: Integer;
Modv: Integer;
end;
type
TForm1 = class(TForm)
Button1: TButton;
PaintBox: TPaintBox;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure Draw(Tree: TNode<NodeInformation>);
end;
var
Form1: TForm1;
procedure CenterNodesBetween(LeftNode, RightNode: TNode<NodeInformation>);
implementation
{$R *.dfm}
procedure GetLeftContour(Node: TNode<NodeInformation>; ModSum: Single;
Dictionary: TDictionary<Integer, Single>);
var
I: Integer;
LocalModSum: Single;
begin
LocalModSum := ModSum;
if not (Dictionary.ContainsKey(Node.Level)) then begin
Dictionary.Add(Node.Level, Node.Data.LocalX + LocalModSum);
end else begin
Dictionary.Items[Node.Level] := Min(Dictionary.Items[Node.Level],
Node.Data.LocalX + LocalModSum);
end;
LocalModSum := LocalModSum + Node.Data.Modv;
if (Node.Count > 0) then begin
for I := 0 to Node.Count - 1 do begin
GetLeftContour(Node[I], LocalModSum, Dictionary);
end;
end;
end;
function MaxKey(Values: TDictionary<Integer, Single>): Integer;
var
MaxKey, I: Integer;
begin
MaxKey := 0;
if (Values.Count > 0) then begin
for I := 0 to Values.Count - 1 do begin
if Values.Keys.ToArray[I] > MaxKey then
MaxKey := Values.Keys.ToArray[I];
end;
end;
Result := MaxKey;
end;
procedure GetRightContour(Node: TNode<NodeInformation>; ModSum: Single;
Dictionary: TDictionary<Integer, Single>);
var
I: Integer;
LocalModSum: Single;
begin
LocalModSum := ModSum;
if not (Dictionary.ContainsKey(Node.Level)) then begin
Dictionary.Add(Node.Level, Node.Data.LocalX + LocalModSum);
end else begin
Dictionary.Items[Node.Level] := Max(Dictionary.Items[Node.Level],
Node.Data.LocalX + LocalModSum);
end;
LocalModSum := LocalModSum + Node.Data.Modv;
if (Node.Count > 0) then begin
for I := 0 to Node.Count - 1 do begin
GetLeftContour(Node[I], LocalModSum, Dictionary);
end;
end;
end;
procedure CheckForConflicts(Tree: TNode<NodeInformation>);
var
MinDistance, TreeDistance, NodeSize, ShiftValue: Integer;
NodeContour, SiblingContour: TDictionary<Integer, Single>;
Level, Distance: Integer;
Sibling: TNode<NodeInformation>;
begin
TreeDistance := 5;
NodeSize := 1;
MinDistance := TreeDistance + NodeSize;
ShiftValue := 0;
SiblingContour := nil;
NodeContour := TDictionary<Integer, Single>.Create();
GetLeftContour(Tree, 0, NodeContour);
{ Get the left most node }
if ((Tree.Parent <> nil) and (Tree.Parent.Count > 0)) then begin
Sibling := Tree.Parent[0];
end else begin
Sibling := nil;
end;
while((Sibling <> nil) and (Sibling <> Tree)) do begin
if (SiblingContour <> nil) then begin
SiblingContour.Free;
end;
SiblingContour := TDictionary<Integer, Single>.Create();
GetRightContour(Sibling, 0, SiblingContour);
for Level := Tree.Level + 1 to Min(MaxKey(SiblingContour), MaxKey(NodeContour)) do begin
Distance := Floor(NodeContour[Level] - SiblingContour[Level]);
if (Distance + ShiftValue < MinDistance) then begin
ShiftValue := MinDistance - Distance;
end;
end;
if (shiftValue > 0) then begin
Tree.Data.LocalX := Tree.Data.LocalX + ShiftValue;
Tree.Data.Modv := Tree.Data.Modv + ShiftValue;
CenterNodesBetween(Tree, Sibling);
ShiftValue := 0;
end;
if (Sibling.Index <> Sibling.Parent.Count - 1) then begin
Sibling := Sibling.Parent[Sibling.Index + 1];
end else begin
Sibling := nil;
end;
end;
end;
procedure CenterNodesBetween(LeftNode, RightNode: TNode<NodeInformation>);
var
LeftIndex, RightIndex, NumNodesBetween, Count, I, DesiredX, Offset: Integer;
DistanceBetweenNodes: Integer;
MiddleNode: TNode<NodeInformation>;
begin
LeftIndex := RightNode.Index;
RightIndex := LeftNode.Index;
NumNodesBetween := (RightIndex - LeftIndex) - 1;
if (NumNodesBetween > 0) then begin
DistanceBetweenNodes := (LeftNode.Data.LocalX - RightNode.Data.LocalX)
div (NumNodesBetween + 1);
Count := 1;
for I := LeftIndex + 1 to RightIndex - 1 do begin
MiddleNode := LeftNode.Parent[I];
DesiredX := RightNode.Data.LocalX + (DistanceBetweenNodes * Count);
Offset := DesiredX - MiddleNode.Data.LocalX;
MiddleNode.Data.LocalX := MiddleNode.Data.LocalX + Offset;
MiddleNode.Data.Modv := MiddleNode.Data.Modv + Offset;
Inc(Count);
end;
CheckForConflicts(LeftNode);
end;
end;
procedure CalculateFinalX(Tree: TNode<NodeInformation>; ModvSum: Integer);
var
I: Integer;
begin
Inc(Tree.Data.LocalX, ModvSum div 20);
Inc(ModvSum, Tree.Data.Modv);
if (Tree.Count > 0) then begin
for I := 0 to Tree.Count-1 do begin
CalculateFinalX(Tree[I], ModvSum);
end;
end;
end;
procedure CalculateInitialX(Tree: TNode<NodeInformation>);
const
NodeSize = 1;
SiblingDistance = 0;
var
I, Mid: Integer;
LeftChild, RightChild: TNode<NodeInformation>;
begin
if (Tree.Count > 0) then begin
for I := 0 to Tree.Count-1 do begin
CalculateInitialX(Tree[I]);
end;
end;
// If a leaf
if (Tree.Count = 0) then begin
if (Tree.Index > 0) then begin
Tree.Data.LocalX := Tree.Parent[Tree.Index - 1].Data.LocalX + NodeSize + SiblingDistance;
end else
Tree.Data.LocalX := 0;
end
else if (Tree.Count = 1) then begin
if (Tree.Index = 0) then begin
Tree.Data.LocalX := Tree[0].Data.LocalX;
end else begin
Tree.Data.LocalX := Tree.Parent[Tree.Index - 1].Data.LocalX + NodeSize + SiblingDistance;
Tree.Data.Modv := Tree.Data.LocalX - Tree.Parent[0].Data.LocalX;
end;
end
else begin
if (Tree.Level <> 0) then begin
LeftChild := Tree.Parent[0];
RightChild := Tree.Parent[Tree.Count - 1];
Mid := (LeftChild.Data.LocalX + RightChild.Data.LocalX) div 2;
if (Tree = LeftChild) then
Tree.Data.LocalX := Mid
else begin
Tree.Data.LocalX := Tree.Parent[Tree.Index - 1].Data.LocalX + NodeSize + SiblingDistance;
Tree.Data.Modv := Tree.Data.LocalX - Mid;
end;
end;
end;
if (Tree.Count > 0) and (Tree.Index <> 0) then begin
CheckForConflicts(Tree);
end;
end;
procedure TForm1.Draw(Tree: TNode<NodeInformation>);
var
I: Integer;
X, Y, pX, pY, Radius: Integer;
Angle: Single;
begin
Radius := 30;
if Tree.Count > 0 then begin
for I := 0 to Tree.Count - 1 do begin
Draw(Tree[I]);
end;
end;
X := Tree.Data.LocalX * 100 + 100;
Y := Tree.Level * 100 + 100;
PaintBox.Canvas.Ellipse(X - Radius, Y - Radius, X + Radius, Y + Radius);
PaintBox.Canvas.TextOut(
X - (PaintBox.Canvas.TextWidth(Tree.Data.Name) div 2),
Y - (PaintBox.Canvas.TextHeight(Tree.Data.Name) div 2),
Tree.Data.Name
);
if (Tree.Parent <> nil) then begin
pX := Tree.Parent.Data.LocalX * 100 + 100;
pY := Tree.Parent.Level * 100 + 100;
PaintBox.Canvas.MoveTo(pX - Floor(Radius * Sin(Angle)), pY - Floor(Radius * Cos(Angle)));
PaintBox.Canvas.LineTo(X - Floor(Radius * Sin(Angle)), Y - Floor(Radius * Cos(Angle)));
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TreeA, TreeB: TNode<NodeInformation>;
DataA, DataB, DataC, DataD, DataE: NodeInformation;
begin
DataA.Name := 'Root';
DataA.LocalX := 0;
TreeA := TNode<NodeInformation>.Create(DataA);
DataB.Name := 'Child A';
DataB.LocalX := 0;
TreeA.Add(DataB);
DataC.Name := 'Child B';
DataC.LocalX := 0;
TreeB := TreeA.Add(DataC);
//
// DataD.Name := 'Child C';
// DataD.LocalX := 0;
// TreeB.Add(DataD);
//
// DataE.Name := 'Child D';
// DataE.LocalX := 0;
// TreeB.Add(DataE);
CalculateInitialX(TreeA);
CalculateFinalX(TreeA, 0);
Draw(TreeA);
end;
end.