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