alpar@1: /* graph.mod - graph visualization */ alpar@1: alpar@1: /* Written in GNU MathProg by Andrew Makhorin */ alpar@1: alpar@1: /* This model creates a picture in EPS format to visualize a graph. */ alpar@1: alpar@1: param file, symbolic, default "graph.eps"; alpar@1: /* output file to write the picture */ alpar@1: alpar@1: param R, default 2; alpar@1: /* radius to draw vertices, in mm */ alpar@1: alpar@1: param n, integer, > 0; alpar@1: /* number of vertices */ alpar@1: alpar@1: set V, default 1..n; alpar@1: /* set of vertices */ alpar@1: alpar@1: set E, within V cross V; alpar@1: /* set of edges */ alpar@1: alpar@1: param x{i in V}, default 50 * cos((i - 1) / card(V) * 8 * atan(1)); alpar@1: param y{i in V}, default 50 * sin((i - 1) / card(V) * 8 * atan(1)); alpar@1: /* x[i] and y[i] are coordinates of node i, in mm */ alpar@1: alpar@1: param x0 := (min{i in V} x[i]) - R - 3.0; alpar@1: param y0 := (min{i in V} y[i]) - R - 3.0; alpar@1: param x1 := (max{i in V} x[i]) + R + 3.0; alpar@1: param y1 := (max{i in V} y[i]) + R + 3.0; alpar@1: alpar@1: printf "%%!PS-Adobe-3.0 EPSF-3.0\n" > file; alpar@1: printf "%%%%BoundingBox: 0 0 %d %d\n", alpar@1: (72 / 25.4) * (x1 - x0), (72 / 25.4) * (y1 - y0) >> file; alpar@1: printf "/Helvetica findfont 6 scalefont setfont\n" >> file; alpar@1: printf "/mm { 72 mul 25.4 div } def\n" >> file; alpar@1: alpar@1: for {(i,j) in E} alpar@1: { printf "newpath\n" >> file; alpar@1: printf "%g mm %g mm moveto\n", x[i] - x0, y[i] - y0 >> file; alpar@1: printf "%g mm %g mm lineto\n", x[j] - x0, y[j] - y0 >> file; alpar@1: printf "closepath\n" >> file; alpar@1: printf "stroke\n" >> file; alpar@1: } alpar@1: alpar@1: for {i in V} alpar@1: { printf "newpath\n" >> file; alpar@1: printf "%g mm %g mm %g mm 0 360 arc\n", alpar@1: x[i] - x0, y[i] - y0, R >> file; alpar@1: printf "closepath\n" >> file; alpar@1: printf "gsave 1 1 1 setrgbcolor fill grestore\n" >> file; alpar@1: printf "stroke\n" >> file; alpar@1: printf "%g mm %g mm moveto\n", alpar@1: x[i] - (if i <= 9 then 1.2 else 1.8) - x0, alpar@1: y[i] - 0.8 - y0 >> file; alpar@1: printf "( %d ) show\n", i >> file; alpar@1: } alpar@1: alpar@1: printf "showpage\n" >> file; alpar@1: printf "%%%%EOF\n" >> file; alpar@1: alpar@1: data; alpar@1: alpar@1: param alpar@1: : V : x y := alpar@1: 1 0 40 alpar@1: 2 38 12 alpar@1: 3 24 -32 alpar@1: 4 -24 -32 alpar@1: 5 -38 12 alpar@1: 6 -19 26 alpar@1: 7 19 26 alpar@1: 8 31 -10 alpar@1: 9 0 -32 alpar@1: 10 -31 -10 alpar@1: 11 -9 12 alpar@1: 12 9 12 alpar@1: 13 14 -5 alpar@1: 14 0 -15 alpar@1: 15 -14 -5 alpar@1: 16 0 0 ; alpar@1: alpar@1: set E := alpar@1: (1,*) 6 10 16 12 7 alpar@1: (2,*) 7 6 16 13 8 alpar@1: (3,*) 8 7 16 14 9 alpar@1: (4,*) 9 8 16 15 10 alpar@1: (5,*) 10 9 16 11 6 alpar@1: (6,*) 14 alpar@1: (7,*) 15 alpar@1: (8,*) 11 alpar@1: (9,*) 12 alpar@1: (10,*) 13 alpar@1: (11,*) 12 15 alpar@1: (12,*) 13 alpar@1: (13,*) 14 alpar@1: (14,*) 15 ; alpar@1: alpar@1: end;