# Using LaTeX/TikZ for fractal flower

I am trying to generate this particular fractal image of a flower, but don't even know where or how to start. Can TikZ be used to generate a "Phylotactic spiral"?

and the image looks as given below:

It seems like loads of dots, so I began trying with a scatterplot on pgfplots, but failed completely. No code worth showing here.

EDIT: How do I put a green-tick to more than one answer? The results you folks have given are astounding!

• I can't see where there's any fractal in that design. It's at most a scaled repetition of the same eight leaves flower. Shouldn't involve any fractal definition. Oct 11, 2022 at 18:55
• Fractal := scaled repetition Oct 11, 2022 at 19:00
• To me it looks like you want what once was called dithering. Here are a few tikz-solutions: tex.stackexchange.com/questions/266881/… . // Se also: bartwronski.com/2016/10/30/… + elopezr.com/dithering-algorithm + tannerhelland.com/2012/12/28/… and many other. Oct 11, 2022 at 19:31
• Hi! An approach for your image might be the following: 1) Draw the basic quadrilateral (plain color first). 2) Transform it into a pic element. 3) Learn how to use the command ($(p)! c! angle: (p)$) which produces the image of the point q through a direct similarity of center p, ratio c, and angle angle. 4) Do some mathematics for one of the spirals and create it with the pic and the above similarity. 5) Use a \foreach loop to have your eight leaves. 6) Modify the filling of the elementary quadrilateral (in the initial pic) to have the desired effect. Oct 11, 2022 at 19:33
• @hpekristiansen Many definitions of a fractal are a bit more strict than that. Otherwise, a line segment would be a fractal, as you can make it by putting together infinitely many line segments end to end, each half the size of the one before. Oct 12, 2022 at 4:30

I'm using Sandy G's formula here, basically the .707^<level> part.

Two approaches:

1. PGF and LuaLaTeX that uses JLDiaz' great poisson lua script.
2. TikZ and PGFmath that just uses the rnd function.

The macro \pgfpointspiralifdefined makes sure that coordinates that have been calculated already don't need to have be recalculated.

Instead of the coordinate a, b, c and d, you can also use the vertex anchors of the kite shape that Sandy G's answer uses (you need to name the nodes of course, e.g. spiral-\l-\n).

In both solutions I'm cubing one of the random values so that the points get bunched to one side of the kites.

## PGF + LuaLaTeX

