7

I would like to draw hexagons with specific layout and boundary like:

cellular network

Ideally, the shape of the board of hexagons could be change easily, and the boundaries could be drawn automatically. I did search for answers, and tried out different implementations. Currently, the closest one is this answer from question Drawing Hex Boards. But I don't know:

  1. how to draw the path,
  2. how to generate the indices

like what it shown in the figure above

  • Can you show us what you've tried so far (if anything)? – Paul Gessler Aug 8 '14 at 21:01
12

This takes care of the required image...

\documentclass[tikz,border=5]{standalone}
\renewcommand\familydefault\sfdefault
\usetikzlibrary{shapes.geometric}
\tikzset{hexagon/.style={%
  shape=regular polygon, regular polygon sides=6, 
  minimum size=2cm, font=\bfseries,
}}
\tikzset{Macrocell/.pic={
  code={
    \node [hexagon, fill=blue!50!cyan!20] (c) {(#1,1)};
\path \foreach \a [evaluate={\s=int(\a-1); \t=int(mod(\s+2,6)+1);}] in {2,...,7}{
  node [hexagon, ultra thin, draw=gray!50,anchor=side \s] 
   (c-\a) at (c.side \t) {(#1,\a)}
};
\draw [ultra thick, black] (c-2.corner 3)
  \foreach \i in {2,...,7} {
    \foreach \j [evaluate={\k=int(mod(1+\i+\j,6)+1);}] in {0,1,2}
       { --(c-\i.corner \k) }} -- cycle;
  }}}
\pgfmathparse{sqrt(21)}\let\magicdistance=\pgfmathresult
\pgfmathparse{acos(\magicdistance/7)}\let\magicangle=\pgfmathresult
\begin{document}
\begin{tikzpicture}
\path  pic {Macrocell=1};
\node [above=1ex] {Macrocell \#1};
\foreach \i in {2,...,7}
\path (100+\i*60+\magicangle:1.625*\magicdistance) node {Macrocell \#\i} 
   (120+\i*60+\magicangle:\magicdistance) pic {Macrocell=\i};
\end{tikzpicture}
\end{document}

enter image description here

...and this allows more general placement of the "macrocells" with a custom coordinate system:

\documentclass[tikz,border=5]{standalone}
\renewcommand\familydefault\sfdefault
\usetikzlibrary{shapes.geometric}
\tikzset{%
  hexagon/.style={%
    shape=regular polygon, regular polygon sides=6, 
    minimum size=2cm, font=\bfseries,
  },
  inner hexagon/.style={
    hexagon,
    fill=blue!50!cyan!20,
  },
  outer hexagon/.style={
    hexagon,
    ultra thin, draw=gray!50
  },
  macrocell border/.style={
    ultra thick, black
  }}
\tikzset{Macrocell/.pic={
  code={
    \edef\m{#1}%
    \node [inner hexagon, fill=blue!50!cyan!20] (macrocell-\m-1) {(\m,1)};
    \path \foreach \a [evaluate={\s=int(\a-1); \t=int(mod(\s+2,6)+1);}] in {2,...,7}{ 
    node [outer hexagon, anchor=side \s] 
      (macrocell-\m-\a) at (macrocell-\m-1.side \t) {(\m,\a)} };
\draw [macrocell border] (macrocell-#1-2.corner 3)
  \foreach \i in {2,...,7} {
    \foreach \j [evaluate={\k=int(mod(1+\i+\j,6)+1);}] in {0,1,2}
       { --(macrocell-\m-\i.corner \k) }} -- cycle;
  }}}

\pgfmathparse{sqrt(21)}\let\magicdistance=\pgfmathresult
\pgfmathparse{acos(\magicdistance/7)}\let\magicangle=\pgfmathresult
\tikzset{macrocell cs/.cd,
  x/.store in=\mx,
  y/.store in=\my,
  x=0, y=0
}
\tikzdeclarecoordinatesystem{macrocell}{%
\tikzset{macrocell cs/.cd,#1}%
\tikzset{x=(\magicangle-60:\magicdistance cm),%
  y=(60+\magicangle:\magicdistance cm)}%
\pgfpointxy{\mx}{\my}%
}
\begin{document}
\begin{tikzpicture}
\foreach \i in {1,...,16}
\path (macrocell cs:x={mod(\i-1,4)},y={int((\i-1)/4)}) pic {Macrocell=\i};

\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.