Skip to content

Instantly share code, notes, and snippets.

@luser-dr00g
Created September 23, 2014 11:51
Show Gist options
  • Save luser-dr00g/da74c38e08fd887f964b to your computer and use it in GitHub Desktop.
Save luser-dr00g/da74c38e08fd887f964b to your computer and use it in GitHub Desktop.
A few small drawing functions: polygons, spirals, and stars.
%polygon.ps
%rad n {proc} atpoly -
%call proc having rotated+scaled
%so (1,0) is each vertex of rad-radius n-polygon
/atpoly {
%3 args (atpoly)=
4 dict begin /p exch def /n exch def % rad
/m matrix currentmatrix def
%(rad2=)print rad2 =
%dup (atpoly: rad=)print =
dup scale
/s matrix currentmatrix def
0 360 n div 359.9 %0 dAng maxAng %{}for
({
{
//s setmatrix
rotate
//p exec
} for
//m setmatrix
}) token pop exch pop %instantiate code template
bind
end exec % run loop without dictionary
} bind def
/drawpoly { % rad n x y
gsave
translate
1 index 0 moveto
{
1 0 lineto
} atpoly
closepath stroke
grestore
} def
/drawpolyweb { % rad n
gsave
translate
dup % rad n n
{ dup } repeat % rad n^(n+1)
1 add
-1 roll % n^n rad
%pstack() =
1 index % n^n rad n
{
1 0 moveto
1 exch {
1 0 lineto
closepath
} atpoly
} atpoly
stroke
grestore
} def
%spir-arc.ps
% circle? reduce N radius center-x center y
/spir { % bool red n r x y
gsave
translate % bool red n r
0 0 % bool red n r 0 0
3 2 roll % bool red n 0 0 r
0 % bool red n 0 0 r a0
5 4 roll % bool red 0 0 r a0 n
360 mul % bool red 0 0 r a0 a1
arc % bool red
4 dict begin
/red exch def %
/circ exch def
flattenpath %
{}{}{}{} pathforall % x0 y0 x1 y1 ... xN yN
circ not { newpath } if
count -2 roll moveto % x1 y1 ... xN yN
count 2 idiv /N exch def % x1 y1 ... xN yN
1 red sub 1 N div exp /sv exch def
N {
sv dup scale
count -2 roll lineto
} repeat %
end
stroke
grestore
} def
%stars.ps
/starpath { % n r1 r2 x y
matrix currentmatrix 6 1 roll % [] n r1 r2 x y
translate % [] n r1 r2
10 dict begin {/r2 /r1 /n} {exch def}forall % []
/da 360 n div def
r2 0 moveto
n {
da 2 div rotate
r1 0 lineto
da 2 div rotate
r2 0 lineto
}
repeat
closepath
end % []
setmatrix %
} def
false .99 2 100 300 400 spir
false .99 4 200 300 400 spir
false .99 6 300 300 400 spir
10 30 70 300 400 starpath fill
20 40 50 400 500 starpath stroke
100 9 300 200 drawpoly
100 9 300 500 drawpolyweb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment