# Drawing recursive randomized structures with TikZ

After I learned earlier today how to draw nice randomized trees (Draw randomized tree in TikZ) , I discovered that I'm not quite satisfied with how the results look like - because the structure I want to draw is not really a tree, but actually a shower of high energy particles. Here's the best I can do:

\documentclass{article}

\usepackage{tikz}
\usepackage{xkeyval}
\usepackage{etoolbox}
\usepackage{ifthen}

\makeatletter
\define@key{hepshower}{nTracks}{\edef\nTracks{#1}}
\define@key{hepshower}{phimin}{\edef\phiMin{#1}}
\define@key{hepshower}{phisep}{\edef\phiSep{#1}}
\define@key{hepshower}{phimax}{\edef\phiMax{#1}}
\define@key{hepshower}{br}{\edef\branchingProbability{#1}}
\define@key{hepshower}{maxdepth}{\edef\maxDepth{#1}}
\presetkeys{hepshower}{nTracks=5,phimin=-90,phimax=90,rmin=0.5,rmax=1.0,phisep=10,br=0.9,maxdepth=3}{}

\newcommand\drawshower[2][]{
\typeout{#2}
\setkeys{hepshower}{#1}{
\foreach \i in {1,...,\nTracks} {
\pgfmathparse{\phiMin+(\phiMax-\phiMin)*(\i-1)/\nTracks + rnd * ((\phiMax-\phiMin)/\nTracks-\phiSep) + \phiSep/2}
\edef\phiVal{\pgfmathresult}
\typeout{#2 \i : \phiVal - \radVal}
\edef\coordname{#2-showerchild\i}
\draw[black] (#2) --  (\coordname);
\pgfmathparse{rnd}
\ifnumcomp{\maxDepth}{>}{0}{
\ifthenelse{\lengthtest{\pgfmathresult pt<\branchingProbability pt}}{
\drawshower[#1,maxdepth=\maxDepth-1]{\coordname}
}{}
}{}
}
}
}

\makeatother

\begin{document}
\begin{tikzpicture}
\coordinate (test);
\drawshower[phimin=0,phimax=180]{test};
\end{tikzpicture}
\end{document}


If you run the output, you see from the \typeouts that all the nodes are created and everything seems to work - apart from the fact that only the first iteration seems to be drawn. I think this has something to do with the polar coordinates, but I can't quite figure out what's wrong with it.

## Edit

Follwing a comment from Qrrbibel, here is an image of a shower that looks like what I was hoping for:

As you can see, it has a tree-like structure, but the nodes from different branches are not leveled and the lines are decreasing in length and varying in angle as the tree expands, giving the whole thing a somewhat more realistic look as compared to purely schematic trees.

• Can you add an example or a link to “a shower of high energy particles”. How does that look like? Why are you using xkeyval? TikZ already uses the pgfkeys package and that integrates nicely. By the way, in your code, you use \pgfmathparse twice successively, the result from the first one is lost and never accessed. Commented Oct 13, 2013 at 15:21