# Generate Mandelbrot images using TikZ?

In my thesis that I'm writing on, I'm using a Mandelbrot generator to test an application I've written. To explain a certain problem discussed in the report an actual generated image need to be included.

Since every figure in the report so far has been generated using TikZ, I'm wondering whether I could also generate the Mandelbrot image using TikZ. But would it possible?

-
There's a TikZ library for fractal decorations (30.7 Fractal Decorations) and a Lindenmayer systems library (37 Lindenmayer System Drawing Library) –  Tom Bombadil Aug 25 '12 at 15:44

There's a Mandelbrot set shading in the shadings library:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\end{document}


Displayed with the Acrobat Reader, as the TeXworks PDF previewer did not show high resolution.

The Mandelbrot set is computed and generated by the PDF renderer. The algorithm is written using PostScript commands, PDF supports a subset of Postscript operators, that's why it should be much faster than computing using higher level pgf or TikZ operators.

-
Ha! Well, that was unexpected. ^^ Unfortunately, the image doesn't exactly look like a Mandelbrot set, but cool nonetheless. –  gablin Dec 28 '11 at 17:05
@gablin This could be caused by a low number of iterations, I would expect that a higher number of iterations would be nearer to the exact Mandelbrot set, which would require an infinite number of iterations. –  Stefan Kottwitz Dec 28 '11 at 17:10
@StefanKottwitz Is there any way to increase the iteration count? That looks like 10 or so iterations . . . you get a much better picture even with just 20. –  mrc Nov 15 at 18:53
@mrc Yes, it can be seen in the code of that TikZ library, the number of iteration is fixed but can be extended by changing or redefining the code. –  Stefan Kottwitz Nov 15 at 22:15

For those who are interested in another approach, i.e., using PSTricks:

\documentclass[border=12pt]{standalone}
\usepackage{pst-fractal}

\begin{document}
\end{document}


Compile the code snippet above with xelatex or latex-dvips-ps2pdf sequence.

-
Very cool! How does the pstricks compare to MATLAB or Python in the terms of speed when it comes to things like plotting strange attractors, KAM islands or fractals? How difficult is to generate movie? In MATLAB is for example trivial to generate movie which shows creation of the Lorenz attractor. It is fairly easy to call such movie from the Powerdot slides (written in PSTricks) but I wonder if I could create animation natively which will not require to launch a video player. –  Predrag Punosevac Dec 29 '11 at 2:05
I believe that pstricks does not actually generate the image, it inserts postscript code that does the actual calculation. Which means the speed would depend on your postscript interpreter. When printing such document on a postscript printer, the calculations would be done on the printer. When viewing the resulting dvi or postscript file, the calculations would be done by your dvi or ps viewer at the time of viewing. Perhaps you could first convert from postscript to another format, the calculation would happen then, and view the result later. –  Jan Hlavacek Dec 29 '11 at 2:52
Of course. I meant PostScript and Ghostscript interpreter vs MATLAB or Python. See my post here for getting row PostScript into TeX with command special tex.stackexchange.com/questions/23987/… –  Predrag Punosevac Dec 29 '11 at 4:39

Here is a rather crude solution using luatex. Note that the calculations are done without any optimization, no use of symmetry or any other properties of the Mandelbrot set. Warning: the code takes a while to process even on a fairly fast computer.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{luacode}

\def\escapetime#1#2{\directlua{tex.print(escape_time(#1,#2))}}
\def\mandcolor#1#2{\directlua{mand_color(#1,#2)}}

\begin{document}
\begin{luacode}
local NUMITER=100

point_iter = function (a,b)
local n = 1
local x = a
local y = b
return function ()
if n > NUMITER then return nil end
n = n+1
x,y = x*x - y*y + a, 2*x*y + b
return n, x*x + y*y
end
end

escape_time = function (a,b)
local esc_time
for n, d in point_iter(a,b) do
esc_time = n
if d > 4 then break end
end
return esc_time
end

mand_color = function (a,b)
local etime = escape_time(a,b)
if etime == NUMITER+1 then tex.print("black") else tex.print("red!"..etime) end
end
\end{luacode}
\begin{tikzpicture}[scale=2]
\foreach \x in {-2,-1.99,...,.5}{
\foreach \y in {-1.3,-1.29,...,1.3}{
\draw[\mandcolor{\x}{\y},fill] (\x,\y) +(-.005,-.005) rectangle +(.005,.005);}}
\end{tikzpicture}
\end{document}


This is the resulting image:

The next step would be to use mplib directly from lua to render the picture.

-