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 :




\def \nbrcircles {28}
\def \outerradius {15mm}
\def \dotradius {1.5mm}

\pgfmathsetmacro {\goldenAngle} {180 * (1+sqrt(5))}
\foreach \b in {1,...,\nbrcircles}{
    \pgfmathsetmacro{\angle} {\goldenAngle * \b}
    \pgfmathsetmacro{\sradius}{\b / \nbrcircles * \outerradius / 10}
    \draw[] (\angle:\sradius) circle [radius=\dotradius] ;

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 :-) :

phyllotaxy that needs more seeds

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 :) – Dan H. Jul 18 '14 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 '14 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:

\pgfmathsetmacro {\goldenRatio} {(1+sqrt(5))}
\foreach \b in {1,...,\nbrcircles}{
    \pgfmathsetmacro{\angle}{mod(\goldenRatio * \b,2)*180}
    \pgfmathsetmacro{\sradius}{\b / \nbrcircles * \outerradius / 10}
    \draw[] (\angle:\sradius) circle [radius=\dotradius] ;

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:

1000 dots

  • Thanks a lot, that was exactly the point I didn't understand – david59279 Jul 18 '14 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 – david59279 Jul 27 '14 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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