examples/graph.mod
changeset 2 4c8956a7bdf4
equal deleted inserted replaced
-1:000000000000 0:b3c44ffa18b8
       
     1 /* graph.mod - graph visualization */
       
     2 
       
     3 /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
       
     4 
       
     5 /* This model creates a picture in EPS format to visualize a graph. */
       
     6 
       
     7 param file, symbolic, default "graph.eps";
       
     8 /* output file to write the picture */
       
     9 
       
    10 param R, default 2;
       
    11 /* radius to draw vertices, in mm */
       
    12 
       
    13 param n, integer, > 0;
       
    14 /* number of vertices */
       
    15 
       
    16 set V, default 1..n;
       
    17 /* set of vertices */
       
    18 
       
    19 set E, within V cross V;
       
    20 /* set of edges */
       
    21 
       
    22 param x{i in V}, default 50 * cos((i - 1) / card(V) * 8 * atan(1));
       
    23 param y{i in V}, default 50 * sin((i - 1) / card(V) * 8 * atan(1));
       
    24 /* x[i] and y[i] are coordinates of node i, in mm */
       
    25 
       
    26 param x0 := (min{i in V} x[i]) - R - 3.0;
       
    27 param y0 := (min{i in V} y[i]) - R - 3.0;
       
    28 param x1 := (max{i in V} x[i]) + R + 3.0;
       
    29 param y1 := (max{i in V} y[i]) + R + 3.0;
       
    30 
       
    31 printf "%%!PS-Adobe-3.0 EPSF-3.0\n" > file;
       
    32 printf "%%%%BoundingBox: 0 0 %d %d\n",
       
    33       (72 / 25.4) * (x1 - x0), (72 / 25.4) * (y1 - y0) >> file;
       
    34 printf "/Helvetica findfont 6 scalefont setfont\n" >> file;
       
    35 printf "/mm { 72 mul 25.4 div } def\n" >> file;
       
    36 
       
    37 for {(i,j) in E}
       
    38 {     printf "newpath\n" >> file;
       
    39       printf "%g mm %g mm moveto\n", x[i] - x0, y[i] - y0 >> file;
       
    40       printf "%g mm %g mm lineto\n", x[j] - x0, y[j] - y0 >> file;
       
    41       printf "closepath\n" >> file;
       
    42       printf "stroke\n" >> file;
       
    43 }
       
    44 
       
    45 for {i in V}
       
    46 {     printf "newpath\n" >> file;
       
    47       printf "%g mm %g mm %g mm 0 360 arc\n",
       
    48          x[i] - x0, y[i] - y0, R >> file;
       
    49       printf "closepath\n" >> file;
       
    50       printf "gsave 1 1 1 setrgbcolor fill grestore\n" >> file;
       
    51       printf "stroke\n" >> file;
       
    52       printf "%g mm %g mm moveto\n",
       
    53          x[i] - (if i <= 9 then 1.2 else 1.8) - x0,
       
    54          y[i] - 0.8 - y0 >> file;
       
    55       printf "( %d ) show\n", i >> file;
       
    56 }
       
    57 
       
    58 printf "showpage\n" >> file;
       
    59 printf "%%%%EOF\n" >> file;
       
    60 
       
    61 data;
       
    62 
       
    63 param
       
    64 :  V  :  x     y :=
       
    65    1     0    40
       
    66    2    38    12
       
    67    3    24   -32
       
    68    4   -24   -32
       
    69    5   -38    12
       
    70    6   -19    26
       
    71    7    19    26
       
    72    8    31   -10
       
    73    9     0   -32
       
    74   10   -31   -10
       
    75   11    -9    12
       
    76   12     9    12
       
    77   13    14    -5
       
    78   14     0   -15
       
    79   15   -14    -5
       
    80   16     0     0 ;
       
    81 
       
    82 set E :=
       
    83    (1,*)  6 10 16 12  7
       
    84    (2,*)  7  6 16 13  8
       
    85    (3,*)  8  7 16 14  9
       
    86    (4,*)  9  8 16 15 10
       
    87    (5,*) 10  9 16 11  6
       
    88    (6,*) 14
       
    89    (7,*) 15
       
    90    (8,*) 11
       
    91    (9,*) 12
       
    92   (10,*) 13
       
    93   (11,*) 12 15
       
    94   (12,*) 13
       
    95   (13,*) 14
       
    96   (14,*) 15 ;
       
    97 
       
    98 end;