Article View: comp.lang.postscript
Article #1830chaos game: Sierpinski Surprise
From: Barry G. Adams
Date: Thu, 20 Aug 1992 12:49
Date: Thu, 20 Aug 1992 12:49
77 lines
2571 bytes
2571 bytes
Here is an interesting postscript program which generates the
Sierpinski gasket using the chaos game.
------------------------ CUT HERE --------------------------------
%!
% chaos.ps: The chaos game
% Reference: Fractals For the Classroom, by Peitgen, Jurgens and Saupe
% Author: Barry G. Adams (barry@ramsey.cs.laurentian.ca)
% Procedure for plotting a pixel
1 setlinecap
0.2 setlinewidth
/plotpixel { moveto 0.2 0 rlineto stroke} def
% getting random integer array index 0, 1, or 2
/randindex {rand 3 mod} def
% triangle vertices
/xvertices [100 520 310] def
/yvertices [100 100 380] def
% procedure for computing average of two numbers
/avg { add 2 div } def
% Procedure for drawing triangle outline
/triangle {
newpath
xvertices 0 get yvertices 0 get moveto
xvertices 1 get yvertices 1 get lineto
xvertices 2 get yvertices 2 get lineto
closepath
stroke
} def
% Play the game:
% (1) Initialize a current point
% (2) randomly generate a triangle vertex number (0, 1, 2),
% (3) calculate the midpoint of the line joining this vertex
% to the current point, plot the midpoint and make it the
% new current point.
% (4) go to (2)
/playgame {
/numpoints exch def % get number of points
% initialize the current point
/cpx xvertices 0 get xvertices 1 get avg def
/cpy yvertices 0 get yvertices 2 get avg def
triangle
cpx cpy plotpixel
numpoints {
/r randindex def
/cpx xvertices r get cpx avg def
/cpy yvertices r get cpy avg def
cpx cpy plotpixel
} repeat
} def
/Times-Roman findfont 24 scalefont setfont
100 710 moveto (The Chaos Game: Sierpinski Surprise) show
/Times-Roman findfont 12 scalefont setfont
/str 30 string def
% Play the game with 1000 points
/numpoints 1000 def
gsave
0 300 translate
numpoints playgame
100 300 moveto numpoints str cvs show ( points) show
grestore
% Play the game with 40000 points
/numpoints 40000 def
gsave
0 0 translate
numpoints playgame
100 300 moveto numpoints str cvs show ( points) show
grestore
showpage
Technical Details
Message-ID:
<1992Aug20.124923.18308@ramsey.cs.laurentian.ca>Path:
arc1-ca.pugleaf.net!sparky!uunet!usc!cs.utexas.edu!torn!pulp.cs.laurentian.ca!barryShow Full Headers
Complete Article Headers
Newsgroups: comp.lang.postscript Path: arc1-ca.pugleaf.net!sparky!uunet!usc!cs.utexas.edu!torn!pulp.cs.laurentian.ca!barry From: barry@ramsey.cs.laurentian.ca (Barry G. Adams) Subject: chaos game: Sierpinski Surprise Message-ID: <1992Aug20.124923.18308@ramsey.cs.laurentian.ca> Keywords: postscript program, chaos, fractals Organization: Dept. of Computer Science, Laurentian University, Sudbury, ON Date: Thu, 20 Aug 1992 12:49:23 GMT Lines: 80