Drawing hexagons

Here's a quick option:

\begin{tikzpicture}
  \foreach \i in {0,...,3} 
  \foreach \j in {0,...,3} {
  \foreach \a in {0,120,-120} \draw (3*\i,2*sin{60}*\j) -- +(\a:1);
  \foreach \a in {0,120,-120} \draw (3*\i+3*cos{60},2*sin{60}*\j+sin{60}) -- +(\a:1);}
\end{tikzpicture}

Which results in

alt text


With TikZ, you can define a pattern which allows to fill any shape with a hexagonal grid by adding the option pattern=hexagons:

alt text

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{patterns}

\def\hexagonsize{0.5cm}
\pgfdeclarepatternformonly
  {hexagons}% name
  {\pgfpointorigin}% lower left
  {\pgfpoint{3*\hexagonsize}{0.866025*2*\hexagonsize}}%  upper right
  {\pgfpoint{3*\hexagonsize}{0.866025*2*\hexagonsize}}%  tile size
  {% shape description
   \pgfsetlinewidth{0.4pt}
   \pgftransformshift{\pgfpoint{0mm}{0.866025*\hexagonsize}}
   \pgfpathmoveto{\pgfpoint{0mm}{0mm}}
   \pgfpathlineto{\pgfpoint{0.5*\hexagonsize}{0mm}}
   \pgfpathlineto{\pgfpoint{\hexagonsize}{-0.866025*\hexagonsize}}
   \pgfpathlineto{\pgfpoint{2*\hexagonsize}{-0.866025*\hexagonsize}}
   \pgfpathlineto{\pgfpoint{2.5*\hexagonsize}{0mm}}
   \pgfpathlineto{\pgfpoint{3*\hexagonsize+0.2mm}{0mm}}
   \pgfpathmoveto{\pgfpoint{0.5*\hexagonsize}{0mm}}
   \pgfpathlineto{\pgfpoint{\hexagonsize}{0.866025*\hexagonsize}}
   \pgfpathlineto{\pgfpoint{2*\hexagonsize}{0.866025*\hexagonsize}}
   \pgfpathlineto{\pgfpoint{2.5*\hexagonsize}{0mm}}
   \pgfusepath{stroke}
  }
\begin{document}

\begin{tikzpicture}
\fill[pattern=hexagons] (0,0) rectangle (10,5);
\end{tikzpicture}

\begin{tikzpicture}
\fill[pattern=hexagons] (0,0) circle (3cm);
\end{tikzpicture}

\end{document}

You can change the size of the hexagons by modifying the value of the macro \hexagonsize.


Another way could be to draw hexagonal nodes over an adjusted coordinate system. The idea came adapting Paul Gaborit's Pascal triangle for How can I draw Pascal's triangle with some its properties?.

shapes.geometric library helps to draw hexagon where the minimum size is the diameter of the circumcircle. Therefore, selecting adjusted values for x (x=1.5*{minimum size}) and y (y=\sqrt{.75}*{minimum size}/2) the hexagonal grid can be drawn placing an node centered in every pair (x,y).

\documentclass[border=2mm, tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}

%
% x=3*(minimum size)/2
% x=\sqrt{3/4}*(minimum size)/2
%
\begin{tikzpicture}[x=7.5mm,y=4.34mm]
  % some styles
  \tikzset{
    box/.style={
      regular polygon,
      regular polygon sides=6,
      minimum size=10mm,
      inner sep=0mm,
      outer sep=0mm,
      rotate=0,
    draw
    }
  }

\foreach \i in {0,...,5} 
    \foreach \j in {0,...,5} {
            \node[box] at (2*\i,2*\j) {};
            \node[box] at (2*\i+1,2*\j+1) {};
        }

\end{tikzpicture}

\end{document}

enter image description here

Tags:

Diagrams

Grids