Alright, so i've been teaching myself PostScript. And this is really the first program i've put together using it. Run it through GhostScript, or just send it to a PostScript printer, and you should get a Sierpinski Triangle. This follows my first recipe in the write-up on that node.

I may eventually comment this as a bit of an example of some commands. For now, there is documentation for some of the commands in the Postscript command reference.

%!PS
1 setlinecap
0 setlinewidth
/verticesX [ 0 1 1 ] def
/verticesY [ 1 1 0 ] def
/numpoints 20000 def

72 0.5 mul 72 3.5 2 div mul translate 
72 7.5 mul dup scale

/nextVertex rand 3 mod def
verticesX nextVertex get verticesY nextVertex get moveto
currentpoint

1 1 numpoints
{
  /nextVertex rand 3 mod def
  verticesY nextVertex get
  add 2 div
  exch
  verticesX nextVertex get
  add 2 div
  exch
  moveto
  0.001 0 rlineto
  currentpoint
  stroke
} repeat
showpage
%quit

Alright, there has been some modification, and adaptation.
%!PS
%
% User-modifiable variables:
%
% numpoints -- the number of points drawn in the figure
/numpoints 4800 def
% radius -- the radius of the polygon
/radius 0.5 def
% numVertices -- the number of vertices in the polygon
/numVertices 5 def
% END user-modifiable stuff

/inch { 72 mul } def
1 setlinecap
0 setlinewidth

% this puts the centre (0,0) at the centre of a letter page
%
4.25 inch 5.5 inch translate
%
% this scales up the image somewhat
%
7.5 inch dup scale

% choose a vertex at random
/nextVertex rand numVertices mod def
% move to it
nextVertex 360 numVertices div mul cos radius mul
nextVertex 360 numVertices div mul sin radius mul
moveto
currentpoint

1 1 numpoints
{
  % choose the next target coordinate
  /nextVertex rand numVertices mod def
  % calculate its Y coordinate
  nextVertex 360 numVertices div mul cos radius mul
  % take the midpoint between the current Y and the target Y
  add 2 div
  exch
  % calculate its X coordinate
  nextVertex 360 numVertices div mul sin radius mul
  % take the midpoint between the current X and the target X
  add 2 div
  exch
  % move to it
  moveto
  % make a dot
  0.001 0 rlineto
  % store the current coordinate on the stack
  currentpoint  
  stroke
} repeat
showpage
quit

Log in or register to write something here or to contact authors.