Saturday, 17 August 2013

Drawing recursive randomized structures with TikZ

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}{rmin}{\edef\radMin{#1}}
\define@key{hepshower}{rmax}{\edef\radMax{#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{(\radMax-\radMin)*rnd+\radMin}
\edef\radVal{\pgfmathresult}
\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}
\coordinate (\coordname) at (\phiVal:\radVal);
\draw[black] (#2) -- (\coordname);
\pgfmathparse{(\radMax-\radMin)*rnd+\radMin}
\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.

No comments:

Post a Comment