DynamicTeXLabels.mp

u:=25;                    % 25 = 25bp = 25 PostScript points = 25/72 in
wi:=10;                   % width  in units u   
he:=10;                   % height in units u
hoehe:=he*u;              % height
breite:=wi*u;             % width
string s;
path p;

transform t;
t:=identity scaled u shifted .5(breite, hoehe);

n:=32;
R:=4;

vardef produce_label(expr i, n) =
  if     i=0:    s:="dotlabel.rt";
  elseif i=n/4:  s:="dotlabel.top";
  elseif i=n/2:  s:="dotlabel.lft";
  elseif i=3n/4: s:="dotlabel.bot";
  elseif i‹n/4:  s:="dotlabel.urt";
  elseif i‹n/2:  s:="dotlabel.ulft";
  elseif i‹3n/4: s:="dotlabel.llft";
  else:          s:="dotlabel.lrt";
  fi  
  s&"(btex $P_{"&decimal i&"}$ etex, z["&decimal i&"]);"
enddef;

def draw_variable(expr P, Q, intb, inte) =
  for i=0 upto 9:
    draw (.1i)[P, Q]--(.1(i+1))[P, Q] withcolor (.1i)[intb, inte];
  endfor
enddef;

beginfig(1)
  % --- Define the n vertices z[0],...,z[n-1] of the regular polygon ---
  for i=0 upto n-1:
    w:=360/n*i;
    z[i]=R*(cosd w, sind w) transformed t;  
  endfor

  % --- Define the polygon as a path, p ---
  p:=for i=0 upto n-1: z[i]-- endfor cycle; 
    
  draw (0, 0)--(breite, 0)--(breite, hoehe)--(0, hoehe)--cycle;
  
  % --- Fill and draw polygon and the diameters ---
  fill p withcolor .9white;
  draw p;
  for i=0 upto n-1:
    draw_variable((0, 0) transformed t, z[i], .9white, black); 
  endfor
   
  % --- Draw labels using `write to' and `input' ---  
  %  
  %  The string containing all the label commands is dead as long
  %  as it remains within the MetaPost file. It cannot be brought 
  %  to life directly by MetaPost.
  %  
  %  If the string is written as a single line to a file 
  %  (DynamicTeXLabels.mp) and subsequently input with the `input' 
  %  command of MetaPost, it is not only read but automatically 
  %  becomes alive: it is also automatically executed.
  %
  for i=0 upto n-1:
    write produce_label(i, n) to "DynamicTeXLabels.tmp";
  endfor  
  write EOF to "DynamicTeXLabels.tmp";       % Close file        
  input DynamicTeXLabels.tmp;                % input file (which effects
                                             % automatic execution)    
endfig;

end