%!PS-Adobe-2.0
%%BoundingBox: 0 0 612 792
%%Pages: 1
%%EndComments
%%Page: 1 1
% mountains.ps
%
% This program generates a silhouette of mountains, generated by a randomized
% recursive fractal. Each time the file is viewed, it generates a different
% image (due to randomness). Just open it in a postscript viewer, or print it
% to a postscript printer.
%
% This is just an illustration of the fact that postscript is in fact a
% programming language and can be used to do things in a programmatic way. Note
% how small this program is.
%
% Copyright (C) 2002 Greg Hamerly (greg_hamerly at baylor dot edu)
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
% or see http://www.gnu.org/copyleft/gpl.html
% this is the primary function, which is recursive
% stack arguments:
% i, y1, y0, x1, x0 (top)
% where x/y's define the endpoints of the line
% and i defines the iteration level
/makepeak {
15 dict begin % create a local namespace
0 index /x0 exch def
1 index /x1 exch def
2 index /y0 exch def
3 index /y1 exch def
x0 x1 add 2 div /x2 exch def
y0 y1 add 2 div /y2 exch def
rand 4095 and 4095 div 0.5 sub 0.5 mul
x1 x0 sub mul
y2 add /y2 exch def
4 index % pull up the recursion counter
1 sub % decrement the recursion counter
dup 0 gt % duplicate and compare the counter to 0
{
y2 y0 x2 x0
makepeak
pop pop pop pop
y1 y2 x1 x2
makepeak
pop pop pop pop
}
{
x2 y2 lineto % draw the line
x1 y1 lineto
} ifelse % recurse if we haven't bottomed out
pop % remove the recursion counter we put on there
end % pop dictionary off stack
} def
%0.5 setlinewidth
(Helvetica) findfont 20 scalefont setfont
250 750 moveto
(Fractal mountains) show
newpath
realtime srand
100 500 moveto
9 500 500 600 100
makepeak
clear
600 400 lineto
100 400 lineto
closepath
fill
showpage