Today, the user N3buchadnezzar asked on TeX.SX, how he could draw images of triangular numbers. He showed a picture:

triangular numbers with balls

Martin Heller showed a nice and clean solution:

triangular numbers with TikZ
\documentclass{article}
\usepackage{tikz}
\newlength\radius
\pgfmathsetlength{\radius}{0.5cm}
%
\newcommand\drawballs[2][]{%
  \foreach \y [evaluate=\y as \yy using #2+1-\y]
    in {1,...,#2} {%
    \foreach \x in {1,...,\yy} {%
      \shade[shading=ball,ball color=white,#1] 
        ({(2*\x-2+\y)*\radius},{sqrt(3)*\y*\radius})
          circle (\radius); 
    };
  }%
}
%
\begin{document}
\begin{tikzpicture}
    \drawballs{1}
    \drawballs[xshift=2cm]{2}
    \drawballs[xshift=5cm]{3}
\end{tikzpicture}
\end{document}

There were several further answers, such as by Altermundus, using tkz-euclide:

triangular numbers with TikZ
\usepackage{tkz-euclide}
...
\begin{tikzpicture} 
  \foreach \i in {0,...,3}
    {\tkzDefPoint(2*\i,0){O\i} 
  \tkzDrawCircle[R,ball color=gray!20](O\i,1cm)} 
  \foreach \i [count=\j from 0] in {4,...,6}
    {\tkzDefShiftPoint[O\j](60:2){O\i} 
  \tkzDrawCircle[R,ball color=gray!20](O\i,1cm)} 
  \foreach \i [count=\j from 4] in {7,...,8}
    {\tkzDefShiftPoint[O\j](60:2){O\i} 
  \tkzDrawCircle[R,ball color=gray!20](O\i,1cm)}
  \foreach \i [count=\j from 7] in {8,...,8}
    {\tkzDefShiftPoint[O\j](60:2){O\i} 
  \tkzDrawCircle[R,ball color=gray!20](O\i,1cm)}    
\end{tikzpicture}

Comments

Adding comments is currently not enabled.