5

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}
\def \outerradius {15mm}
\def \dotradius {1.5mm}

\begin{tikzpicture}
\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] ;
}  
\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 :-) :

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

2
  • add a second value to \foreach \b in {1,...,\nbrcircles, e.g. with {1,1.1,...,\nbrcircles i get a nice spiral :)
    – Dan H.
    Commented 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
    Commented Jul 18, 2014 at 7:49

2 Answers 2

8

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}
    \pgfmathsetmacro{\sradius}{\b / \nbrcircles * \outerradius / 10}
    \draw[] (\angle:\sradius) circle [radius=\dotradius] ;
}  
\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:

1000 dots

2
  • Thanks a lot, that was exactly the point I didn't understand
    – david59279
    Commented Jul 18, 2014 at 10:14
  • 1
    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
    Commented Jul 27, 2014 at 10:24
3
\documentclass[tikz,border=2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin {tikzpicture}
\foreach \x in {0,...,1000}{
    \fill ({mod((1+sqrt(5))*\x,2)*180}:{0.1+sqrt(\x/pi)/2}) circle [radius=0.15];
}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .