[Tex/LaTex] How to draw grids of geometric shapes with Tikz

tikz-pgf

I want to draw an image like the following:

alt text

I did this using the following Tikz code:

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepgflibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
   [hexagon/.style={regular polygon, regular polygon sides=6,
    draw, minimum size=1cm, anchor=center}]

\foreach \x in {0,...,2}
  \node at (\x, 5) [hexagon]{};
\foreach \x in {0,...,3}
  \node at (\x-0.5, 4.12) [hexagon]{};
\foreach \x in {0,...,4}
  \node at (\x-1, 3.25) [hexagon]{};
\foreach \x in {0,...,3}
  \node at (\x-.5, 2.38) [hexagon]{};
\foreach \x in {0,...,2}
  \node at (\x, 1.52) [hexagon]{};

\end{tikzpicture}
\end{document}

Is there a better way to code this, using fewer manually-calculated approximations in Tikz? Also, as can be seen, the lines overlap in a weird inexact way. Can I avoid that too?

Best Answer

I realized that this question is slightly different form the drawing hexagons of yore...so, how about this solution:

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepgflibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
  [hexagon/.style={regular polygon, regular polygon sides=6,
    draw, minimum size=1cm, anchor=center}]
  \foreach[evaluate=\y] \y in {-2,...,2}
  {
  \pgfmathparse{5-abs{ \y }}
\foreach \x in {1,...,\pgfmathresult}
\node at (abs \y /2 +\x, sqrt 3 * \y/2) [hexagon]{};
}
\end{tikzpicture}
\end{document}

which yields: alt text