14

I would like to represent the densest sphere packing in dimension 3 as the following picture, for a beamer presentation.

Sphere packing

Is Tikz the way to go for it? It is not a real 3D drawing since I only want a fixed representation of the picture, right?

Thank you for your help.

Wokwok
  • 143

1 Answers1

17

By nesting 2 for-loops one could do something like this:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach \x in {4,...,0}{
    \foreach \y in {\x,...,4}{    
        \draw[fill=white] (0.6*\x,-\y+0.3*\x) circle (0.55) (-0.6*\x,-\y+0.3*\x) circle (0.55);
    }
}
\end{tikzpicture}
\end{document}

enter image description here

Dynamite
  • 292
  • @KJO Please keep your answer, I very much prefer the compact syntax with the loops – Dynamite Jun 16 '19 at 20:00
  • @Dynamite Excellent :-) also the answer of KJO. Many sincere compliments. – Sebastiano Jun 16 '19 at 20:14
  • 1
    Beautiful and elegant, very nicely done – cmhughes Jun 16 '19 at 20:49
  • 2
    Nice! (You could replace the inner loop by \foreach \y [evaluate=\y as \z using {int(mod(\x+\y,2)+mod(\y,2))}] in {\x,...,4}{ \draw[fill=white] \ifnum\z=0 [fill=blue!10] \fi (0.6*\x,-\y+0.3*\x) circle (0.55) (-0.6*\x,-\y+0.3*\x) circle (0.55); }...) –  Jun 16 '19 at 21:20
  • Perfect thank you very much ! – Wokwok Jun 17 '19 at 08:13
  • Alternative colors: \foreach \x in {4,...,0}{ \foreach \y in {\x,...,4}{ \pgfmathsetmacro{\val}{ 0.6 + 0.1*(4-\y) } \definecolor{MyColor}{gray}{\val} \draw[fill=MyColor] (0.6*\x, -\y+0.3*\x) circle (0.55) %node {\x, \y} (-0.6*\x,-\y+0.3*\x) circle (0.55); %node {\x, \y}; } } – Watson Feb 08 '23 at 10:33