3599
|
1 |
/***************************************************************************
|
|
2 |
Title: GraphBrowser/Graph.java
|
|
3 |
ID: $Id$
|
|
4 |
Author: Stefan Berghofer, TU Muenchen
|
|
5 |
Copyright 1997 TU Muenchen
|
|
6 |
|
|
7 |
This class contains the core of the layout algorithm and methods for
|
|
8 |
drawing and PostScript output.
|
|
9 |
***************************************************************************/
|
|
10 |
|
|
11 |
package GraphBrowser;
|
|
12 |
|
|
13 |
import java.util.*;
|
|
14 |
import java.awt.*;
|
|
15 |
import java.io.*;
|
|
16 |
|
|
17 |
public class Graph {
|
|
18 |
/**** parameters for layout ****/
|
|
19 |
|
|
20 |
public int box_height=0;
|
|
21 |
public int box_height2;
|
|
22 |
public int box_width;
|
|
23 |
public int box_width2;
|
|
24 |
public int box_hspace;
|
|
25 |
|
|
26 |
Vector vertices=new Vector(10,10);
|
|
27 |
Vector splines=new Vector(10,10);
|
|
28 |
Vector numEdges=new Vector(10,10);
|
|
29 |
Vertex []vertices2;
|
|
30 |
|
|
31 |
public int min_x=0,min_y=0,max_x=10,max_y=10;
|
|
32 |
|
|
33 |
/********************************************************************/
|
|
34 |
/* clone graph object */
|
|
35 |
/********************************************************************/
|
|
36 |
|
|
37 |
public Object clone() {
|
|
38 |
Graph gr=new Graph();
|
|
39 |
Enumeration e1;
|
|
40 |
int i;
|
|
41 |
|
|
42 |
gr.splines=(Vector)(splines.clone());
|
|
43 |
|
|
44 |
e1=vertices.elements();
|
|
45 |
while (e1.hasMoreElements())
|
|
46 |
gr.addVertex((Vertex)(((Vertex)(e1.nextElement())).clone()));
|
|
47 |
|
|
48 |
for (i=0;i<vertices.size();i++) {
|
|
49 |
Vertex vx1=(Vertex)(gr.vertices.elementAt(i));
|
|
50 |
e1=((Vertex)(vertices.elementAt(i))).getChildren();
|
|
51 |
while (e1.hasMoreElements()) {
|
|
52 |
Vertex vx2=(Vertex)(gr.vertices.elementAt(vertices.indexOf(e1.nextElement())));
|
|
53 |
vx1.addChild(vx2);
|
|
54 |
}
|
|
55 |
}
|
|
56 |
|
|
57 |
gr.vertices2 = new Vertex[vertices.size()];
|
|
58 |
gr.vertices.copyInto(gr.vertices2);
|
|
59 |
|
|
60 |
gr.min_x=min_x;gr.max_x=max_x;
|
|
61 |
gr.min_y=min_y;gr.max_y=max_y;
|
|
62 |
|
|
63 |
return gr;
|
|
64 |
}
|
|
65 |
|
|
66 |
Graph() {}
|
|
67 |
|
|
68 |
/********************************************************************/
|
|
69 |
/* Read graph from stream */
|
|
70 |
/********************************************************************/
|
|
71 |
|
|
72 |
public Graph(InputStream s,TreeNode tn) throws IOException, ParseError {
|
6541
|
73 |
StreamTokenizer tok=new StreamTokenizer(new InputStreamReader(s));
|
3599
|
74 |
String name,dir,vertexID;
|
|
75 |
Vertex ve1,ve2;
|
|
76 |
boolean children,unfoldDir;
|
|
77 |
int index=0;
|
|
78 |
|
|
79 |
tok.nextToken();
|
|
80 |
while (tok.ttype!=StreamTokenizer.TT_EOF) {
|
|
81 |
if (tok.ttype!=StreamTokenizer.TT_WORD && tok.ttype!='"')
|
|
82 |
throw new ParseError("expected: vertex name\nfound : "+tok.toString());
|
|
83 |
name=tok.sval;
|
|
84 |
tok.nextToken();
|
|
85 |
if (tok.ttype!=StreamTokenizer.TT_WORD && tok.ttype!='"')
|
|
86 |
throw new ParseError("expected: vertex identifier\nfound : "+tok.toString());
|
|
87 |
vertexID=tok.sval;
|
|
88 |
tok.nextToken();
|
|
89 |
if (tok.ttype!=StreamTokenizer.TT_WORD && tok.ttype!='"')
|
|
90 |
throw new ParseError("expected: directory name\nfound : "+tok.toString());
|
|
91 |
dir=tok.sval;
|
|
92 |
tok.nextToken();
|
|
93 |
if (tok.ttype=='+') {
|
|
94 |
unfoldDir=true;
|
|
95 |
tok.nextToken();
|
|
96 |
} else
|
|
97 |
unfoldDir=false;
|
|
98 |
if (tok.ttype!=StreamTokenizer.TT_WORD && tok.ttype!='"')
|
|
99 |
throw new ParseError("expected: path name\nfound : "+tok.toString());
|
|
100 |
ve1=findVertex(vertexID);
|
|
101 |
if (ve1==null) {
|
|
102 |
ve1=new NormalVertex("");
|
|
103 |
ve1.setID(vertexID);
|
|
104 |
ve1.setNumber(index++);
|
|
105 |
addVertex(ve1);
|
|
106 |
}
|
|
107 |
ve1.setPath(tok.sval);
|
|
108 |
ve1.setDir(dir);
|
|
109 |
ve1.setLabel(name);
|
|
110 |
tn.insertNode(name,dir,tok.sval,ve1.getNumber(),unfoldDir);
|
|
111 |
tok.nextToken();
|
|
112 |
if (tok.ttype=='<') {
|
|
113 |
children=true;
|
|
114 |
tok.nextToken();
|
|
115 |
} else if (tok.ttype=='>') {
|
|
116 |
children=false;
|
|
117 |
tok.nextToken();
|
|
118 |
} else children=true;
|
|
119 |
while (tok.ttype!=';') {
|
|
120 |
if (tok.ttype!=StreamTokenizer.TT_WORD && tok.ttype!='"')
|
|
121 |
throw new ParseError("expected: child vertex identifier or ';'\nfound : "+tok.toString());
|
|
122 |
ve2=findVertex(tok.sval);
|
|
123 |
if (ve2==null) {
|
|
124 |
ve2=new NormalVertex("");
|
|
125 |
ve2.setID(tok.sval);
|
|
126 |
ve2.setNumber(index++);
|
|
127 |
addVertex(ve2);
|
|
128 |
}
|
|
129 |
if (children)
|
|
130 |
ve1.addChild(ve2);
|
|
131 |
else
|
|
132 |
ve1.addParent(ve2);
|
|
133 |
tok.nextToken();
|
|
134 |
}
|
|
135 |
tok.nextToken();
|
|
136 |
}
|
|
137 |
vertices2 = new Vertex[vertices.size()];
|
|
138 |
vertices.copyInto(vertices2);
|
|
139 |
}
|
|
140 |
|
|
141 |
/*** Find vertex with identifier vertexID ***/
|
|
142 |
|
|
143 |
public Vertex findVertex(String vertexID) {
|
|
144 |
Enumeration e1=vertices.elements();
|
|
145 |
Vertex v1;
|
|
146 |
|
|
147 |
while (e1.hasMoreElements()) {
|
|
148 |
v1=(Vertex)(e1.nextElement());
|
|
149 |
if ((v1.getID()).equals(vertexID))
|
|
150 |
return v1;
|
|
151 |
}
|
|
152 |
return null;
|
|
153 |
}
|
|
154 |
|
|
155 |
public void addVertex(Vertex v) {
|
|
156 |
vertices.addElement(v);
|
|
157 |
v.setGraph(this);
|
|
158 |
}
|
|
159 |
|
|
160 |
public void removeVertex(Vertex v) {
|
|
161 |
vertices.removeElement(v);
|
|
162 |
}
|
|
163 |
|
|
164 |
public Enumeration getVertices() {
|
|
165 |
return vertices.elements();
|
|
166 |
}
|
|
167 |
|
|
168 |
/********************************************************************/
|
|
169 |
/* graph layout */
|
|
170 |
/********************************************************************/
|
|
171 |
|
|
172 |
public void layout(Graphics g) {
|
|
173 |
splines.removeAllElements();
|
|
174 |
hasseDiagram();
|
|
175 |
Vector layers=min_crossings(insert_dummies((Vector)(sort().elementAt(0))));
|
|
176 |
setParameters(g);
|
|
177 |
init_coordinates(layers);
|
|
178 |
pendulum(layers);
|
|
179 |
rubberband(layers);
|
|
180 |
calcSplines(layers);
|
|
181 |
calcBoundingBox();
|
|
182 |
}
|
|
183 |
|
|
184 |
/********************************************************************/
|
|
185 |
/* set layout parameters */
|
|
186 |
/********************************************************************/
|
|
187 |
|
|
188 |
public void setParameters(Graphics g) {
|
|
189 |
Enumeration e1=vertices.elements();
|
|
190 |
int h,w;
|
|
191 |
h=w=Integer.MIN_VALUE;
|
|
192 |
|
|
193 |
while (e1.hasMoreElements()) {
|
13968
|
194 |
Box dim=((Vertex)(e1.nextElement())).getLabelSize(g);
|
3599
|
195 |
h=Math.max(h,dim.height);
|
|
196 |
w=Math.max(w,dim.width);
|
|
197 |
}
|
|
198 |
box_height=h+4;
|
|
199 |
box_height2=box_height/2;
|
|
200 |
box_width=w+8;
|
|
201 |
box_width2=box_width/2;
|
|
202 |
box_hspace=box_width+20;
|
|
203 |
}
|
|
204 |
|
|
205 |
/********************************************************************/
|
|
206 |
/* topological sorting */
|
|
207 |
/********************************************************************/
|
|
208 |
|
|
209 |
public Vector sort() {
|
|
210 |
Vector todo=(Vector)(vertices.clone());
|
|
211 |
Vector layers=new Vector(10,10);
|
|
212 |
Vector todo2;
|
|
213 |
Enumeration e1,e2;
|
|
214 |
Vertex v,v2;
|
|
215 |
|
|
216 |
e1=vertices.elements();
|
|
217 |
while (e1.hasMoreElements())
|
|
218 |
((Vertex)(e1.nextElement())).setDegree(0);
|
|
219 |
|
|
220 |
e1=vertices.elements();
|
|
221 |
while (e1.hasMoreElements()) {
|
|
222 |
v=(Vertex)(e1.nextElement());
|
|
223 |
e2=v.getChildren();
|
|
224 |
while (e2.hasMoreElements()) {
|
|
225 |
v2=(Vertex)(e2.nextElement());
|
|
226 |
todo.removeElement(v2);
|
|
227 |
v2.setDegree(1+v2.getDegree());
|
|
228 |
}
|
|
229 |
}
|
|
230 |
while (!todo.isEmpty()) {
|
|
231 |
layers.addElement(todo);
|
|
232 |
todo2=new Vector(10,10);
|
|
233 |
e1=todo.elements();
|
|
234 |
while (e1.hasMoreElements()) {
|
|
235 |
e2=((Vertex)(e1.nextElement())).getChildren();
|
|
236 |
while (e2.hasMoreElements()) {
|
|
237 |
v=(Vertex)(e2.nextElement());
|
|
238 |
v.setDegree(v.getDegree()-1);
|
|
239 |
if (v.getDegree()==0) {
|
|
240 |
todo2.addElement(v);
|
|
241 |
v.setDegree(layers.size());
|
|
242 |
}
|
|
243 |
}
|
|
244 |
}
|
|
245 |
todo=todo2;
|
|
246 |
}
|
|
247 |
return layers;
|
|
248 |
}
|
|
249 |
|
|
250 |
/********************************************************************/
|
|
251 |
/* compute hasse diagram */
|
|
252 |
/********************************************************************/
|
|
253 |
|
|
254 |
public void hasseDiagram() {
|
|
255 |
Enumeration e1,e2;
|
|
256 |
Vertex vx1,vx2;
|
|
257 |
|
|
258 |
/** construct adjacence matrix **/
|
|
259 |
|
|
260 |
int vs=vertices.size();
|
|
261 |
boolean adj[][]=new boolean[vs][vs];
|
|
262 |
boolean adj2[][]=new boolean[vs][vs];
|
|
263 |
int i,j,k;
|
|
264 |
|
|
265 |
e1=getVertices();
|
|
266 |
for (i=0;i<vs;i++) {
|
|
267 |
vx1=(Vertex)(e1.nextElement());
|
|
268 |
e2=vx1.getChildren();
|
|
269 |
while (e2.hasMoreElements()) {
|
|
270 |
vx2=(Vertex)(e2.nextElement());
|
|
271 |
j=vertices.indexOf(vx2);
|
|
272 |
adj[i][j]=true;
|
|
273 |
adj2[i][j]=true;
|
|
274 |
}
|
|
275 |
}
|
|
276 |
|
|
277 |
/** compute transitive closure R+ **/
|
|
278 |
|
|
279 |
for (k=0;k<vs;k++)
|
|
280 |
for (i=0;i<vs;i++)
|
|
281 |
if (adj[i][k])
|
|
282 |
for (j=0;j<vs;j++)
|
|
283 |
adj[i][j] = adj[i][j] || adj[k][j];
|
|
284 |
|
|
285 |
/** compute R \ (R+)^2 **/
|
|
286 |
|
|
287 |
for (i=0;i<vs;i++)
|
|
288 |
for (j=0;j<vs;j++)
|
|
289 |
if (adj2[i][j]) {
|
|
290 |
vx1=(Vertex)(vertices.elementAt(i));
|
|
291 |
vx2=(Vertex)(vertices.elementAt(j));
|
|
292 |
for (k=0;k<vs;k++)
|
|
293 |
if (adj[i][k] && adj[k][j]) {
|
|
294 |
vx1.removeChild(vx2);
|
|
295 |
break;
|
|
296 |
}
|
|
297 |
}
|
|
298 |
}
|
|
299 |
|
|
300 |
/********************************************************************/
|
|
301 |
/* insert dummy vertices */
|
|
302 |
/********************************************************************/
|
|
303 |
|
|
304 |
public Vector insert_dummies(Vector v) {
|
|
305 |
Vector layers2=new Vector(10,10);
|
|
306 |
int n_edges;
|
|
307 |
|
|
308 |
do {
|
|
309 |
Enumeration e1=v.elements(),e2;
|
|
310 |
Vector next=new Vector(10,10);
|
|
311 |
|
|
312 |
layers2.addElement(v);
|
|
313 |
n_edges=0;
|
|
314 |
while (e1.hasMoreElements()) {
|
|
315 |
Vertex v1=(Vertex)(e1.nextElement());
|
|
316 |
e2=v1.getChildren();
|
|
317 |
while (e2.hasMoreElements()) {
|
|
318 |
n_edges++;
|
|
319 |
Vertex v2=(Vertex)(e2.nextElement());
|
|
320 |
if (v2.getDegree()!=v1.getDegree()+1) {
|
|
321 |
Vertex v3=new DummyVertex();
|
|
322 |
v3.addChild(v2);
|
|
323 |
v3.setDegree(v1.getDegree()+1);
|
|
324 |
v1.removeChild(v2);
|
|
325 |
v1.addChild(v3);
|
|
326 |
next.addElement(v3);
|
|
327 |
addVertex(v3);
|
|
328 |
} else if (next.indexOf(v2)<0) next.addElement(v2);
|
|
329 |
}
|
|
330 |
}
|
|
331 |
v=next;
|
|
332 |
numEdges.addElement(new Integer(n_edges));
|
|
333 |
} while (!v.isEmpty());
|
|
334 |
return layers2;
|
|
335 |
}
|
|
336 |
|
|
337 |
/********************************************************************/
|
|
338 |
/* calculation of crossings */
|
|
339 |
/********************************************************************/
|
|
340 |
|
|
341 |
public int count_crossings(Vector layers,int oldcr) {
|
|
342 |
int i,j,y1,y2,cr=0,l;
|
|
343 |
for (l=0;l<layers.size()-1;l++) {
|
|
344 |
Vector v1=(Vector)(layers.elementAt(l));
|
|
345 |
for (i=0;i<v1.size();i++) {
|
|
346 |
Enumeration e2=((Vertex)(v1.elementAt(i))).getChildren();
|
|
347 |
while (e2.hasMoreElements()) {
|
|
348 |
y1=((Vector)(layers.elementAt(l+1))).indexOf(e2.nextElement());
|
|
349 |
for (j=0;j<i;j++) {
|
|
350 |
Enumeration e3=((Vertex)(v1.elementAt(j))).getChildren();
|
|
351 |
while (e3.hasMoreElements()) {
|
|
352 |
y2=((Vector)(layers.elementAt(l+1))).indexOf(e3.nextElement());
|
|
353 |
if (y1<y2) {
|
|
354 |
cr++;
|
|
355 |
if (cr>=oldcr) return cr;
|
|
356 |
}
|
|
357 |
}
|
|
358 |
}
|
|
359 |
}
|
|
360 |
}
|
|
361 |
}
|
|
362 |
return cr;
|
|
363 |
}
|
|
364 |
|
|
365 |
/********************************************************************/
|
|
366 |
/* calculation of crossings where vertices vx1 and vx2 are involved */
|
|
367 |
/* vx1 and vx2 must be in same layer and vx1 is left from vx2 */
|
|
368 |
/********************************************************************/
|
|
369 |
|
|
370 |
public int count_crossings_2(Vector layers,Vertex vx1,Vertex vx2,int oldcr) {
|
|
371 |
int i,cr=0,l=vx1.getDegree();
|
|
372 |
Vertex va,vb;
|
|
373 |
Vector layer;
|
|
374 |
Enumeration e1,e2;
|
|
375 |
|
|
376 |
if (l>0) {
|
|
377 |
layer=(Vector)(layers.elementAt(l-1));
|
|
378 |
e1=vx1.getParents();
|
|
379 |
while (e1.hasMoreElements()) {
|
|
380 |
va=(Vertex)(e1.nextElement());
|
|
381 |
i=layer.indexOf(va);
|
|
382 |
e2=vx2.getParents();
|
|
383 |
while (e2.hasMoreElements()) {
|
|
384 |
vb=(Vertex)(e2.nextElement());
|
|
385 |
if (layer.indexOf(vb)<i) {
|
|
386 |
cr++;
|
|
387 |
if (cr>=oldcr) return cr;
|
|
388 |
}
|
|
389 |
}
|
|
390 |
}
|
|
391 |
}
|
|
392 |
if (l<layers.size()-1) {
|
|
393 |
layer=(Vector)(layers.elementAt(l+1));
|
|
394 |
e1=vx1.getChildren();
|
|
395 |
while (e1.hasMoreElements()) {
|
|
396 |
va=(Vertex)(e1.nextElement());
|
|
397 |
i=layer.indexOf(va);
|
|
398 |
e2=vx2.getChildren();
|
|
399 |
while (e2.hasMoreElements()) {
|
|
400 |
vb=(Vertex)(e2.nextElement());
|
|
401 |
if (layer.indexOf(vb)<i) {
|
|
402 |
cr++;
|
|
403 |
if (cr>=oldcr) return cr;
|
|
404 |
}
|
|
405 |
}
|
|
406 |
}
|
|
407 |
}
|
|
408 |
return cr;
|
|
409 |
}
|
|
410 |
|
|
411 |
/********************************************************************/
|
|
412 |
/* reduction of crossings by exchanging adjacent vertices */
|
|
413 |
/********************************************************************/
|
|
414 |
|
|
415 |
public void exchangeVertices(Vector layers,int oldcr) {
|
|
416 |
int i,l,c1,c2;
|
|
417 |
Vertex vx1,vx2;
|
|
418 |
Vector v1;
|
|
419 |
|
|
420 |
for (l=0;l<layers.size();l++) {
|
|
421 |
v1=(Vector)(layers.elementAt(l));
|
|
422 |
for (i=0;i<v1.size()-1;i++) {
|
|
423 |
vx1=(Vertex)(v1.elementAt(i));
|
|
424 |
vx2=(Vertex)(v1.elementAt(i+1));
|
|
425 |
c1=count_crossings_2(layers,vx1,vx2,oldcr);
|
|
426 |
c2=count_crossings_2(layers,vx2,vx1,c1);
|
|
427 |
if (c2<c1) {
|
|
428 |
v1.setElementAt(vx2,i);
|
|
429 |
v1.setElementAt(vx1,i+1);
|
|
430 |
}
|
|
431 |
}
|
|
432 |
}
|
|
433 |
}
|
|
434 |
|
|
435 |
/********************************************************************/
|
|
436 |
/* minimization of crossings */
|
|
437 |
/********************************************************************/
|
|
438 |
|
|
439 |
public Vector min_crossings(Vector layers) {
|
|
440 |
int n,i,l,k,z=0,cr2,cr=count_crossings(layers,Integer.MAX_VALUE);
|
|
441 |
boolean topdown=true,first=true;
|
|
442 |
Enumeration e1,e2;
|
|
443 |
Vector v1,v2,layers2=null,best=layers;
|
|
444 |
Vertex vx1,vx2;
|
|
445 |
n=0;
|
|
446 |
while (n<3 && cr>0) {
|
|
447 |
if (topdown) {
|
|
448 |
/** top-down-traversal **/
|
|
449 |
|
|
450 |
layers2=new Vector(10,10);
|
|
451 |
for (l=0;l<layers.size();l++) {
|
|
452 |
v1=(Vector)(layers.elementAt(l));
|
|
453 |
if (l==0) layers2.addElement(v1.clone());
|
|
454 |
else {
|
|
455 |
v2=new Vector(10,10);
|
|
456 |
layers2.addElement(v2);
|
|
457 |
e1=v1.elements();
|
|
458 |
while (e1.hasMoreElements()) {
|
|
459 |
vx1=(Vertex)(e1.nextElement());
|
|
460 |
k=0;z=0;
|
|
461 |
e2=vx1.getParents();
|
|
462 |
while (e2.hasMoreElements()) {
|
|
463 |
k+=((Vector)(layers2.elementAt(l-1))).indexOf(e2.nextElement());
|
|
464 |
z++;
|
|
465 |
}
|
|
466 |
if (z>0)
|
|
467 |
vx1.setWeight(((double)(k))/z);
|
|
468 |
else if (first)
|
|
469 |
vx1.setWeight(Double.MAX_VALUE);
|
|
470 |
for (i=0;i<v2.size();i++)
|
|
471 |
if (vx1.getWeight()<((Vertex)(v2.elementAt(i))).getWeight()) break;
|
|
472 |
if (i==v2.size()) v2.addElement(vx1);
|
|
473 |
else v2.insertElementAt(vx1,i);
|
|
474 |
}
|
|
475 |
}
|
|
476 |
}
|
|
477 |
} else {
|
|
478 |
/** bottom-up-traversal **/
|
|
479 |
|
|
480 |
layers2=new Vector(10,10);
|
|
481 |
for (l=layers.size()-1;l>=0;l--) {
|
|
482 |
v1=(Vector)(layers.elementAt(l));
|
|
483 |
if (l==layers.size()-1) layers2.addElement(v1.clone());
|
|
484 |
else {
|
|
485 |
v2=new Vector(10,10);
|
|
486 |
layers2.insertElementAt(v2,0);
|
|
487 |
e1=v1.elements();
|
|
488 |
while (e1.hasMoreElements()) {
|
|
489 |
vx1=(Vertex)(e1.nextElement());
|
|
490 |
k=0;z=0;
|
|
491 |
e2=vx1.getChildren();
|
|
492 |
while (e2.hasMoreElements()) {
|
|
493 |
k+=((Vector)(layers2.elementAt(1))).indexOf(e2.nextElement());
|
|
494 |
z++;
|
|
495 |
}
|
|
496 |
if (z>0)
|
|
497 |
vx1.setWeight(((double)(k))/z);
|
|
498 |
else if (first)
|
|
499 |
vx1.setWeight(Double.MAX_VALUE);
|
|
500 |
for (i=0;i<v2.size();i++)
|
|
501 |
if (vx1.getWeight()<((Vertex)(v2.elementAt(i))).getWeight()) break;
|
|
502 |
if (i==v2.size()) v2.addElement(vx1);
|
|
503 |
else v2.insertElementAt(vx1,i);
|
|
504 |
}
|
|
505 |
}
|
|
506 |
}
|
|
507 |
}
|
|
508 |
//exchangeVertices(layers2,cr);
|
|
509 |
topdown=!topdown;
|
|
510 |
first=false;
|
|
511 |
layers=layers2;
|
|
512 |
|
|
513 |
cr2=count_crossings(layers2,cr);
|
|
514 |
if (cr2<cr) {
|
|
515 |
best=layers2;
|
|
516 |
cr=cr2;
|
|
517 |
} else n++;
|
|
518 |
}
|
|
519 |
|
|
520 |
while (true) {
|
|
521 |
exchangeVertices(best,cr);
|
|
522 |
cr2=count_crossings(best,cr);
|
|
523 |
if (cr2<cr)
|
|
524 |
cr=cr2;
|
|
525 |
else
|
|
526 |
break;
|
|
527 |
}
|
|
528 |
|
|
529 |
return best;
|
|
530 |
}
|
|
531 |
|
|
532 |
/********************************************************************/
|
|
533 |
/* set initial coordinates */
|
|
534 |
/********************************************************************/
|
|
535 |
|
|
536 |
public void init_coordinates(Vector layers) {
|
|
537 |
int y=0;
|
|
538 |
Enumeration e1=layers.elements();
|
|
539 |
Enumeration e3=numEdges.elements();
|
|
540 |
while (e1.hasMoreElements()) {
|
|
541 |
Vector v1=(Vector)(e1.nextElement());
|
|
542 |
Enumeration e2=v1.elements();
|
|
543 |
int x=box_width2;
|
|
544 |
while (e2.hasMoreElements()) {
|
|
545 |
Vertex ve=(Vertex)(e2.nextElement());
|
|
546 |
ve.setX(x);
|
|
547 |
ve.setY(y);
|
|
548 |
x+=box_hspace;
|
|
549 |
}
|
|
550 |
y+=box_height+Math.max(35,7*(((Integer)(e3.nextElement())).intValue()));
|
|
551 |
}
|
|
552 |
}
|
|
553 |
|
|
554 |
/********************************************************************/
|
|
555 |
/* pendulum method */
|
|
556 |
/********************************************************************/
|
|
557 |
|
|
558 |
public void pendulum(Vector layers) {
|
|
559 |
Vector layers2=new Vector(10,10);
|
|
560 |
Enumeration e1=layers.elements(),e2;
|
|
561 |
int i,j,d1,d2,k,offset,dsum;
|
|
562 |
Region r1,r2;
|
|
563 |
boolean change;
|
|
564 |
|
|
565 |
while (e1.hasMoreElements()) {
|
|
566 |
e2=((Vector)(e1.nextElement())).elements();
|
|
567 |
Vector layer=new Vector(10,10);
|
|
568 |
layers2.addElement(layer);
|
|
569 |
while (e2.hasMoreElements()) {
|
|
570 |
Region r=new Region(this);
|
|
571 |
r.addVertex((Vertex)(e2.nextElement()));
|
|
572 |
layer.addElement(r);
|
|
573 |
}
|
|
574 |
}
|
|
575 |
for (k=0;k<10;k++) {
|
|
576 |
dsum=0;
|
|
577 |
for (j=1;j<layers2.size();j++) {
|
|
578 |
Vector l=(Vector)(layers2.elementAt(j));
|
|
579 |
if (l.size()>=2) {
|
|
580 |
do {
|
|
581 |
change=false;
|
|
582 |
d1=((Region)(l.firstElement())).pred_deflection();
|
|
583 |
for (i=0;i<l.size()-1;i++) {
|
|
584 |
r1=(Region)(l.elementAt(i));
|
|
585 |
r2=(Region)(l.elementAt(i+1));
|
|
586 |
d2=r2.pred_deflection();
|
|
587 |
if (r1.touching(r2) && (d1 <= 0 && d2 < d1 ||
|
|
588 |
d2 > 0 && d1 > d2 || d1 > 0 && d2 < 0)) {
|
|
589 |
r1.combine(r2);
|
|
590 |
l.removeElement(r2);
|
|
591 |
change=true;
|
|
592 |
d2=r1.pred_deflection();
|
|
593 |
}
|
|
594 |
d1=d2;
|
|
595 |
}
|
|
596 |
} while (change);
|
|
597 |
}
|
|
598 |
for (i=0;i<l.size();i++) {
|
|
599 |
r1=(Region)(l.elementAt(i));
|
|
600 |
d1=r1.pred_deflection();
|
|
601 |
offset=d1;
|
|
602 |
if (d1<0 && i>0) offset=-Math.min(
|
|
603 |
((Region)(l.elementAt(i-1))).spaceBetween(r1),-d1);
|
|
604 |
if (d1>=0 && i<l.size()-1) offset=Math.min(
|
|
605 |
r1.spaceBetween((Region)(l.elementAt(i+1))),d1);
|
|
606 |
r1.move(offset);
|
|
607 |
dsum+=Math.abs(d1);
|
|
608 |
}
|
|
609 |
}
|
|
610 |
if (dsum==0) break;
|
|
611 |
}
|
|
612 |
}
|
|
613 |
|
|
614 |
/********************************************************************/
|
|
615 |
/* rubberband method */
|
|
616 |
/********************************************************************/
|
|
617 |
|
|
618 |
public void rubberband(Vector layers) {
|
|
619 |
Enumeration e1,e2;
|
|
620 |
int i,n,k,d,d2;
|
|
621 |
Vector v;
|
|
622 |
Vertex vx;
|
|
623 |
|
|
624 |
for (k=0;k<10;k++) {
|
|
625 |
e1=layers.elements();
|
|
626 |
while (e1.hasMoreElements()) {
|
|
627 |
v=(Vector)(e1.nextElement());
|
|
628 |
for (i=0;i<v.size();i++) {
|
|
629 |
n=0;d=0;
|
|
630 |
vx=(Vertex)(v.elementAt(i));
|
|
631 |
e2=vx.getChildren();
|
|
632 |
while (e2.hasMoreElements()) {
|
|
633 |
d+=((Vertex)(e2.nextElement())).getX()-vx.getX();
|
|
634 |
n++;
|
|
635 |
}
|
|
636 |
e2=vx.getParents();
|
|
637 |
while (e2.hasMoreElements()) {
|
|
638 |
d+=((Vertex)(e2.nextElement())).getX()-vx.getX();
|
|
639 |
n++;
|
|
640 |
}
|
|
641 |
d2=(n!=0?d/n:0);
|
|
642 |
|
|
643 |
if (d<0 && (i==0 || ((Vertex)(v.elementAt(i-1))).rightX()+box_hspace-box_width < vx.leftX()+d2) ||
|
|
644 |
d>0 && (i==v.size()-1 || ((Vertex)(v.elementAt(i+1))).leftX()-box_hspace+box_width > vx.rightX()+d2))
|
|
645 |
vx.setX(vx.getX()+d2);
|
|
646 |
}
|
|
647 |
}
|
|
648 |
}
|
|
649 |
}
|
|
650 |
|
|
651 |
/**** Intersection point of two lines (auxiliary function for calcSplines) ****/
|
|
652 |
/**** Calculate intersection point of line which is parallel to line (p1,p2) ****/
|
|
653 |
/**** and leads through p5, with line (p3,p4) ****/
|
|
654 |
|
|
655 |
Point intersect(Point p1,Point p2,Point p3,Point p4,Point p5) {
|
|
656 |
float x=0,y=0,s1=0,s2=0;
|
|
657 |
|
|
658 |
if (p1.x!=p2.x)
|
|
659 |
s1=((float)(p2.y-p1.y))/(p2.x-p1.x);
|
|
660 |
if (p3.x!=p4.x)
|
|
661 |
s2=((float)(p4.y-p3.y))/(p4.x-p3.x);
|
|
662 |
if (p1.x==p2.x) {
|
|
663 |
x=p5.x;
|
|
664 |
y=s2*(p5.x-p3.x)+p3.y;
|
|
665 |
} else if (p3.x==p4.x) {
|
|
666 |
x=p3.x;
|
|
667 |
y=s1*(p3.x-p5.x)+p5.y;
|
|
668 |
} else {
|
|
669 |
x=(p5.x*s1-p3.x*s2+p3.y-p5.y)/(s1-s2);
|
|
670 |
y=s2*(x-p3.x)+p3.y;
|
|
671 |
}
|
|
672 |
return new Point(Math.round(x),Math.round(y));
|
|
673 |
}
|
|
674 |
|
|
675 |
/**** Calculate control points (auxiliary function for calcSplines) ****/
|
|
676 |
|
|
677 |
Points calcPoint(Point p1,Point p2,Point p3,int lboxx,int rboxx,int boxy) {
|
|
678 |
|
|
679 |
/*** Points p1 , p2 , p3 define a triangle which encloses the spline. ***/
|
|
680 |
/*** Check if adjacent boxes (specified by lboxx,rboxx and boxy) ***/
|
|
681 |
/*** collide with the spline. In this case p1 and p3 are shifted by an ***/
|
|
682 |
/*** appropriate offset before they are returned ***/
|
|
683 |
|
|
684 |
int xh1,xh2,bx=0,by=0;
|
|
685 |
boolean pt1 = boxy >= p1.y && boxy <= p3.y || boxy >= p3.y && boxy <= p1.y;
|
|
686 |
boolean pt2 = boxy+box_height >= p1.y && boxy+box_height <= p3.y ||
|
|
687 |
boxy+box_height >= p3.y && boxy+box_height <= p1.y;
|
|
688 |
boolean move = false;
|
|
689 |
Point b;
|
|
690 |
|
|
691 |
xh1 = p1.x+(boxy-p1.y)*(p3.x-p1.x)/(p3.y-p1.y);
|
|
692 |
xh2 = p1.x+(boxy+box_height-p1.y)*(p3.x-p1.x)/(p3.y-p1.y);
|
|
693 |
|
|
694 |
if (xh1 <= lboxx && pt1 && xh2 <= lboxx && pt2) {
|
|
695 |
move = true;
|
|
696 |
bx = lboxx;
|
|
697 |
by = boxy + (xh1 < xh2 ? 0 : box_height ) ;
|
|
698 |
} else if (xh1 >= rboxx && pt1 && xh2 >= rboxx && pt2) {
|
|
699 |
move = true;
|
|
700 |
bx = rboxx;
|
|
701 |
by = boxy + (xh1 > xh2 ? 0 : box_height ) ;
|
|
702 |
} else if ( (xh1 <= lboxx || xh1 >= rboxx) && pt1) {
|
|
703 |
move = true;
|
|
704 |
bx = (xh1 <= lboxx ? lboxx : rboxx ) ;
|
|
705 |
by = boxy;
|
|
706 |
} else if ( (xh2 <= lboxx || xh2 >= rboxx) && pt2) {
|
|
707 |
move = true;
|
|
708 |
bx = (xh2 <= lboxx ? lboxx : rboxx ) ;
|
|
709 |
by = boxy+box_height;
|
|
710 |
}
|
|
711 |
b=new Point(bx,by);
|
|
712 |
if (move) return new Points(intersect(p1,p3,p1,p2,b),intersect(p1,p3,p2,p3,b));
|
|
713 |
else return new Points(p1,p3);
|
|
714 |
}
|
|
715 |
|
|
716 |
/********************************************************************/
|
|
717 |
/* calculate splines */
|
|
718 |
/********************************************************************/
|
|
719 |
|
|
720 |
public void calcSplines(Vector layers) {
|
|
721 |
Enumeration e2,e1=vertices.elements();
|
|
722 |
Vertex vx1,vx2,vx3;
|
|
723 |
Vector pos,layer;
|
|
724 |
int x1,y1,x2,y2,x3,y3,xh,k,leftx,rightx,spc;
|
|
725 |
|
|
726 |
while (e1.hasMoreElements()) {
|
|
727 |
vx1=(Vertex)(e1.nextElement());
|
|
728 |
if (!vx1.isDummy()) {
|
|
729 |
e2=vx1.getChildren();
|
|
730 |
while (e2.hasMoreElements()) {
|
|
731 |
vx2=(Vertex)(e2.nextElement());
|
|
732 |
if (vx2.isDummy()) {
|
|
733 |
vx3=vx2;
|
|
734 |
/**** convert edge to spline ****/
|
|
735 |
pos=new Vector(10,10);
|
|
736 |
x1=vx1.getX();
|
|
737 |
y1=vx1.getY()+box_height;
|
|
738 |
|
|
739 |
do {
|
|
740 |
/*** calculate position of control points ***/
|
|
741 |
x2=vx2.getX();
|
|
742 |
y2=vx2.getY();
|
|
743 |
layer=(Vector)(layers.elementAt(vx2.getDegree()));
|
|
744 |
k=layer.indexOf(vx2);
|
|
745 |
vx2=(Vertex)((vx2.getChildren()).nextElement());
|
|
746 |
x3=vx2.getX();
|
|
747 |
y3=vx2.getY();
|
|
748 |
// spc=(box_hspace-box_width)/3;
|
|
749 |
// spc=box_height*3/4;
|
|
750 |
spc=0;
|
|
751 |
leftx = k==0 /* || ((Vertex)(layer.elementAt(k-1))).isDummy() */ ?
|
|
752 |
Integer.MIN_VALUE:
|
|
753 |
((Vertex)(layer.elementAt(k-1))).rightX()+spc;
|
|
754 |
rightx = k==layer.size()-1 /* || ((Vertex)(layer.elementAt(k+1))).isDummy() */ ?
|
|
755 |
Integer.MAX_VALUE:
|
|
756 |
((Vertex)(layer.elementAt(k+1))).leftX()-spc;
|
|
757 |
xh=x2+box_height*(x3-x2)/(y3-y2);
|
|
758 |
if (!(x2<=x3 && xh>=rightx || x2>x3 && xh<=leftx)) {
|
|
759 |
/* top control point */
|
|
760 |
pos.addElement(new Integer(1));
|
|
761 |
y1=y2;
|
|
762 |
} else {
|
|
763 |
xh=x1+(y2-y1)*(x2-x1)/(y2+box_height-y1);
|
|
764 |
if (!(x2<=x1 && xh>=rightx || x2>x1 && xh<=leftx))
|
|
765 |
/* bottom control point */
|
|
766 |
pos.addElement(new Integer(2));
|
|
767 |
else
|
|
768 |
/* two control points needed */
|
|
769 |
pos.addElement(new Integer(3));
|
|
770 |
y1=y2+box_height;
|
|
771 |
}
|
|
772 |
x1=x2;
|
|
773 |
} while (vx2.isDummy());
|
|
774 |
pos.addElement(new Integer(1));
|
|
775 |
|
|
776 |
/**** calculate triangles ****/
|
|
777 |
vx2=vx3;
|
|
778 |
|
|
779 |
int pos1,pos2,i=0;
|
|
780 |
Vector pts=new Vector(10,10);
|
|
781 |
int lboxx,rboxx,boxy;
|
|
782 |
|
|
783 |
x1=vx1.getX();
|
|
784 |
y1=vx1.getY()+box_height;
|
|
785 |
pts.addElement(new Point(x1,y1)); /** edge starting point **/
|
|
786 |
do {
|
|
787 |
x2=vx2.getX();
|
|
788 |
y2=vx2.getY();
|
|
789 |
pos1=((Integer)(pos.elementAt(i))).intValue();
|
|
790 |
pos2=((Integer)(pos.elementAt(i+1))).intValue();
|
|
791 |
i++;
|
|
792 |
layer=(Vector)(layers.elementAt(vx2.getDegree()));
|
|
793 |
k=layer.indexOf(vx2);
|
|
794 |
boxy=vx2.getY();
|
|
795 |
vx2=(Vertex)((vx2.getChildren()).nextElement());
|
|
796 |
x3=vx2.getX();
|
|
797 |
y3=vx2.getY();
|
|
798 |
if (pos1==2) y2+=box_height;
|
|
799 |
if (pos2==2) y3+=box_height;
|
|
800 |
|
|
801 |
lboxx = (k==0 /* || ((Vertex)(layer.elementAt(k-1))).isDummy() */ ) ?
|
|
802 |
Integer.MIN_VALUE :
|
|
803 |
((Vertex)(layer.elementAt(k-1))).rightX();
|
|
804 |
|
|
805 |
rboxx = (k==layer.size()-1 /* || ((Vertex)(layer.elementAt(k+1))).isDummy() */ ) ?
|
|
806 |
Integer.MAX_VALUE :
|
|
807 |
((Vertex)(layer.elementAt(k+1))).leftX();
|
|
808 |
|
|
809 |
Point p1,p2,p3;
|
|
810 |
Points ps;
|
|
811 |
|
|
812 |
p1 = new Point((x1+x2)/2,(y1+y2)/2);
|
|
813 |
|
|
814 |
if (pos1<=2) {
|
|
815 |
/** one control point **/
|
|
816 |
p2 = new Point(x2,y2);
|
|
817 |
ps = calcPoint(p1,p2,new Point((x2+x3)/2,(y2+y3)/2),lboxx,rboxx,boxy);
|
|
818 |
pts.addElement(ps.p);
|
|
819 |
pts.addElement(p2);
|
|
820 |
pts.addElement(ps.q);
|
|
821 |
} else {
|
|
822 |
/** two control points **/
|
|
823 |
p2 = new Point(x2,y2-box_height);
|
|
824 |
p3 = new Point(x2,y2+box_height2);
|
|
825 |
ps = calcPoint(p1,p2,p3,lboxx,rboxx,boxy);
|
|
826 |
pts.addElement(ps.p);
|
|
827 |
pts.addElement(p2);
|
|
828 |
pts.addElement(ps.q);
|
|
829 |
p2 = new Point(x2,y2+box_height*2);
|
|
830 |
ps = calcPoint(p3,p2,new Point((p2.x+x3)/2,(p2.y+y3)/2),
|
|
831 |
lboxx,rboxx,boxy);
|
|
832 |
pts.addElement(ps.p);
|
|
833 |
pts.addElement(p2);
|
|
834 |
pts.addElement(ps.q);
|
|
835 |
}
|
|
836 |
x1=p2.x;
|
|
837 |
y1=p2.y;
|
|
838 |
} while (vx2.isDummy());
|
|
839 |
|
|
840 |
pts.addElement(new Point(vx2.getX(),vx2.getY())); /** edge end point **/
|
|
841 |
splines.addElement(new Spline(pts));
|
|
842 |
}
|
|
843 |
}
|
|
844 |
}
|
|
845 |
}
|
|
846 |
}
|
|
847 |
|
|
848 |
/********************************************************************/
|
|
849 |
/* calculate bounding box */
|
|
850 |
/********************************************************************/
|
|
851 |
|
|
852 |
public void calcBoundingBox() {
|
|
853 |
min_y=min_x=Integer.MAX_VALUE;
|
|
854 |
max_y=max_x=Integer.MIN_VALUE;
|
|
855 |
|
|
856 |
Enumeration e1=vertices.elements();
|
|
857 |
Vertex v;
|
|
858 |
|
|
859 |
while (e1.hasMoreElements()) {
|
|
860 |
v=(Vertex)(e1.nextElement());
|
|
861 |
min_x=Math.min(min_x,v.leftX());
|
|
862 |
max_x=Math.max(max_x,v.rightX());
|
|
863 |
min_y=Math.min(min_y,v.getY());
|
|
864 |
max_y=Math.max(max_y,v.getY()+box_height);
|
|
865 |
}
|
|
866 |
min_x-=20;
|
|
867 |
min_y-=20;
|
|
868 |
max_x+=20;
|
|
869 |
max_y+=20;
|
|
870 |
}
|
|
871 |
|
|
872 |
/********************************************************************/
|
|
873 |
/* draw graph */
|
|
874 |
/********************************************************************/
|
|
875 |
|
|
876 |
public void draw(Graphics g) {
|
|
877 |
if (box_height==0) layout(g);
|
|
878 |
|
|
879 |
g.translate(-min_x,-min_y);
|
|
880 |
|
|
881 |
Enumeration e1=vertices.elements();
|
|
882 |
while (e1.hasMoreElements())
|
|
883 |
((Vertex)(e1.nextElement())).draw(g);
|
|
884 |
|
|
885 |
e1=splines.elements();
|
|
886 |
while (e1.hasMoreElements())
|
|
887 |
((Spline)(e1.nextElement())).draw(g);
|
|
888 |
}
|
|
889 |
|
|
890 |
/********************************************************************/
|
|
891 |
/* return vertex at position (x,y) */
|
|
892 |
/********************************************************************/
|
|
893 |
|
|
894 |
public Vertex vertexAt(int x,int y) {
|
|
895 |
Enumeration e1=vertices.elements();
|
|
896 |
while (e1.hasMoreElements()) {
|
|
897 |
Vertex v=(Vertex)(e1.nextElement());
|
|
898 |
if (v.contains(x,y)) return v;
|
|
899 |
}
|
|
900 |
return null;
|
|
901 |
}
|
|
902 |
|
|
903 |
/********************************************************************/
|
|
904 |
/* encode list of vertices (as array of vertice numbers) */
|
|
905 |
/********************************************************************/
|
|
906 |
|
|
907 |
public Vector encode(Vector v) {
|
|
908 |
Vector code=new Vector(10,10);
|
|
909 |
Enumeration e1=v.elements();
|
|
910 |
|
|
911 |
while (e1.hasMoreElements()) {
|
|
912 |
Vertex vx=(Vertex)(e1.nextElement());
|
|
913 |
if (vx.getNumber()>=0)
|
|
914 |
code.addElement(new Integer(vx.getNumber()));
|
|
915 |
}
|
|
916 |
return code;
|
|
917 |
}
|
|
918 |
|
|
919 |
/********************************************************************/
|
|
920 |
/* get vertex with number n */
|
|
921 |
/********************************************************************/
|
|
922 |
|
|
923 |
public Vertex getVertexByNum(int x) {
|
|
924 |
Enumeration e1=vertices.elements();
|
|
925 |
|
|
926 |
while (e1.hasMoreElements()) {
|
|
927 |
Vertex vx=(Vertex)(e1.nextElement());
|
|
928 |
if (vx.getNumber()==x) return vx;
|
|
929 |
}
|
|
930 |
return null;
|
|
931 |
}
|
|
932 |
|
|
933 |
/********************************************************************/
|
|
934 |
/* decode list of vertices */
|
|
935 |
/********************************************************************/
|
|
936 |
|
|
937 |
public Vector decode(Vector code) {
|
|
938 |
Enumeration e1=code.elements();
|
|
939 |
Vector vec=new Vector(10,10);
|
|
940 |
|
|
941 |
while (e1.hasMoreElements()) {
|
|
942 |
int i=((Integer)(e1.nextElement())).intValue();
|
|
943 |
//Vertex vx=getVertexByNum(i);
|
|
944 |
//if (vx!=null) vec.addElement(vx);
|
|
945 |
vec.addElement(vertices2[i]);
|
|
946 |
}
|
|
947 |
return vec;
|
|
948 |
}
|
|
949 |
|
|
950 |
/********************************************************************/
|
|
951 |
/* collapse vertices */
|
|
952 |
/********************************************************************/
|
|
953 |
|
|
954 |
public void collapse(Vector vs,String name,Vector inflate) {
|
|
955 |
Enumeration e1,e2,e3;
|
|
956 |
boolean nonempty=false;
|
|
957 |
Vertex vx3,vx2,vx1;
|
|
958 |
|
|
959 |
e1=vertices.elements();
|
|
960 |
|
|
961 |
vx1=new NormalVertex(name);
|
|
962 |
vx1.setInflate(inflate);
|
|
963 |
|
|
964 |
while (e1.hasMoreElements()) {
|
|
965 |
vx2=(Vertex)(e1.nextElement());
|
|
966 |
|
|
967 |
if (vs.indexOf(vx2)<0) {
|
|
968 |
e2=vx2.getParents();
|
|
969 |
while (e2.hasMoreElements()) {
|
|
970 |
vx3=(Vertex)(e2.nextElement());
|
|
971 |
if (vs.indexOf(vx3)>=0) {
|
|
972 |
if (!vx1.isChild(vx2))
|
|
973 |
vx1.addChild(vx2);
|
|
974 |
vx3.removeChild(vx2);
|
|
975 |
}
|
|
976 |
}
|
|
977 |
|
|
978 |
e2=vx2.getChildren();
|
|
979 |
while (e2.hasMoreElements()) {
|
|
980 |
vx3=(Vertex)(e2.nextElement());
|
|
981 |
if (vs.indexOf(vx3)>=0) {
|
|
982 |
if (!vx2.isChild(vx1))
|
|
983 |
vx2.addChild(vx1);
|
|
984 |
vx2.removeChild(vx3);
|
|
985 |
}
|
|
986 |
}
|
|
987 |
} else { nonempty=true; }
|
|
988 |
}
|
|
989 |
|
|
990 |
e1=vs.elements();
|
|
991 |
while (e1.hasMoreElements())
|
|
992 |
try {
|
|
993 |
removeVertex((Vertex)(e1.nextElement()));
|
|
994 |
} catch (NoSuchElementException exn) {}
|
|
995 |
|
|
996 |
if (nonempty) addVertex(vx1);
|
|
997 |
}
|
|
998 |
|
|
999 |
/********************************************************************/
|
|
1000 |
/* PostScript output */
|
|
1001 |
/********************************************************************/
|
|
1002 |
|
|
1003 |
public void PS(String fname,boolean printable) throws IOException {
|
|
1004 |
FileOutputStream f = new FileOutputStream(fname);
|
6541
|
1005 |
PrintWriter p = new PrintWriter(f, true);
|
3599
|
1006 |
|
|
1007 |
if (printable)
|
|
1008 |
p.println("%!PS-Adobe-2.0\n\n%%BeginProlog");
|
|
1009 |
else {
|
|
1010 |
p.println("%!PS-Adobe-2.0 EPSF-2.0\n%%Orientation: Portrait");
|
|
1011 |
p.println("%%BoundingBox: "+min_x+" "+min_y+" "+max_x+" "+max_y);
|
|
1012 |
p.println("%%EndComments\n\n%%BeginProlog");
|
|
1013 |
}
|
|
1014 |
p.println("/m { moveto } def /l { lineto } def /n { newpath } def");
|
|
1015 |
p.println("/s { stroke } def /c { curveto } def");
|
|
1016 |
p.println("/b { n 0 0 m dup true charpath pathbbox 1 index 4 index sub");
|
|
1017 |
p.println("7 index exch sub 2 div 9 index add 1 index 4 index sub 7 index exch sub");
|
|
1018 |
p.println("2 div 9 index add 2 index add m pop pop pop pop");
|
|
1019 |
p.println("1 -1 scale show 1 -1 scale n 3 index 3 index m 1 index 0 rlineto");
|
|
1020 |
p.println("0 exch rlineto neg 0 rlineto closepath s pop pop } def");
|
|
1021 |
p.println("%%EndProlog\n");
|
|
1022 |
if (printable) {
|
|
1023 |
int hsize=max_x-min_x;
|
|
1024 |
int vsize=max_y-min_y;
|
|
1025 |
if (hsize>vsize) {
|
|
1026 |
// Landscape output
|
|
1027 |
double scale=Math.min(1,Math.min(750.0/hsize,500.0/vsize));
|
|
1028 |
double trans_x=50+max_y*scale+(500-scale*vsize)/2.0;
|
|
1029 |
double trans_y=50+max_x*scale+(750-scale*hsize)/2.0;
|
|
1030 |
p.println(trans_x+" "+trans_y+" translate");
|
|
1031 |
p.println("-90 rotate");
|
|
1032 |
p.println(scale+" "+(-scale)+" scale");
|
|
1033 |
} else {
|
|
1034 |
// Portrait output
|
|
1035 |
double scale=Math.min(1,Math.min(500.0/hsize,750.0/vsize));
|
|
1036 |
double trans_x=50-min_x*scale+(500-scale*hsize)/2.0;
|
|
1037 |
double trans_y=50+max_y*scale+(750-scale*vsize)/2.0;
|
|
1038 |
p.println(trans_x+" "+trans_y+" translate");
|
|
1039 |
p.println(scale+" "+(-scale)+" scale");
|
|
1040 |
}
|
|
1041 |
} else
|
|
1042 |
p.println("0 "+(max_y+min_y)+" translate\n1 -1 scale");
|
|
1043 |
|
|
1044 |
p.println("/Helvetica findfont 12 scalefont setfont");
|
|
1045 |
p.println("0.5 setlinewidth");
|
|
1046 |
|
|
1047 |
Enumeration e1=vertices.elements();
|
|
1048 |
while (e1.hasMoreElements())
|
|
1049 |
((Vertex)(e1.nextElement())).PS(p);
|
|
1050 |
|
|
1051 |
e1=splines.elements();
|
|
1052 |
while (e1.hasMoreElements())
|
|
1053 |
((Spline)(e1.nextElement())).PS(p);
|
|
1054 |
|
|
1055 |
if (printable) p.println("showpage");
|
|
1056 |
|
|
1057 |
f.close();
|
|
1058 |
}
|
|
1059 |
}
|
|
1060 |
|
|
1061 |
/**** Return value of function calcPoint ****/
|
|
1062 |
|
|
1063 |
class Points {
|
|
1064 |
public Point p,q;
|
|
1065 |
|
|
1066 |
public Points(Point p1,Point p2) {
|
|
1067 |
p=p1;q=p2;
|
|
1068 |
}
|
|
1069 |
}
|
|
1070 |
|