# TikZ: trying to draw a phyllotaxy (sunflower pattern) and get short by computation limits

As the title says, I'm trying to draw a sunflower pattern with TikZ. I base my experiment on information found on this tutorial written by Jim Bumgardner, mainly from examples 8 and 10.

His demonstration uses javascript, and I already adapted it to python for generating svg, but this time I need to produce a printable document in cmyk, so I'm trying to get my hands on TikZ.

I was doing OK with the code shown below, but my problem is that I can't draw the pattern with more circles. As soon as I try to compile it with a greater value for 'nbrcircles', I get the error 'Dimension too large'. After reading as most as possible, I understand that this error is linked to the computation limits of the TikZ system.

I'm very new in the TikZ world (even in the Tex one), and I'm looking for a way to draw bigger patterns, as shown on the page linked above..

Any help would be greatly appreciated.

Here is the working code that I have so far. Simply replace value for nbrcircles with a greater number to produce the error :

\documentclass[10pt]{minimal}

\usepackage{tikz}

\begin{document}
\pagestyle{empty}

\def \nbrcircles {28}

\begin{tikzpicture}
\pgfmathsetmacro {\goldenAngle} {180 * (1+sqrt(5))}
\foreach \b in {1,...,\nbrcircles}{
\pgfmathsetmacro{\angle} {\goldenAngle * \b}
}
\end{tikzpicture}
\end{document}


Here is the result I get with that code. Compare it with example #8 on Jim's page, you'll see that mine misses a few dots :-) :

Ideally, I'd like to be able to produce a pattern with 300 to 600 seeds, and be able to increase the size of the circle as the spiral develops (just like Jim's example 10).

• add a second value to \foreach \b in {1,...,\nbrcircles, e.g. with {1,1.1,...,\nbrcircles i get a nice spiral :) Jul 18, 2014 at 6:36
• To save others from fiddling, I found that one can increase the \nbrcircles to 50 when increasing the angle in \goldenAngle to 101: writelatex.com/1281146wbfkcz
– Habi
Jul 18, 2014 at 7:49

The problem in your code comes from the computation of the \angle, at the line

\pgfmathsetmacro{\angle} {\goldenAngle * \b}


When \b is greater than 28, the result this product becomes larger than 16384, which is the maximum allowed value. You should refactor your code to avoid computing such values.

One way to do this in this code is to compute the same angle modulo 360. Try the following:

\begin{tikzpicture}
\pgfmathsetmacro {\goldenRatio} {(1+sqrt(5))}
\foreach \b in {1,...,\nbrcircles}{
\pgfmathsetmacro{\angle}{mod(\goldenRatio * \b,2)*180}
}
\end{tikzpicture}


You can see that we now multiply \b by only 1+sqrt(5), then use the modulo operation before the product with 180. This will give the same result as the previous code, but allow to much bigger values of \b. See for instance this rendering with \nbrcircles set to 1000:

• Thanks a lot, that was exactly the point I didn't understand Jul 18, 2014 at 10:14
• The code and rendering of the final result, with increasing radius for the seeds, can be viewed here: texample.net/tikz/examples/phyllotaxy - thanks to stephan, owner and maintainer of texamples.net Jul 27, 2014 at 10:24
\documentclass[tikz,border=2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin {tikzpicture}
\foreach \x in {0,...,1000}{