annotate pkg-src/tree-x/tree.c @ 169:15872534500d r20-3b11

Import from CVS: tag r20-3b11
author cvs
date Mon, 13 Aug 2007 09:46:53 +0200
parents 85ec50267440
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
1 /* ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
2 * File : tree.c
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
3 * Purpose : dynamic tree program based on Sven Moen's algorithm
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
4 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
5 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
6
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
7 #include "defs.h"
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
8 #include "tree.h"
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
9 #include "dbl.h"
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
10 #include "intf.h"
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
11 #include <string.h>
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
12 #include <stdlib.h>
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
13
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
14 /* ------------------------------------------------------------------------- */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
15 /* Global Variables */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
16 /* ------------------------------------------------------------------------- */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
17
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
18 static int NumLines = 0;
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
19 static int NumNodes = 0;
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
20
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
21
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
22 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
23 *
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
24 * MakeLine() allocates the memory required for a Polyline and
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
25 * initializes the fields of a Polyline to the arguments. The
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
26 * newly-allocated Polyline is returned by the function.
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
27 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
28 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
29 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
30
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
31 Polyline*
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
32 MakeLine(short dx, short dy, Polyline *line)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
33 {
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
34 Polyline *new_line = (Polyline *) malloc(sizeof(Polyline));
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
35
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
36 NASSERT(new_line, "could not allocate memory for polyline");
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
37 NumLines++;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
38
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
39 new_line->dx = dx;
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
40 new_line->dy = dy;
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
41 new_line->link = line;
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
42
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
43 return new_line;
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
44 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
45
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
46 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
47 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
48 * MakeNode() allocates the memory required for a tree node, and
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
49 * zeros out all the fields in the Node. It returns a pointer to the
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
50 * tree node upon success, and NULL upon failure.
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
51 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
52 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
53 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
54
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
55 Tree*
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
56 MakeNode(void)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
57 {
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
58 Tree *node = (Tree *) malloc(sizeof(Tree));
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
59
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
60 NASSERT(node, "could not allocate memory for node");
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
61 NumNodes++;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
62
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
63 if (node == NULL)
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 167
diff changeset
64 return NULL;
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
65 else {
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
66 memset((char *) node, 0, sizeof(Tree));
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 167
diff changeset
67 return node;
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
68 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
69 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
70
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
71 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
72 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
73 * MakeBridge()
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
74 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
75 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
76 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
77
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
78 static Polyline*
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
79 MakeBridge(Polyline *line1, int x1, int y1, Polyline *line2, int x2, int y2)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
80 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
81 int dx, dy, s;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
82 Polyline *r;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
83
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
84 dx = x2 + line2->dx - x1;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
85 if (line2->dx == 0)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
86 dy = line2->dy;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
87 else {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
88 s = dx * line2->dy;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
89 dy = s / line2->dx;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
90 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
91 r = MakeLine(dx, dy, line2->link);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
92 line1->link = MakeLine(0, y2 + line2->dy - dy - y1, r);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
93
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 167
diff changeset
94 return r;
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
95 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
96
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
97 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
98 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
99 * Offset() computes the necessary offset that prevents two line segments
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
100 * from intersecting each other. This is the "heart" of the merge step
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
101 * that computes how two subtree contours should be separated.
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
102 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
103 * The code is taken directly from Sven Moen's paper, with changes in
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
104 * some variable names to give more meaning:
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
105 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
106 * - px,py indicate the x- and y-coordinates of the point on the longer
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
107 * segment if the previous Offset() call had two unequal segments
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
108 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
109 * - lx,ly indicate the dx and dy values of the "lower" line segment
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
110 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
111 * - ux,uy indicate the dx and dy values of the "upper" line segment
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
112 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
113 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
114 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
115
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
116 static int
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
117 Offset(int px, int py, int lx, int ly, int ux, int uy)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
118 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
119 int d, s, t;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
120
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
121 if (ux <= px || px+lx <= 0)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
122 return 0;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
123
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
124 t = ux*ly - lx*uy;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
125
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
126 if (t > 0) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
127 if (px < 0) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
128 s = px*ly;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
129 d = s/lx - py;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
130 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
131 else if (px > 0) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
132 s = px*uy;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
133 d = s/ux - py;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
134 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
135 else {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
136 d = -py;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
137 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
138 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
139 else {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
140 if (ux < px+lx) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
141 s = (ux-px) * ly;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
142 d = uy - (py + s/lx);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
143 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
144 else if (ux > px+lx) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
145 s = (lx+px) * uy;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
146 d = s/ux - (py+ly);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
147 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
148 else {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
149 d = uy - (py+ly);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
150 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
151 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
152
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
153 return MAX(0, d);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
154 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
155
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
156 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
157 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
158 * Merge()
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
159 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
160 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
161 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
162
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
163 static int
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
164 Merge(Polygon *c1, Polygon *c2)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
165 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
166 int x, y, total, d;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
167 Polyline *lower, *upper, *bridge;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
168
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
169 x = y = total = 0;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
170
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
171 /* compare lower part of upper child's contour
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
172 * with upper part of lower child's contour
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
173 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
174 upper = c1->lower.head;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
175 lower = c2->upper.head;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
176
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
177 while (lower && upper) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
178 d = Offset(x, y, lower->dx, lower->dy, upper->dx, upper->dy);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
179 y += d;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
180 total += d;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
181
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
182 if (x + lower->dx <= upper->dx) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
183 x += lower->dx;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
184 y += lower->dy;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
185 lower = lower->link;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
186 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
187 else {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
188 x -= upper->dx;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
189 y -= upper->dy;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
190 upper = upper->link;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
191 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
192 }
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
193
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
194 if (lower) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
195 bridge = MakeBridge(c1->upper.tail, 0, 0, lower, x, y);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
196 c1->upper.tail = (bridge->link) ? c2->upper.tail : bridge;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
197 c1->lower.tail = c2->lower.tail;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
198 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
199 else {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
200 bridge = MakeBridge(c2->lower.tail, x, y, upper, 0, 0);
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
201 if (!bridge->link)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
202 c1->lower.tail = bridge;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
203 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
204 c1->lower.head = c2->lower.head;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
205
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 167
diff changeset
206 return total;
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
207 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
208
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
209 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
210 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
211 * DetachParent() reverses the effects of AttachParent by removing
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
212 * the four line segments that connect the subtree contour to the
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
213 * node specified by 'tree'.
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
214 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
215 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
216 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
217
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
218 static void
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
219 DetachParent(Tree *tree)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
220 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
221 free(tree->contour.upper.head->link);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
222 free(tree->contour.upper.head);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
223 tree->contour.upper.head = NULL;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
224 tree->contour.upper.tail = NULL;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
225
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
226 free(tree->contour.lower.head->link);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
227 free(tree->contour.lower.head);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
228 tree->contour.lower.head = NULL;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
229 tree->contour.lower.tail = NULL;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
230
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
231 NumLines -= 4;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
232 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
233
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
234 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
235 *
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
236 * AttachParent()
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
237 * This function also establishes the position of the first child
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
238 * The code follows Sven Moen's version, with slight modification to
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
239 * support varying borders at different levels.
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
240 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
241 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
242 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
243
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
244 static void
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
245 AttachParent(Tree *tree, int h)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
246 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
247 int x, y1, y2;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
248
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
249 if (TreeAlignNodes)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
250 x = tree->border + (TreeParentDistance * 2) +
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
251 (TreeParentDistance - tree->width);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
252 else
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
253 x = tree->border + TreeParentDistance;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
254 y2 = (h - tree->height)/2 - tree->border;
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
255 y1 = y2 + tree->height + (2 * tree->border) - h;
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
256 tree->child->offset.x = x + tree->width;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
257 tree->child->offset.y = y1;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
258 tree->contour.upper.head = MakeLine(tree->width, 0,
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
259 MakeLine(x, y1,
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
260 tree->contour.upper.head));
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
261 tree->contour.lower.head = MakeLine(tree->width, 0,
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
262 MakeLine(x, y2,
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
263 tree->contour.lower.head));
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
264 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
265
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
266 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
267 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
268 * Split()
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
269 * The tree passed to Split() must have at least 1 child, because
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
270 * it doesn't make sense to split a leaf (there are no bridges)
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
271 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
272 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
273 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
274
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
275 static void
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
276 Split(Tree *tree)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
277 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
278 Tree *child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
279 Polyline *link;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
280
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
281 FOREACH_CHILD(child, tree) {
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
282 if ((link = child->contour.upper.tail->link)) {
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
283 free(link->link);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
284 free(link);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
285 child->contour.upper.tail->link = NULL;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
286 NumLines -= 2;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
287 }
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
288 if ((link = child->contour.lower.tail->link)) {
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
289 free(link->link);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
290 free(link);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
291 NumLines -= 2;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
292 child->contour.lower.tail->link = NULL;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
293 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
294 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
295 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
296
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
297 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
298 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
299 * Join() merges all subtree contours of the given tree and returns the
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
300 * height of the entire tree contour.
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
301 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
302 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
303 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
304
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
305 static int
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
306 Join(Tree *tree)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
307 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
308 Tree *child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
309 int d, h, sum;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
310
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
311 /* to start, set the parent's contour and height
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
312 * to contour and height of first child
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
313 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
314 child = tree->child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
315 tree->contour = child->contour;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
316 sum = h = child->height + (2 * child->border);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
317
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
318 /* extend contour to include contours of all children of parent */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
319 for (child = child->sibling ; child ; child = child->sibling) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
320 d = Merge(&tree->contour, &child->contour);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
321 child->offset.y = d + h;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
322 child->offset.x = 0;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
323 h = child->height + (2 * child->border);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
324 /* keep cumulative heights of subtree contours */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
325 sum += d + h;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
326 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
327 return sum;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
328 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
329
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
330 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
331 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
332 * RuboutLeaf() accepts a single node (leaf) and removes its contour.
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
333 * The memory associated with the contour is deallocated.
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
334 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
335 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
336 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
337
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
338 void
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
339 RuboutLeaf(Tree *tree)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
340 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
341 free(tree->contour.upper.head);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
342 free(tree->contour.lower.tail);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
343 free(tree->contour.lower.head);
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
344 tree->contour.upper.head = NULL;
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
345 tree->contour.upper.tail = NULL;
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
346 tree->contour.lower.head = NULL;
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
347 tree->contour.lower.tail = NULL;
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
348 NumLines -= 3;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
349 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
350
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
351 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
352 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
353 * LayoutLeaf() accepts a single node (leaf) and forms its contour. This
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
354 * function assumes that the width, height, and border fields of the
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
355 * node have been assigned meaningful values.
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
356 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
357 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
358 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
359
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
360 void
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
361 LayoutLeaf(Tree *tree)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
362 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
363 tree->node_height = 0;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
364 tree->border = TreeBorderSize;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
365
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
366 tree->contour.upper.tail = MakeLine(tree->width + 2 * tree->border, 0,
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
367 NULL);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
368 tree->contour.upper.head = tree->contour.upper.tail;
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
369
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
370 tree->contour.lower.tail = MakeLine(0, -tree->height - 2 * tree->border,
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
371 NULL);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
372 tree->contour.lower.head = MakeLine(tree->width + 2 * tree->border, 0,
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
373 tree->contour.lower.tail);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
374
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
375 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
376
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
377 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
378 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
379 * LayoutTree() traverses the given tree (in depth-first order), and forms
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
380 * subtree or leaf contours at each node as needed. Each node's contour is
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
381 * stored in its "contour" field. Elision is also supported by generating
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
382 * the contour for both the expanded and collapsed node. This routine
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
383 * also computes the tree height of each node in the tree, so that variable
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
384 * density layout can be demonstrated.
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
385 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
386 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
387 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
388
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
389 void
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
390 LayoutTree(Tree *tree)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
391 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
392 Tree *child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
393 int height = 0;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
394
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
395 FOREACH_CHILD(child, tree) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
396 LayoutTree(child);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
397
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
398 if (child->elision) { /* support elision */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
399 child->old_contour = child->contour;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
400 LayoutLeaf(child);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
401 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
402
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
403 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
404
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
405 if (tree->child) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
406
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
407 FOREACH_CHILD(child, tree)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
408 height = MAX(child->node_height, height);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
409 tree->node_height = height + 1;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
410
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
411 if (TreeLayoutDensity == Fixed)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
412 tree->border = TreeBorderSize;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
413 else
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
414 tree->border =
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
415 (int) (TreeBorderSize * (tree->node_height * DENSITY_FACTOR));
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
416
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
417 AttachParent(tree, Join(tree));
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
418 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
419 else
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
420 LayoutLeaf(tree);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
421 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
422
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
423 /* ------------------------------------------------------------------------- */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
424
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
425 void
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
426 Unzip(Tree *tree)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
427 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
428 Tree *child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
429
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
430 #ifdef INTF
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
431 if (TreeShowSteps) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
432 HiliteNode(tree, New);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
433 tree->on_path = TRUE;
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
434 StatusMsg("Unzip: follow parent links up to root", 0);
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
435 Pause();
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
436 }
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
437 #endif
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
438
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
439 if (tree->parent)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
440 Unzip(tree->parent);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
441
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
442 if (tree->child) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
443
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
444 #ifdef INTF
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
445 /* draw entire contour; do it only for root, because the last
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
446 * frame drawn in this function will have already drawn the
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
447 * contour for the most recently split subtree.
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
448 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
449 if (TreeShowSteps) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
450 if (tree->parent == NULL) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
451 BeginFrame();
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
452 DrawTreeContour(tree, New, CONTOUR_COLOR, FALSE, FALSE, FALSE);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
453 DrawTree(TheTree, New);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
454 EndFrame();
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
455 StatusMsg("Unzip: disassemble entire contour", 0);
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
456 Pause();
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
457 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
458 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
459 #endif
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
460
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
461 #ifdef INTF
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
462 /* draw contour as it would appear after DetachParent() */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
463 if (TreeShowSteps) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
464 BeginFrame();
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
465 #if 0 /* mrb */
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
466 DrawTreeContour(tree, New, CONTOUR_COLOR, TRUE,
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
467 FALSE, FALSE, FALSE);
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
468 #endif
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
469 DrawTreeContour(tree, New, CONTOUR_COLOR, TRUE,
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
470 FALSE, FALSE);
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
471 DrawTree(TheTree, New);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
472 EndFrame();
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
473 StatusMsg("Unzip: detach parent", 0);
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
474 Pause();
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
475 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
476 #endif
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
477
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
478 DetachParent(tree);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
479 Split(tree);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
480
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
481 #ifdef INTF
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
482 if (TreeShowSteps) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
483 BeginFrame();
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
484 /* mark other subtree contours as split, and */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
485 /* draw only the contour on path in full */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
486 FOREACH_CHILD(child, tree) {
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
487 if (!child->on_path)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
488 child->split = TRUE;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
489 else
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
490 DrawTreeContour(child, New, CONTOUR_COLOR,
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
491 FALSE, FALSE, FALSE);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
492 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
493 DrawTree(TheTree, New);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
494 EndFrame();
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
495 StatusMsg("Unzip: split tree", 0);
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
496 Pause();
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
497 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
498 #endif
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
499
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
500 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
501 else
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
502 RuboutLeaf(tree); /* leaf node */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
503 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
504
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
505 /* ------------------------------------------------------------------------- */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
506
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
507 void
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
508 Zip(Tree *tree)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
509 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
510 if (tree->child)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
511 AttachParent(tree, Join(tree));
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
512 else
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
513 LayoutLeaf(tree);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
514
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
515 if (tree->parent)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
516 Zip(tree->parent);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
517 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
518
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
519 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
520 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
521 * Insert() adds the specified child to parent, just after the specified
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
522 * sibling. If 'sibling' is Null, the child is added as the first child.
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
523 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
524 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
525 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
526
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
527 void
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
528 Insert(Tree *parent, Tree *child, Tree *sibling)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
529 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
530 Unzip(parent);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
531 child->parent = parent;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
532 if (sibling) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
533 child->sibling = sibling->sibling;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
534 sibling->sibling = child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
535 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
536 else {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
537 child->sibling = parent->child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
538 parent->child = child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
539 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
540 Zip(parent);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
541 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
542
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
543
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
544
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
545
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
546 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
547 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
548 * Delete() traverses the specified tree and frees all storage
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
549 * allocated to the subtree, including contours and bridges.
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
550 * If the tree had a preceding sibling, the preceding sibling is
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
551 * modified to point to the tree's succeeding sibling, if any.
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
552 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
553 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
554 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
555
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
556 void
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
557 Delete(Tree *tree)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
558 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
559 Tree *sibling = NULL;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
560 Tree *parent, *child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
561
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
562 /* find sibling */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
563 parent = tree->parent;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
564 if (parent) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
565 FOREACH_CHILD(child, parent)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
566 if (child->sibling == tree) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
567 sibling = child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
568 break;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
569 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
570 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
571 if (sibling)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
572 sibling->sibling = tree->sibling;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
573 else if (parent)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
574 parent->child = tree->sibling;
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
575
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
576 DeleteTree(tree, FALSE);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
577 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
578
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
579
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
580 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
581 *
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
582 * DeleteTree() is the recursive function that supports Delete().
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
583 * If 'contour' is True, then only the contours are recursively deleted.
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
584 * This flag should be True when you are regenerating a tree's layout
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
585 * and still want to preserve the nodes. Since contours would be deleted
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
586 * only due to a change in sibling or level distance, each node's border
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
587 * value is updated with the current value of TreeBorderSize;
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
588 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
589 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
590 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
591
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
592 void
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
593 DeleteTree(Tree *tree, int contour)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
594 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
595 Tree *child;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
596
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
597 if (tree->elision) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
598 RuboutLeaf(tree);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
599 tree->contour = tree->old_contour;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
600 tree->old_contour.upper.head = NULL; /* flag to note 'NULL' contour */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
601 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
602
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
603 if (!IS_LEAF(tree)) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
604 DetachParent(tree);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
605 Split(tree);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
606
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
607 FOREACH_CHILD(child,tree)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
608 DeleteTree(child, contour);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
609 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
610 else
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
611 RuboutLeaf(tree);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
612
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
613 if (contour)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
614 tree->border = TreeBorderSize;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
615 else {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
616 free(tree->label.text);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
617 free(tree);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
618 NumNodes--;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
619 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
620 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
621
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
622
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
623 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
624 *
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
625 * ComputeTreeSize()
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
626 * This function should be called after tree layout.
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
627 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
628 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
629 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
630
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
631 void
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
632 ComputeTreeSize(Tree *tree,
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
633 int *width, int *height,
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
634 int *x_offset, int *y_offset)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
635 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
636 Polyline *contour, *tail;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
637 int upper_min_y = 0, lower_max_y = 0;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
638 int upper_abs_y = 0, lower_abs_y = 0;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
639 int x = 0;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
640
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
641 /* do upper contour */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
642 contour = tree->contour.upper.head;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
643 tail = tree->contour.upper.tail;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
644 while (contour) {
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
645 if ((contour->dy + upper_abs_y) < upper_min_y)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
646 upper_min_y = contour->dy + upper_abs_y;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
647 upper_abs_y += contour->dy;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
648 if (contour == tail)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
649 contour = NULL;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
650 else
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
651 contour = contour->link;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
652 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
653
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
654 /* do lower contour */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
655 contour = tree->contour.lower.head;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
656 tail = tree->contour.lower.tail;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
657 while (contour) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
658 if ((contour->dy + lower_abs_y) > lower_max_y)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
659 lower_max_y = contour->dy + lower_abs_y;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
660 lower_abs_y += contour->dy;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
661 x += contour->dx;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
662 if (contour == tail)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
663 contour = NULL;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
664 else
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
665 contour = contour->link;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
666 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
667
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
668 *width = x + 1;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
669 *height = lower_max_y - upper_min_y +
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
670 (tree->height + (2 * tree->border)) + 1;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
671 if (x_offset)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
672 *x_offset = tree->border;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
673 if (y_offset)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
674 *y_offset = - upper_min_y + tree->border;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
675 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
676
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
677 /* ----------------------------------------------------------------------------
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
678 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
679 * PetrifyTree()
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
680 *
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
681 * ----------------------------------------------------------------------------
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
682 */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
683
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
684 void
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
685 PetrifyTree(Tree *tree, int x, int y)
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
686 {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
687 tree->old_pos = tree->pos; /* used by AnimateTree */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
688
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
689 /* fix position of each node */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
690 tree->pos.x = x + tree->offset.x;
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
691 tree->pos.y = y + tree->offset.y;
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 163
diff changeset
692
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
693 if (tree->child) {
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
694 PetrifyTree(tree->child, tree->pos.x, tree->pos.y);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
695 ComputeSubTreeExtent(tree); /* for benefit of interface picking */
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
696 }
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
697 if (tree->sibling)
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
698 PetrifyTree(tree->sibling, tree->pos.x, tree->pos.y);
0132846995bd Import from CVS: tag r20-3b8
cvs
parents:
diff changeset
699 }