1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/graph.mod Mon Dec 06 13:09:21 2010 +0100
1.3 @@ -0,0 +1,98 @@
1.4 +/* graph.mod - graph visualization */
1.5 +
1.6 +/* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
1.7 +
1.8 +/* This model creates a picture in EPS format to visualize a graph. */
1.9 +
1.10 +param file, symbolic, default "graph.eps";
1.11 +/* output file to write the picture */
1.12 +
1.13 +param R, default 2;
1.14 +/* radius to draw vertices, in mm */
1.15 +
1.16 +param n, integer, > 0;
1.17 +/* number of vertices */
1.18 +
1.19 +set V, default 1..n;
1.20 +/* set of vertices */
1.21 +
1.22 +set E, within V cross V;
1.23 +/* set of edges */
1.24 +
1.25 +param x{i in V}, default 50 * cos((i - 1) / card(V) * 8 * atan(1));
1.26 +param y{i in V}, default 50 * sin((i - 1) / card(V) * 8 * atan(1));
1.27 +/* x[i] and y[i] are coordinates of node i, in mm */
1.28 +
1.29 +param x0 := (min{i in V} x[i]) - R - 3.0;
1.30 +param y0 := (min{i in V} y[i]) - R - 3.0;
1.31 +param x1 := (max{i in V} x[i]) + R + 3.0;
1.32 +param y1 := (max{i in V} y[i]) + R + 3.0;
1.33 +
1.34 +printf "%%!PS-Adobe-3.0 EPSF-3.0\n" > file;
1.35 +printf "%%%%BoundingBox: 0 0 %d %d\n",
1.36 + (72 / 25.4) * (x1 - x0), (72 / 25.4) * (y1 - y0) >> file;
1.37 +printf "/Helvetica findfont 6 scalefont setfont\n" >> file;
1.38 +printf "/mm { 72 mul 25.4 div } def\n" >> file;
1.39 +
1.40 +for {(i,j) in E}
1.41 +{ printf "newpath\n" >> file;
1.42 + printf "%g mm %g mm moveto\n", x[i] - x0, y[i] - y0 >> file;
1.43 + printf "%g mm %g mm lineto\n", x[j] - x0, y[j] - y0 >> file;
1.44 + printf "closepath\n" >> file;
1.45 + printf "stroke\n" >> file;
1.46 +}
1.47 +
1.48 +for {i in V}
1.49 +{ printf "newpath\n" >> file;
1.50 + printf "%g mm %g mm %g mm 0 360 arc\n",
1.51 + x[i] - x0, y[i] - y0, R >> file;
1.52 + printf "closepath\n" >> file;
1.53 + printf "gsave 1 1 1 setrgbcolor fill grestore\n" >> file;
1.54 + printf "stroke\n" >> file;
1.55 + printf "%g mm %g mm moveto\n",
1.56 + x[i] - (if i <= 9 then 1.2 else 1.8) - x0,
1.57 + y[i] - 0.8 - y0 >> file;
1.58 + printf "( %d ) show\n", i >> file;
1.59 +}
1.60 +
1.61 +printf "showpage\n" >> file;
1.62 +printf "%%%%EOF\n" >> file;
1.63 +
1.64 +data;
1.65 +
1.66 +param
1.67 +: V : x y :=
1.68 + 1 0 40
1.69 + 2 38 12
1.70 + 3 24 -32
1.71 + 4 -24 -32
1.72 + 5 -38 12
1.73 + 6 -19 26
1.74 + 7 19 26
1.75 + 8 31 -10
1.76 + 9 0 -32
1.77 + 10 -31 -10
1.78 + 11 -9 12
1.79 + 12 9 12
1.80 + 13 14 -5
1.81 + 14 0 -15
1.82 + 15 -14 -5
1.83 + 16 0 0 ;
1.84 +
1.85 +set E :=
1.86 + (1,*) 6 10 16 12 7
1.87 + (2,*) 7 6 16 13 8
1.88 + (3,*) 8 7 16 14 9
1.89 + (4,*) 9 8 16 15 10
1.90 + (5,*) 10 9 16 11 6
1.91 + (6,*) 14
1.92 + (7,*) 15
1.93 + (8,*) 11
1.94 + (9,*) 12
1.95 + (10,*) 13
1.96 + (11,*) 12 15
1.97 + (12,*) 13
1.98 + (13,*) 14
1.99 + (14,*) 15 ;
1.100 +
1.101 +end;