绘制具有特定边界的六角板

绘制具有特定边界的六角板

我想绘制具有特定布局和边界的六边形,例如:

蜂窝网络

理想情况下,六边形棋盘的形状可以轻松更改,并且可以自动绘制边界。我确实寻找了答案,并尝试了不同的实现方式。目前,最接近的是这个答案来自问题绘制六角板。但我不知道:

  1. 如何绘制路径,
  2. 如何生成索引

就像上图所示

答案1

这处理了所需的图像...

\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}

在此处输入图片描述

...并且这允许使用自定义坐标系更通用地放置“宏单元”:

\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}

在此处输入图片描述

相关内容