Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
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

3 Answers

up vote 20 down vote accepted

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

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadings}
\begin{document}
\tikz\shade[shading=Mandelbrot set] (0,0) rectangle (4,4);
\end{document}

Mandelbrot set

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.

share|improve this answer
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
2  
@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

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

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

\begin{document}
\psfractal[type=Mandel,baseColor=red,maxRadius=30,dIter=30,cx=-1.3,xWidth=4cm,yWidth=4cm](-3,-2)(2,2)
\end{document}

enter image description here

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

share|improve this answer
1  
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
2  
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:

mandelbrot set generated with lualatex

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.