Transform.mp

u:=25;                    % 25 = 25bp = 25 PostScript points = 30/72 in
wi:=10;                   % width  in units u   
he:=8;                    % height in units u
hoehe:=he*u;              % height
breite:=wi*u;             % width

R:=3.7;                   % maximum radius in units u
n:=17;                    % number of edges
phi:=360/(n*10);          
phi:=360/(n*5);           % rotation angle

def draw_point(expr P, col) =
  unfill fullcircle scaled 1.5mm shifted P withcolor white;
  draw   fullcircle scaled 1.5mm shifted P withcolor col;
enddef;

transform t, Rot, T;
path q, p[];
pair P[];

Rot:=identity rotated(360/n);


% ----- Calculations in mathematical coordinates ------------------------------------
P1=(.85, 0);  
P2=(1, 0);  
P3=P1 transformed Rot;  
p0:=P1{1, 2}..{3, -1}P2--P3;                      % p0: first path element
for i=1 upto n-1:                                 % get remaining path elements  
  p[i]:=p[i-1] transformed Rot;                   % p[1],..., p[n-1]
endfor                                            % by rotating p0     
%
% path (n path elements)
%
p17:=p0 for i=1 upto n-1: &p[i] endfor ..cycle;   % put path elements together 

q:=((0, 0)--(1,0)) rotated phi;

P4=p0 intersectionpoint q;
% ----- End of calculations in mathematical coordinates -----------------------------


%
% transform t maps mathematical coordinates on MetaPost coordinates
%
t:=identity scaled (R*u) shifted .5(breite, hoehe);


beginfig(1)    % ========================= figure 1 =================================         
  draw (0, 0)--(breite, 0)--(breite, hoehe)--(0, hoehe)--cycle;  
  T:=identity;
  for i=0 upto 49:
    p98:=p17 transformed T transformed t;
    if i mod 2 = 0:
      fill p98;
    else:     
      unfill p98;
    fi 
    draw p98;
    T:=T zscaled P4;
  endfor      
endfig;        % ====================== end of figure 1 =============================


beginfig(2)    % ========================= figure 2 =================================         
  %
  % Box (in mathematical coordinates)
  %
  path kasten;
  kasten=(.65, -.15)--(1.15, -.15)--(1.15, .5)--(.65, .5)--cycle;
  z98=point 3.5 of kasten transformed t;
  fill kasten transformed t withcolor .9white;
  draw kasten transformed t;
  
  draw (0, 0)--(breite, 0)--(breite, hoehe)--(0, hoehe)--cycle; 
  
  draw p17 transformed t;
  draw p17 zscaled P4 transformed t withcolor red;
  draw q rotated -phi transformed t;
  draw q transformed t withcolor red;
  
  pickup pencircle scaled 2;
  draw p0 transformed t;
  label.rt(btex $p_{99}$ etex, point 3 of p17 transformed t);
  
  pickup pencircle scaled .5;
  draw_point((0,0) transformed t, black);
  label.bot(btex (0,0) etex, (0,0) transformed t);
  
  draw_point((1,0) transformed t, black);
  label.bot(btex (1,0) etex, (1,0) transformed t);
  
  draw_point(P4 transformed t, red);
  label.urt(btex $P_4$ etex, P4 transformed t);
  
  %
  % renew definition of transform t in order ============
  % to draw the enlarged kasten at the left     ============
  %
  t:=identity scaled (2.3R*u) shifted (-.35breite-1.8u, .5hoehe-.4u); 
  
  kasten:=kasten transformed t;
  fill kasten withcolor .9white; 
  draw kasten;
  z99=point 1.5 of kasten;
  
  draw p0     transformed t;
  draw p1     transformed t cutafter  kasten;
  draw p[n-1] transformed t cutbefore kasten;

  draw p0     zscaled P4 transformed t withcolor red;
  draw p1     zscaled P4 transformed t cutafter  kasten withcolor red;
  draw p[n-1] zscaled P4 transformed t cutbefore kasten withcolor red;
    
  draw q rotated -phi transformed t cutbefore kasten;
  draw q transformed t cutbefore kasten withcolor red;
  
  pickup pencircle scaled 2;
  draw p0 transformed t;
  label.rt(btex $p_{99}$ etex, point 3 of p17 transformed t);
  
  pickup pencircle scaled .5;
  
  draw_point((1,0) transformed t, black);
  label.bot(btex (1,0) etex, (1,0) transformed t);
  
  draw_point(P4 transformed t, red);
  label.urt(btex $P_4$ etex, P4 transformed t);
  
  pickup pencircle scaled 1;
  drawarrow z98--z99;
  pickup pencircle scaled .5;
  draw_point(z98, black);
  

endfig;        % ====================== end of figure 2 =============================

end