This example attempts to mimic the behavior of the built-in path construction operators like arc
.
If there is a current point, poly
first draws a line to (x,y)+(r,0), otherwise it starts by moving to that point.
Instead of gsave
... grestore
(which has the undesirable effect of discarding the very changes to the current path which we want), it saves a copy of the current transformation matrix (CTM) as it exists when the function starts.
Then it does lineto
to each succeeding point, which by scaling and rotating the matrix is always at (0,1). Finally, it calls closepath
and then restores the saved matrix as the CTM.
% x y n radius poly -
% construct a path of a closed n-polygon
/poly {
matrix currentmatrix 5 1 roll % matrix x y n radius
4 2 roll translate % matrix n radius
dup scale % matrix n
360 1 index div exch % matrix 360/n n
0 1 {lineto currentpoint moveto}stopped{moveto}if % start or re-start subpath
{ % matrix 360/n
dup rotate % matrix 360/n
0 1 lineto % matrix 360/n
} repeat % matrix 360/n
pop % matrix
closepath % matrix
setmatrix %
} def