%  TeX Mandelbrot Program
%  
%  My entry for the contest ``The most useless TeX program ever written''.
%  Can anyone top that? (Don't mention MusicTeX in this context. That's not
%  meant to be a joke.)
%  Also welcome: Improvements of this saving on TeX's resources. This takes
%  more than five minutes on my NeXT 486/66.    
%     
%  Seb, 6.7.94
%
%  (seb@balu.afp.ruhr-uni-bochum.de)
%
%
%  parameters that you can modify:
%  
%    \x, \y, \xmax, \ymax in units of 10000 
\newcount\x
\x=-20000        %  means -2.000 for real part of start value
\newcount\y        
\y=-20000        %  imaginary part
\newcount\xmax   
\xmax=20000      %  end value, real
\newcount\ymax
\ymax=20000      %  end value, imaginary
%
%    calculation depth
\newcount\mandeldepth
\mandeldepth=100
%  
%
% recursation depth = number of pixels. 
\newcount\xsize   % (Don't go wild, tail recursion 
\xsize=100        %  uses loads of TeX memory!)
\newcount\ysize 
\ysize=100         
%
%  picture size
\newdimen\height
\newdimen\width
\height=10cm
\width=10cm
%
%   that was all the parameters!
%
%
%  pixel size:
\newdimen\xgrid
\xgrid=\height
\divide\xgrid by\xsize
\newdimen\ygrid
\ygrid=\width
\divide\ygrid by\ysize
%
\nopagenumbers
%
\newcount\stepX       % work out step sizes 
\stepX=\xmax
\advance\stepX by -\x
\divide\stepX by\xsize
\newcount\stepY
\stepY=\ymax
\advance\stepY by -\y
\divide\stepY by\ysize
%
\def\cout#1{\immediate\write16{#1}}  % type out debug message 
%
\def\yes{Y}    % some stupid logical macros
\def\no{N}
\def\notyet{x}
%
\newcount\u                % variables for calculation
\newcount\v
\newcount\newV
\newcount\newU
\newcount\d
\newcount\h
\def\runmandel{%  % recursive Mandelbrot macro
    \newU=\u%
    \multiply\newU by \u%     % newU := u^2
    \h=\v%
    \multiply\h by \v%        % h := v^2
    \advance\newU by -\h%     % newU := u^2 - v^2
    \h=\x%
    \multiply\h by 10000%     % h := x * 10000 (normalize)
    \advance\newU by \h%      % newU := newU + h
    \divide\newU by 10000%    % renormalize u
    \newV=\u%   
    \multiply\newV by \v%
    \multiply\newV by 2%      % newV := 2 *u * v    
    \h=\y%
    \multiply\h by 10000%     % h := y * 10000
    \advance\newV by \h%      % newV := newV + h
    \divide\newV by 10000%    % renormalize v
    \u=\newU%                 %  newU = u^2 - v^2 + x
    \v=\newV%                 %  newV = 2 * u * v + y 
    \advance\d by 1%
    % \cout{d: \the\d,   u: \the\u, v: \the\v}%
    \ifnum\d>\mandeldepth%      % member ofthe Mandelbrot set 
	\def\result{\yes}%      
    \fi%                   
    \ifnum\u<-20000%           
	\def\result{\no}%       % real part exceeded -2
    \fi%
    \ifnum\u>20000%
	\def\result{\no}%       % real part exceeded 2
    \fi%
    \ifnum\v<-20000%
	\def\result{\no}%       % imaginary part exceeded -2
    \fi%
    \ifnum\v>20000%      
	\def\result{\no}%       % real part exceeded 2
    \fi%  
    \if\notyet\result\runmandel\fi% % play it again...
}%
\def\result{\yes}%
\def\makeline{\advance\x by \stepX%    
    %\cout{testing \the\x = i * \the\y}%
    \u=0%
    \v=0%
    \d=0%
    \def\result{\notyet}%
    \runmandel%    result expands to either yes or no now
    \if\result\yes%  
	\vrule height \xgrid depth 0pt width \ygrid%
    \else%
	\vrule height \xgrid depth 0pt width 0pt\hskip\xgrid%
    \fi%
    \ifnum\x<\xmax%
	\makeline%
    \else\fi}%
\def\makepicture{\advance\y by \stepY% 
    \hbox{\makeline}%
    %\cout{y in makepicture: \the\y}%
    \ifnum\y<\ymax%
	\makepicture%
    \else\fi}%
\vbox{\offinterlineskip\makepicture}  % now, let's go tail
\bye				      % recursive! 

