Debian Codegraph @

texmf-dist/metapost/automata/automata.mp

raw
1 % PACKAGE FOR METAPOST TO DRAW FINITE STATE MACHINES, GRAPHS, TREES, ETC.
2 % Author: gabriele puppis
3
4 % declares a grid of equally-spaced nodes
5 %
6 % syntax: matrix.<name>(<#rows>,<#cols>);
7 %
8 % example: matrix.a(2,3);
9 %
10 % parameters: spacing = (60,30) | ...
11 % offset = (0,0) | ...
12 % numbering = (0,0) | ...
13 %
14 % (nodes are identified by <name>[<row>][<col>],
15 % <name> can't be c,h,x,y or any other global variable,
16 % the center/anchor of a node is <name>[<row>][<col>].c/.h,
17 % <row> and <col> start from 0,
18 % so <name>[0][0] is the top left-most node,
19 % <name>[<#rows>-1][<#cols>-1] is the bottom right-most node)
20 vardef matrix@# expr d =
21 pair @#[][].c;
22 pair @#[][].h;
23 pair @#[][].s;
24 path @#[][].f;
25 numeric @#[][].m;
26
27 for i=0 upto xpart(d)-1:
28 for j=0 upto ypart(d)-1:
29 @#[xpart(numbering)+i][ypart(numbering)+j].c :=
30 (xpart(spacing)*j,-ypart(spacing)*i) shifted offset;
31 endfor;
32 endfor;
33 enddef;
34
35
36 % declares a tree-shaped arrangement of nodes
37 %
38 % syntax: tree.<name>(<#levels>,<arity>);
39 %
40 % example: tree.a(4,2);
41 %
42 % parameters: spacing = (60,30) | ...
43 % offset = (0,0) | ...
44 % numbering = (0,0) | ...
45 %
46 % (nodes are identified by <name>[<depth>][<child>],
47 % <name> can't be c,h,x,y or any other global variable,
48 % the center/anchor of a node is <name>[<depth>][<child>].c/.h,
49 % <depth> and <child> start from 0,
50 % so <name>[0][0] is the root,
51 % <name>[1][0] ... <name>[1][<arity>-1] are the children of the root,
52 % and so on)
53 vardef tree@# expr d =
54 pair @#[][].c;
55 pair @#[][].h;
56 pair @#[][].s;
57 path @#[][].f;
58 numeric @#[][].m;
59
60 for i=0 upto xpart(d)-1:
61 for j=0 upto (ypart(d)**i) - 1:
62 @#[xpart(numbering)+i][ypart(numbering)+j].c =
63 (xpart(spacing) * ((j+0.5)*(ypart(d)**(xpart(d)-i)) - 0.5*(ypart(d)**xpart(d))),
64 -ypart(spacing) * i) shifted offset;
65 endfor;
66 endfor;
67 enddef;
68
69
70 % declares a single node
71 %
72 % syntax: put.<name>(x,y);
73 %
74 % example: put.a(100,100);
75 %
76 % (the center/anchor of a node is <name>.c/.h,
77 % <name> can't be c,h,x,y or any other global variable)
78 vardef put@# expr p =
79 pair @#.c;
80 pair @#.h;
81 pair @#.s;
82 path @#.f;
83 numeric @#.m;
84
85 @#.c := p;
86 enddef;
87
88
89 % declares an additional single node inside an existing matrix or tree
90 % (or change the position of an existing node)
91 %
92 % syntax: setpos.<name>[<row>][<col>](x,y);
93 %
94 % example: setpos.a[0][0](100,100);
95 %
96 % (the center/anchor of a node is <name>[<row>][<col>].c/.h)
97 vardef setpos@# expr p =
98 @#.c := p;
99 enddef;
100
101
102 % change the position of an existing node
103 %
104 % syntax: movepos.<name>(x,y);
105 %
106 % example: movepos.a(10,10);
107 % movepos.b[0][0](10,10);
108 vardef movepos@# expr p =
109 @#.c := @#.c shifted p;
110 @#.h := @#.h shifted p;
111 enddef;
112
113
114 % draws an existing node
115 %
116 % syntax: node.<node> <label>;
117 %
118 % example: node.a[0][0] btex $\alpha$ etex;
119 %
120 % parameters: size = 7 | ...
121 % nodemargin = 1.5 | ...
122 % shape = circle | utriangle | ltriangle | btriangle | rtriangle |
123 % boxed | fixedbox | roundbox | roundfixedbox | diamond | none
124 % boxmargin = 5 | ...
125 % fixedboxwidth = 14 | ...
126 % fixedboxheight = 14 | ...
127 % border = solid | bold | dash | double | none
128 % bordercolor = black | white | ...
129 % filling = none | solid | lefthalf | righthalf
130 % fillingcolor = black | white | ...
131 % nodelabel = yes | no
132 % nodelabelcolor = black | white | ...
133 % solidsize = 0.9 | ...
134 % boldsize = 1.5 | ...
135 % dashsize = 0.75 | ...
136 % doublesize = 0.75 | ...
137 vardef node@#(expr l) =
138 save pic,fs,fm;
139 picture pic; pic := thelabel(l, (0,0));
140 pair fs;
141 numeric fm;
142
143 if (shape=circle):
144 @#.s := (size,size);
145 @#.f := circlepath;
146 @#.h := @#.c;
147 fs := (size,size);
148 fm := 0;
149 elseif (shape=utriangle):
150 @#.s := (size+solidsize,size+solidsize);
151 @#.f := utrianglepath;
152 @#.h := @#.c shifted (0,0.5ypart(@#.s));
153 fs = (size,size);
154 fm := 4solidsize;
155 elseif (shape=ltriangle):
156 @#.s := (size+solidsize,size+solidsize);
157 @#.f := ltrianglepath;
158 @#.h := @#.c shifted (-0.5xpart(@#.s),0);
159 fs = (size,size);
160 fm := 4solidsize;
161 elseif (shape=btriangle):
162 @#.s := (size+solidsize,size+solidsize);
163 @#.f := btrianglepath;
164 @#.h := @#.c shifted (0,-0.5ypart(@#.s));
165 fs = (size,size);
166 fm := 4solidsize;
167 elseif (shape=rtriangle):
168 @#.s := (size+solidsize,size+solidsize);
169 @#.f := rtrianglepath;
170 @#.h := @#.c shifted (0.5xpart(@#.s),0);
171 fs := (size,size);
172 fm := 4solidsize;
173 elseif (shape=boxed):
174 @#.s := urcorner pic - llcorner pic + (boxmargin,boxmargin);
175 @#.f := squarepath;
176 @#.h := @#.c;
177 fs := @#.s;
178 fm := 0;
179 elseif (shape=fixedbox):
180 @#.s := (fixedboxwidth,fixedboxheight);
181 @#.f := squarepath;
182 @#.h := @#.c;
183 fs := @#.s;
184 fm := 0;
185 elseif (shape=roundbox):
186 @#.s := urcorner pic - llcorner pic;
187 @#.f := rrectangle(xpart(@#.s),ypart(@#.s));
188 @#.h := @#.c;
189 fs := @#.s;
190 fm := 0;
191 elseif (shape=roundfixedbox):
192 @#.s := (fixedboxwidth,fixedboxheight);
193 @#.f := rrectangle(xpart(@#.s),ypart(@#.s));
194 @#.h := @#.c;
195 fs := @#.s;
196 fm := 0;
197 elseif (shape=diamond):
198 @#.s := (size,size);
199 @#.f := diamondpath;
200 @#.h := @#.c;
201 fs := (size,size);
202 fm := 0;
203 elseif (shape=none):
204 @#.s := (0,0);
205 @#.f := (0,0)..(0,0);
206 @#.h := @#.c;
207 @#.m := 0;
208 fs := (0,0);
209 fm := 0;
210 else:
211 errmessage("Node shape not defined");
212 fi
213
214 if (shape<>none):
215
216 if (filling=solid):
217 if (border=double):
218 fill @#.f xscaled (xpart(fs)-2doublesize) yscaled (ypart(fs)-2doublesize)
219 shifted @#.c withcolor fillingcolor;
220 else:
221 fill @#.f xscaled xpart(fs) yscaled ypart(fs)
222 shifted @#.c withcolor fillingcolor;
223 fi
224 elseif (filling=lefthalf):
225 if (border=double):
226 fill buildcycle(@#.f,(0,-1)--(0,1))
227 xscaled (xpart(fs)-2doublesize) yscaled (ypart(fs)-2doublesize)
228 shifted @#.c withcolor fillingcolor;
229 else:
230 fill buildcycle(@#.f,(0,-1)--(0,1))
231 xscaled xpart(fs) yscaled ypart(fs)
232 shifted @#.c withcolor fillingcolor;
233 fi
234 elseif (filling=righthalf):
235 if (border=double):
236 fill buildcycle(@#.f,(0,-1)--(0,1)) rotated 180
237 xscaled (xpart(fs)-2doublesize) yscaled (ypart(fs)-2doublesize)
238 shifted @#.c withcolor fillingcolor;
239 else:
240 fill buildcycle(@#.f,(0,-1)--(0,1)) rotated 180
241 xscaled xpart(fs) yscaled ypart(fs)
242 shifted @#.c withcolor fillingcolor;
243 fi
244 fi
245
246 if (border=solid):
247 @#.m := nodemargin * solidsize + fm;
248 draw @#.f xscaled xpart(fs) yscaled ypart(fs) shifted @#.c
249 withpen pencircle scaled solidsize withcolor bordercolor;
250 elseif (border=bold):
251 @#.m := nodemargin * boldsize + fm;
252 draw @#.f xscaled xpart(fs) yscaled ypart(fs) shifted @#.c
253 withpen pencircle scaled boldsize withcolor bordercolor;
254 elseif (border=dash):
255 @#.m := nodemargin * solidsize + fm;
256 draw @#.f xscaled xpart(fs) yscaled ypart(fs) shifted @#.c
257 withpen pencircle scaled solidsize dashed evenly scaled dashsize withcolor bordercolor;
258 elseif (border=double):
259 @#.m := nodemargin * solidsize + fm;
260 draw @#.f xscaled (xpart(fs)-2doublesize) yscaled (ypart(fs)-2doublesize) shifted @#.c
261 withpen pencircle scaled solidsize withcolor bordercolor;
262 draw @#.f xscaled xpart(fs) yscaled ypart(fs) shifted @#.c
263 withpen pencircle scaled solidsize withcolor bordercolor;
264 elseif (border=none):
265 @#.m := nodemargin;
266 else:
267 errmessage("Node border not defined");
268 fi
269
270 fi
271
272 if (nodelabel=yes):
273 if ((urcorner pic - llcorner pic) <> (0,0)):
274 label(l, @#.c) withcolor nodelabelcolor;
275 fi
276 fi
277 enddef;
278
279
280 % draws an arrow between two nodes and labels it
281 %
282 % syntax: arrow.<labelpos>(<labeloff>,<label>)(<node>,<node>) <path>;
283 % where <labelpos> = lft | rt | top | bot | ulft | urt | llft | lrt
284 % <labeloff> is between 0.0 and 1.0
285 % <node> is a node
286 % <path> is a path
287 %
288 % example: arrow.top(0.5, btex $a$ etex)(a[0][0],a[0][1]) a[0][0].c{dir90}..a[0][1].c;
289 %
290 % parameters: style = solid | bold | dash | bolddash | invisible
291 % arrowcolor = black | white | ...
292 % arrowlabel = yes | no
293 % unfillonarrowlabel = yes | no
294 % unfillcolor = white | black | ...
295 % arrowlabelcolor = black | white | ...
296 % tipangle = 25 | ...;
297 % tipsharpness = 10 | ...;
298 % tipsize = 4 | ...;
299 % arrowmargin = 1.5 | ...
300 % solidsize = 0.9 | ...
301 % boldsize = 1.5 | ...
302 % dashsize = 0.75 | ...
303 vardef arrow@#(expr o,l)(suffix a,b) expr p =
304 save cutp;
305 path cutp;
306 if ((style=bold) or (style=bolddash)):
307 cutp := (p) cutbefore (a.f xscaled xpart(a.s) yscaled ypart(a.s) shifted a.c)
308 cutafter (b.f xscaled (xpart(b.s)+b.m+arrowmargin*boldsize)
309 yscaled (ypart(b.s)+b.m+arrowmargin*boldsize) shifted b.c);
310 else:
311 cutp := (p) cutbefore (a.f xscaled xpart(a.s) yscaled ypart(a.s) shifted a.c)
312 cutafter (b.f xscaled (xpart(b.s)+b.m+arrowmargin*solidsize)
313 yscaled (ypart(b.s)+b.m+arrowmargin*solidsize) shifted b.c);
314 fi
315
316 save endpoint,endpointdir,pathtotip,tippoint,tipa,tipb,tip;
317 pair endpoint; endpoint := point length(cutp) of cutp;
318 pair endpointdir; endpointdir := unitvector(direction length(cutp) of cutp);
319 path pathtotip; pathtotip := cutp cutafter (circlepath scaled 2tipsize shifted endpoint);
320 pair tippoint; tippoint := point length(pathtotip) of pathtotip;
321 path tipa; tipa := endpoint{-endpointdir rotated tipsharpness}..
322 (endpoint - ((endpoint-tippoint) rotated tipangle));
323 path tipb; tipb := endpoint{-endpointdir rotated -tipsharpness}..
324 (endpoint - ((endpoint-tippoint) rotated -tipangle));
325 path tip;
326 if ((style=bold) or (style=bolddash)):
327 tip := reverse(tipa)--tipb--cycle;
328 else:
329 tip := tipa--reverse(tipa)--tipb--reverse(tipb)--cycle;
330 fi
331
332 if (style=solid):
333 draw cutp withpen pencircle scaled solidsize withcolor arrowcolor;
334 draw tip withpen pencircle scaled solidsize withcolor arrowcolor;
335 elseif (style=bold):
336 draw cutp withpen pencircle scaled boldsize withcolor arrowcolor;
337 draw tip withpen pencircle scaled boldsize withcolor arrowcolor;
338 elseif (style=dash):
339 draw cutp withpen pencircle scaled solidsize dashed evenly scaled dashsize withcolor arrowcolor;
340 draw tip withpen pencircle scaled solidsize withcolor arrowcolor;
341 elseif (style=bolddash):
342 draw cutp withpen pencircle scaled boldsize dashed evenly scaled dashsize withcolor arrowcolor;
343 draw tip withpen pencircle scaled boldsize withcolor arrowcolor;
344 elseif (style=invisible):
345 else:
346 errmessage("Arrow style not defined");
347 fi
348
349 if (arrowlabel=yes):
350 save lab;
351 picture lab;
352 lab := thelabel.@#(l, point o*length(cutp) of cutp);
353 if (unfillonarrowlabel=yes):
354 fill (bbox lab) withcolor unfillcolor;
355 fi
356 draw lab withcolor arrowlabelcolor;
357 %label.@#(l, point o*length(cutp) of cutp) withcolor arrowlabelcolor;
358 fi
359 enddef;
360
361
362 % draws an arrow looping around a node and labels it
363 %
364 % syntax: loop.<labelpos>(<labeloff>,<label>)(<node>) <dir>;
365 % where <labelpos> = lft | rt | top | bot | ulft | urt | llft | lrt
366 % <labeloff> is between 0.0 and 1.0
367 % <node> is a node
368 % <dir> is an angle
369 %
370 % example: loop.top(0.5, btex $a$ etex)(a[0][0]) 90;
371 %
372 % parameters: loopsize = 15 | ...
373 % looporientation = counterclockwiseorientation | clockwiseorientation
374 % style = solid | bold | dash | bolddash | invisible
375 % arrowcolor = black | white | ...
376 % arrowlabel = yes | no
377 % tipangle = 25 | ...;
378 % tipsharpness = 10 | ...;
379 % tipsize = 4 | ...;
380 % arrowmargin = 1.5 | ...
381 % boldsize = 1.5 | ...
382 % dashsize = 0.75 | ...
383 vardef loop@#(expr o,l)(suffix a) expr d =
384 save p;
385 path p;
386 if (looporientation=clockwiseorientation):
387 p := (-0.5*loopsize,0)..(0,0.5*loopsize)..(0.5*loopsize,0)..(0,-0.5*loopsize)..cycle;
388 elseif (looporientation=counterclockwiseorientation):
389 p := (-0.5*loopsize,0)..(0,-0.5*loopsize)..(0.5*loopsize,0)..(0,0.5*loopsize)..cycle;
390 else:
391 errmessage("Loop orientation not defined");
392 fi
393 arrow.@#(o,l)(a,a) (p shifted (0.6*loopsize,0) rotated d shifted a.c)
394 enddef;
395
396
397 % draws an arrow reaching a node and labels it
398 %
399 % syntax: incoming.<labelpos>(<labeloff>,<label>)(<node>) <dir>;
400 % where <labelpos> = lft | rt | top | bot | ulft | urt | llft | lrt
401 % <labeloff> is between 0.0 and 1.0
402 % <node> is a node
403 % <dir> is an angle
404 %
405 % example: incoming.lft(0.5, btex $a$ etex)(a[0][0]) 90;
406 %
407 % parameters: incominglength = 20 | ...
408 % style = solid | bold | dash | bolddash
409 % arrowcolor = black | white | ...
410 % arrowlabel = yes | no
411 % tipangle = 25 | ...;
412 % tipsharpness = 10 | ...;
413 % tipsize = 4 | ...;
414 % arrowmargin = 1 | ...
415 % boldsize = 1.5 | ...
416 % dashsize = 0.75 | ...
417 vardef incoming@#(expr o,l)(suffix a) expr d =
418 save incoming_a;
419 put.incoming_a ((incominglength,0) rotated d shifted a.c);
420 node_marker.incoming_a("");
421
422 arrow.@#(o,l)(incoming_a,a) incoming_a.c--a.c;
423 enddef;
424
425
426 % draws an arrow departing from a node and labels it
427 %
428 % syntax: outgoing.<labelpos>(<labeloff>,<label>)(<node>) <dir>;
429 % where <labelpos> = lft | rt | top | bot | ulft | urt | llft | lrt
430 % <labeloff> is between 0.0 and 1.0
431 % <node> is a node
432 % <dir> is an angle
433 %
434 % example: outgoing.lft(0.5, btex $a$ etex)(a[0][0]) 90;
435 %
436 % parameters: outgoinglength = 20 | ...
437 % style = solid | bold | dash | bolddash
438 % arrowcolor = black | white | ...
439 % arrowlabel = yes | no
440 % tipangle = 25 | ...;
441 % tipsharpness = 10 | ...;
442 % tipsize = 4 | ...;
443 % arrowmargin = 1 | ...
444 % boldsize = 1.5 | ...
445 % dashsize = 0.75 | ...
446 vardef outgoing@#(expr o,l)(suffix a) expr d =
447 save outgoing_a;
448 put.outgoing_a ((outgoinglength,0) rotated d shifted a.c);
449 node_marker.outgoing_a("");
450
451 arrow.@#(o,l)(a,outgoing_a) a.c--outgoing_a.c;
452 enddef;
453
454
455 % this function can be used to enclose a sequence of points inside a bounding area
456 %
457 % syntax: enclose <path>;
458 % where <path> is a closed path, whose points are listed in clockwise order
459 %
460 % example: enclose a[0][0].c--a[0][1].c--a[1][1].c--cycle;
461 %
462 % parameters: style = solid | bold | dash | bolddash
463 % enclosurecolor = black | white | ...
464 % enclosuremargin = 5 | ...
465 % solidsize = 0.9 | ...
466 % boldsize = 1.5 | ...
467 % dashsize = 0.75 | ...
468 vardef enclose expr p =
469 if (style=solid):
470 draw enclosure(p) withpen pencircle scaled solidsize withcolor enclosurecolor;
471 elseif (style=bold):
472 draw enclosure(p) withpen pencircle scaled boldsize withcolor enclosurecolor;
473 elseif (style=dash):
474 draw enclosure(p) withpen pencircle scaled solidsize dashed evenly scaled dashsize withcolor enclosurecolor;
475 elseif (style=bolddash):
476 draw enclosure(p) withpen pencircle scaled boldsize dashed evenly scaled dashsize withcolor enclosurecolor;
477 else:
478 errmessage("Arrow style not defined");
479 fi
480 enddef;
481
482
483 % shorthand macros
484 def node_circle = with shape(circle) node enddef;
485 def node_utriangle = with shape(utriangle) node enddef;
486 def node_ltriangle = with shape(ltriangle) node enddef;
487 def node_dtriangle = with shape(dtriangle) node enddef;
488 def node_rtriangle = with shape(rtriangle) node enddef;
489 def node_diamond = with shape(diamond) node enddef;
490 def node_unfilled = with filling(none) node enddef;
491 def node_filled = with filling(solid) node enddef;
492 def node_filledleft = with filling(lefthalf) node enddef;
493 def node_filledright = with filling(righthalf) node enddef;
494 def node_solid = with border(solid) node enddef;
495 def node_bold = with border(bold) node enddef;
496 def node_dash = with border(dash) node enddef;
497 def node_double = with border(double) node enddef;
498 def node_hidden = with border(none) node enddef;
499 def node_boxed = with shape(boxed) node enddef;
500 def node_fixed = with shape(fixedbox) node enddef;
501 def node_roundbox = with shape(roundbox) node enddef;
502 def node_roundfixed = with shape(roundfixedbox) node enddef;
503 def node_marker = with shape(none) node enddef;
504 def arrow_solid = with style(solid) arrow enddef;
505 def arrow_bold = with style(bold) arrow enddef;
506 def arrow_dash = with style(dash) arrow enddef;
507 def arrow_bolddash = with style(bolddash) arrow enddef;
508 def arrow_invisible = with style(invisible) arrow enddef;
509 def loop_solid = with style(solid) loop enddef;
510 def loop_bold = with style(bold) loop enddef;
511 def loop_dash = with style(dash) loop enddef;
512 def loop_bolddash = with style(bolddash) loop enddef;
513 def loop_invisible = with style(invisible) loop enddef;
514 def loop_cw = with looporientation(clockwiseorientation) loop enddef;
515 def loop_ccw = with looporientation(counterclockwiseorientation) loop enddef;
516 def incoming_solid = with style(solid) incoming enddef;
517 def incoming_bold = with style(bold) incoming enddef;
518 def incoming_dash = with style(dash) incoming enddef;
519 def incoming_bolddash = with style(bolddash) incoming enddef;
520 def incoming_invisible = with style(invisible) incoming enddef;
521 def outgoing_solid = with style(solid) outgoing enddef;
522 def outgoing_bold = with style(bold) outgoing enddef;
523 def outgoing_dash = with style(dash) outgoing enddef;
524 def outgoing_bolddash = with style(bolddash) outgoing enddef;
525 def outgoing_invisible = with style(invisible) outgoing enddef;
526 def enclose_solid = with style(solid) enclose enddef;
527 def enclose_bold = with style(bold) enclose enddef;
528 def enclose_dash = with style(dash) enclose enddef;
529 def enclose_bolddash = with style(bolddash) enclose enddef;
530 def enclose_thin = with style(dash) dashsize(0.5) enclose enddef;
531 % e.g. with fillingcolor(black) node ...
532 vardef with@#(expr v) text e = push.@#; @# := v; e; pop.@# enddef;
533 % e.g. light.gray, light.light.gray, ...
534 vardef dark@# = 0.5[@#,black] enddef;
535 vardef light@# = 0.5[white,@#] enddef;
536 def gray = dark.white enddef;
537 % e.g. push fillingcolor; fillingcolor := gray; ... pop fillingcolor;
538 vardef push@# = @#.back[@#.count] := @#; @#.count := @#.count + 1 enddef;
539 vardef pop@# = @#.count := @#.count - 1; @# := @#.back[@#.count] enddef;
540
541
542 % default paths
543 def circlepath = ((0.5,0)..(0,0.5)..(-0.5,0)..(0,-0.5)..cycle) enddef;
544 def trianglepath = ((0.433,-0.25)--(0,0.5)--(-0.433,-0.25)--cycle) enddef;
545 def utrianglepath = trianglepath enddef;
546 def ltrianglepath = (trianglepath rotated 90) enddef;
547 def btrianglepath = (trianglepath rotated 180) enddef;
548 def rtrianglepath = (trianglepath rotated 270) enddef;
549 def squarepath = ((0.5,-0.5)--(-0.5,-0.5)--(-0.5,0.5)--(0.5,0.5)--cycle) enddef;
550 def diamondpath = ((0.5,0)--(0,0.5)--(-0.5,0)--(0,-0.5)--cycle) enddef;
551 def urdiamondpath = ((0,0.5)--(0.5,0)--(0,0)--cycle) enddef;
552 def uldiamondpath = ((-0.5,0)--(0,0.5)--(0,0)--cycle) enddef;
553 def lrdiamondpath = ((0.5,0)--(0,-0.5)--(0,0)--cycle) enddef;
554 def lldiamondpath = ((0,0)--(0,-0.5)--(-0.5,0)--cycle) enddef;
555 vardef rrectangle(expr w,h) = (((0.5w-0.25h,-0.5h)---(-0.5w+0.25h,-0.5h)..
556 (-0.5w+0.25h,0.5h)---(0.5w-0.25h,0.5h)..cycle)
557 xscaled (1/w) yscaled (1/h)) enddef;
558 vardef sqr(expr x) = ((x)*(x)) enddef;
559 vardef firstenclosurepoint(expr p,q) = ((ypart(p)-ypart(q)) * sqrt(sqr(enclosuremargin) / (sqr(xpart(p)-xpart(q)) + sqr(ypart(p)-ypart(q)))) + xpart(p),
560 (xpart(q)-xpart(p)) * sqrt(sqr(enclosuremargin) / (sqr(xpart(p)-xpart(q)) + sqr(ypart(p)-ypart(q)))) + ypart(p)) enddef;
561 vardef secondenclosurepoint(expr p,q) = ((ypart(p)-ypart(q)) * sqrt(sqr(enclosuremargin) / (sqr(xpart(p)-xpart(q)) + sqr(ypart(p)-ypart(q)))) + xpart(q),
562 (xpart(q)-xpart(p)) * sqrt(sqr(enclosuremargin) / (sqr(xpart(p)-xpart(q)) + sqr(ypart(p)-ypart(q)))) + ypart(q)) enddef; vardef enclosuresegment(expr p,q) = firstenclosurepoint(p,q)---secondenclosurepoint(p,q) enddef;
563 vardef enclosure(expr p) =
564 save enc;
565 save i;
566 path enc;
567 if (pair p):
568 enc := circlepath scaled (2*enclosuremargin) shifted point 1 of p;
569 else:
570 enc := enclosuresegment(point 0 of p, point 1 of p);
571 for i=1 upto length(p)-1:
572 enc := enc..enclosuresegment(point i of p, point i+1 of p);
573 endfor;
574 enc := enc..cycle;
575 fi
576 enc
577 enddef;
578
579
580 % general appearence constants and variables
581 def none = 1 enddef;
582 def yes = 2 enddef;
583 def no = 3 enddef;
584 def circle = 10 enddef;
585 def utriangle = 11 enddef;
586 def ltriangle = 12 enddef;
587 def btriangle = 13 enddef;
588 def rtriangle = 14 enddef;
589 def boxed = 15 enddef;
590 def fixedbox = 16 enddef;
591 def roundbox = 17 enddef;
592 def roundfixedbox = 18 enddef;
593 def diamond = 19 enddef;
594 def solid = 20 enddef;
595 def lefthalf = 21 enddef;
596 def righthalf = 22 enddef;
597 def bold = 30 enddef;
598 def dash = 31 enddef;
599 def bolddash = 32 enddef;
600 def double = 33 enddef;
601 def invisible = 34 enddef;
602 def clockwiseorientation = 40 enddef;
603 def counterclockwiseorientation = 41 enddef;
604
605 pair spacing;
606 pair offset;
607 pair numbering;
608 numeric size;
609 numeric shape;
610 numeric boxmargin;
611 numeric fixedboxwidth;
612 numeric fixedboxheight;
613 numeric border;
614 numeric filling;
615 numeric style;
616 numeric nodelabel;
617 numeric arrowlabel;
618 numeric unfillonarrowlabel;
619 numeric tipangle;
620 numeric tipsharpness;
621 numeric tipsize;
622 numeric loopsize;
623 numeric looporientation;
624 numeric incominglength;
625 numeric outgoinglength;
626 numeric solidsize;
627 numeric boldsize;
628 numeric dashsize;
629 numeric doublesize;
630 numeric nodemargin;
631 numeric arrowmargin;
632 numeric enclosuremargin;
633 color bordercolor;
634 color fillingcolor;
635 color nodelabelcolor;
636 color arrowcolor;
637 color arrowlabelcolor;
638 color unfillcolor;
639 color enclosurecolor;
640
641 spacing = (60,30);
642 offset = (0,0);
643 numbering = (0,0);
644 size := 5;
645 shape := circle;
646 boxmargin := 5;
647 fixedboxwidth := 14;
648 fixedboxheight := 14;
649 border := solid;
650 filling := none;
651 style := solid;
652 nodelabel := yes;
653 arrowlabel := yes;
654 unfillonarrowlabel := no;
655 unfillcolor := white;
656 tipangle := 20;
657 tipsharpness := 15;
658 tipsize := 4;
659 loopsize := 14;
660 looporientation := counterclockwiseorientation;
661 incominglength := 20;
662 outgoinglength := 20;
663 solidsize := 0.7;
664 boldsize := 1.5;
665 dashsize := 0.7;
666 doublesize := 1.3;
667 nodemargin := 1.5;
668 arrowmargin := 3.8;
669 enclosuremargin := 5;
670 bordercolor = black;
671 fillingcolor = black;
672 nodelabelcolor = black;
673 arrowcolor = black;
674 arrowlabelcolor = black;
675 enclosurecolor = gray;
676 linecap := butt;
677 linejoin := mitered;
678
679
680 % variable stacks
681 pair spacing.back[];
682 pair offset.back[];
683 pair numbering.back[];
684 numeric size.back[];
685 numeric shape.back[];
686 numeric boxmargin.back[];
687 numeric fixedboxwidth.back[];
688 numeric fixedboxheight.back[];
689 numeric border.back[];
690 numeric filling.back[];
691 numeric style.back[];
692 numeric nodelabel.back[];
693 numeric arrowlabel.back[];
694 numeric unfillonarrowlabel.back[];
695 numeric tipangle.back[];
696 numeric tipsharpness.back[];
697 numeric tipsize.back[];
698 numeric loopsize.back[];
699 numeric looporientation.back[];
700 numeric incominglength.back[];
701 numeric outgoinglength.back[];
702 numeric solidsize.back[];
703 numeric boldsize.back[];
704 numeric dashsize.back[];
705 numeric doublesize.back[];
706 numeric nodemargin.back[];
707 numeric arrowmargin.back[];
708 numeric enclosuremargin.back[];
709 color bordercolor.back[];
710 color fillingcolor.back[];
711 color nodelabelcolor.back[];
712 color arrowcolor.back[];
713 color arrowlabelcolor.back[];
714 color unfillcolor.back[];
715 color enclosurecolor.back[];
716
717 numeric spacing.count;
718 numeric offset.count;
719 numeric numbering.count;
720 numeric size.count;
721 numeric shape.count;
722 numeric boxmargin.count;
723 numeric fixedboxwidth.count;
724 numeric fixedboxheight.count;
725 numeric border.count;
726 numeric filling.count;
727 numeric style.count;
728 numeric nodelabel.count;
729 numeric arrowlabel.count;
730 numeric unfillonarrowlabel.count;
731 numeric unfillcolor.count;
732 numeric tipangle.count;
733 numeric tipsharpness.count;
734 numeric tipsize.count;
735 numeric loopsize.count;
736 numeric looporientation.count;
737 numeric incominglength.count;
738 numeric outgoinglength.count;
739 numeric solidsize.count;
740 numeric boldsize.count;
741 numeric dashsize.count;
742 numeric doublesize.count;
743 numeric nodemargin.count;
744 numeric arrowmargin.count;
745 numeric enclosuremargin.count;
746 numeric bordercolor.count;
747 numeric fillingcolor.count;
748 numeric nodelabelcolor.count;
749 numeric arrowcolor.count;
750 numeric arrowlabelcolor.count;
751 numeric enclosurecolor.count;
752
753 spacing.count := 0;
754 offset.count := 0;
755 numbering.count := 0;
756 size.count := 0;
757 shape.count := 0;
758 boxmargin.count := 0;
759 fixedboxwidth.count := 0;
760 fixedboxheight.count := 0;
761 border.count := 0;
762 filling.count := 0;
763 style.count := 0;
764 nodelabel.count := 0;
765 arrowlabel.count := 0;
766 unfillonarrowlabel.count := 0;
767 unfillcolor.count := 0;
768 tipangle.count := 0;
769 tipsharpness.count := 0;
770 tipsize.count := 0;
771 loopsize.count := 0;
772 looporientation.count := 0;
773 incominglength.count := 0;
774 outgoinglength.count := 0;
775 solidsize.count := 0;
776 boldsize.count := 0;
777 dashsize.count := 0;
778 doublesize.count := 0;
779 nodemargin.count := 0;
780 arrowmargin.count := 0;
781 enclosuremargin.count := 0;
782 bordercolor.count := 0;
783 fillingcolor.count := 0;
784 nodelabelcolor.count := 0;
785 arrowcolor.count := 0;
786 arrowlabelcolor.count := 0;
787 enclosurecolor.count := 0;