\documentclass{standalone}
\usepackage{pgf,pgffor}
\usepackage{jldiaz-poisson}% https://tex.stackexchange.com/a/185423/16595
\usepackage{xcolor} % colorwheel
\definecolor{cw0}{HTML}{9AFF00}\definecolor{cw1}{HTML}{FFA500}
\definecolor{cw2}{HTML}{FF001A}\definecolor{cw3}{HTML}{FF00D9}
\definecolor{cw4}{HTML}{6500FF}\definecolor{cw5}{HTML}{005AFF}
\definecolor{cw6}{HTML}{00FFE5}\definecolor{cw7}{HTML}{00FF25}
\pgfset{
declare function={
spiralAngle(\level,\spiral) = \directlua{tex.print(
180/(\pgfkeysvalueof{/pgf/spiral\space N})*\level
+360/(\pgfkeysvalueof{/pgf/spiral\space N})*\spiral)};
\newcommand*\pgfpointspiral[2]{% #1 = level, #2 = spiral
\makeatletter
\newcommand*\pgfpointspiralifdefined[3]{%
% if spiral-#2-#3 doesn't exist, define it
% if it does do nothing
\pgfutil@ifundefined{pgf@sh@ns@spiral-#2-#3}{%
\pgfcoordinate{spiral-#2-#3}{\pgfpointspiral{#2}{#3}}%
}{}% and make it an alias for #1
\pgfnodealias{#1}{spiral-#2-#3}}
\makeatother
\begin{document}
\begin{pgfpicture}
\pgfsetxvec{\pgfqpoint{5mm}{0mm}}
\pgfsetyvec{\pgfqpoint{0mm}{5mm}}
\foreach \l in {0,...,6}{
\foreach \n in {0,...,7}{
\pgfpointspiralifdefined{a}{\l}            {\n}
\pgfpointspiralifdefined{b}{\inteval{\l+1}}{\n}
\pgfpointspiralifdefined{c}{\l}            {\inteval{\n+1}}
\pgfpointspiralifdefined{d}{\inteval{\l-1}}{\inteval{\n+1}}
\pgfsetfillcolor{cw\n}
\x/\y in {\poissonpointslist{1}{1}{.02+.0\l}{10}} {
\pgfpathcircle{
{\pgfpointanchor{a}{center}}{\pgfpointanchor{b}{center}}}
{\pgfpointanchor{d}{center}}{\pgfpointanchor{c}{center}}}
}{+.25pt}
\pgfusepath{fill}
}
}
}
\end{pgfpicture}
\end{document}


## TikZ + PGFmath

The \pgfpointspiralifdefined could have also been implemented by a custom TikZ coordinate system but why bother …

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\pgfset{
declare function={
spiral N/.initial=8}
\newcommand*\pgfpointspiral[2]{% #1 = level, #2 = spiral
\pgfpointpolarxy{180/(\pgfkeysvalueof{/pgf/spiral N})*(#1)
+360/(\pgfkeysvalueof{/pgf/spiral N})*(#2)}
\makeatletter
\newcommand*\pgfpointspiralifdefined[3]{%
\pgfutil@ifundefined{pgf@sh@ns@spiral-#2-#3}{%
\pgfcoordinate{spiral-#2-#3}{\pgfpointspiral{#2}{#3}}%
}{}%
\pgfnodealias{#1}{spiral-#2-#3}}
\makeatother
\begin{document}
\begin{tikzpicture}[x=+5mm, y=+5mm]
\foreach \l[evaluate={\Dots=250*.7^\l}] in {0,...,6} {
\foreach \n in {0,...,7} {
\pgfpointspiralifdefined{a}{\l}            {\n}
\pgfpointspiralifdefined{b}{\inteval{\l+1}}{\n}
\pgfpointspiralifdefined{c}{\l}            {\inteval{\n+1}}
\pgfpointspiralifdefined{d}{\inteval{\l-1}}{\inteval{\n+1}}
\dot in {0,...,\Dots} {
($($(a)!\ySpread!(d)$)!\xSpread!($(b)!\ySpread!(c)$)$)
}
}
\end{tikzpicture}
\end{document}


## Output

Here is the rose, which uses the kite node shape from the tikz library shapes.geometric. I leave it to others to fill the nodes with dots.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\tikzset{mykite/.style={inner sep=.1pt, kite, fill=red!30, kite vertex angles=120 and 75}}

\begin{document}

\begin{tikzpicture}
\foreach \l in {1,...,12}{
\foreach \n[evaluate=\n as \t using \l*22.5+\n*45, % \t is the angle for node placement
evaluate=\n as \s using .703^(\l-1), % \s is the scaling factor for the distance from 0 for each level
evaluate=\n as \c using \s-.01] in {1,...,8} % \c is used to shrink each node just a bit.
{\node[mykite, rotate=\t, minimum size=\c cm] at (\t-90:{\s*1.38}){};}
}
\end{tikzpicture}

\end{document}


To get an 8-vertex rose, the kite vertex angles must differ by 45 (120 and 75 in the code above). You can change .703 to (sin((\aaa-45)/2)/sin(\aaa/2)), where \aaa is the larger angle to get variations in the shape. The 1.38 factor needs adjusting as well.

Test

I tried to make the construction using a pic and a helix like curve. All the computations are hidden in the definition of the petalPiece (the pic element) which creates a quadrangle; each petal will consist of \nbQuad pieces.

There are two global variables: the number of petals and the number of pieces for each petal. For example, with 9 petals, we get the drawing below.

The code

\documentclass[11pt, margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math, calc}
\begin{document}

\tikzmath{
\nbPetals = 8;
real \a, \base, \r;
\a = 360/\nbPetals;  % petal's angle
\base = 2;  % base of the exponential definig the helix
\r = .35;  % scaling constant
function tmpR(\i) {% integer giving a point along the helix
};
}
\tikzset{%
pics/petalPiece/.style 2 args={% branch number, base point number
code={%
\tikzmath{integer \b, \p; \b = #1; \p = #2;}
\path
({(\p +1)/\nbQuad*180 +\b*\a}: {tmpR(\p +1)}) coordinate (NE)
({(\p +2)/\nbQuad*180 +(\b -1)*\a}: {tmpR(\p +2)}) coordinate (SE)
({(\p +1)/\nbQuad*180 +(\b -1)*\a}: {tmpR(\p +1)}) coordinate (SW);
\foreach \i in {0, .05, ..., .9}{%
\draw[white, thick, fill=magenta!90, opacity=.05]
($(NW)!\i!(SW)$) -- ($(NE)!\i/2!(SE)$) -- (SE) -- (SW) -- cycle;
}
}
}
}
\begin{tikzpicture}
\foreach \l in {1, 2, ..., \nbPetals}{
\foreach \i in {-1, 0, 1, 2, ..., \nbQuad}{%
\path (0, 0) pic {petalPiece={\l}{\i}};
}
}
\end{tikzpicture}