[comp.lang.postscript] Postscript Fractal

danjudd@thor.acc.stolaf.edu (Dan Judd) (01/23/90)

I had had a discussion on attractors with a friend, and wanted to see how
smilely faces would turn out, so I hacked this out.

Here is a postscript program that calculates a Serpinski (sp) triangle at a
defined level of recursion. The default uses triangles, but squares and
smilefaces are built in also. Warning setting iterate above 12 on 400 dpi
or less will have little effect if any and will take a long time to do.

Dan Judd
danjudd@thor.acc.stolaf.edu
------Cut here for PS code------------
%!
%%Creator: dagon:danjudd (Dan Judd)
%%Title: A quick and dirty fractal style display
%%CreationDate: 
%%DocumentFonts: Courier-Bold Courier
%by Dan Judd 10/19/89
%to use this either redefine the points xpos ypos in polygon and
%redefine the figure routine to draw what you like  OR
%write another  routine making sure to include the figure routine and the x/ypos  defs 
%and replace the word polygon in calculate with the name of  your new  routine this
%would be a good idea if you wanted to set up several shape routines

%number of iterations to be made any more that 10 will take ages
/iterate 9 def
/polygon {
% the points where the next iteration is called from ( 0 0 is assumed )
/xpos1 0 def
/ypos1 760 def
/xpos2 580 def
/ypos2 380 def

% the actual definition of  the figure. NOTE: it is important it be named figure
% I also recommed using bind def instead of def as things will go faster
/figure { 0 0 moveto 0 760 lineto 580 380 lineto 0 0 lineto stroke } bind def
%/figure {0 0 moveto 0 500 lineto 500 500 lineto 500 0 lineto 0 0 lineto stroke } bind def
%if you uncomment the lower figure a square will be used in stead of a triangle
} bind def

%an example of renaming polygon to use this one replace polygon with smiley 
/smiley {
% the points where the next iteration is called from ( 0 0 is assumed )
/xpos1 0 def
/ypos1 760 def
/xpos2 580 def
/ypos2 380 def
% the actual definition of  the figure. NOTE: it is important it be named figure
% I also recommed using bind def instead of def as things will go faster
/figure {300 380 280 0 360 arc stroke
300 380 220 120 240 arc stroke
400 480 20 0 360 arc stroke
400 280 20 0 360 arc stroke
  } bind def
} bind def

/square {
% the points where the next iteration is called from ( 0 0 is assumed )
/xpos1 0 def
/ypos1 570 def
/xpos2 570 def
/ypos2 0 def
% the actual definition of  the figure. NOTE: it is important it be named figure
% I also recommed using bind def instead of def as things will go faster
/figure {0 0 moveto 0 570 lineto 570 570 lineto 570 0 lineto 0 0 lineto stroke 
               0 300 moveto 470 570 lineto stroke} bind def
} bind def
%**routine  that calls fract  and polygon, change polygon here if you are using
%**a different set up procedure
/calculate  { polygon iterate fract } bind def

/fract { 
  figure
  dup 1 ne{ gsave .5 .5 scale 1 sub dup dup
        fract  
        gsave  xpos1 ypos1 translate fract grestore
        xpos2 ypos2 translate  fract
        grestore 
    }
   {pop} ifelse
} bind def 

gsave 
%translate so that everything won't get jammed on the edge of the paper
12 12 translate
calculate
grestore
showpage