我创建了一个六角螺旋,但在尝试编号时遇到了问题。螺旋由 2 个部分组成。我可以手动对整个内容进行编号,如下所示,但我找不到更简单的方法。
我读关于对螺旋进行编号的方法,遍历环,但它似乎与 TeX 生成形状的方式不太兼容。以下是该方法:
function cube_spiral(center, radius):
var results = [center]
for each 1 ≤ k ≤ radius:
results = results + cube_ring(center, k)
return results
我的六角网格是这个网格。
它看起来应该是这样的:
生成六角螺旋的代码(带部分手动编号):
\documentclass{standalone}
\usepackage{tikz}
\usepackage{ifthen} % if else statements
\usetikzlibrary{shapes} %allows hexagons
\begin{document}
\begin{tikzpicture} [hexa/.style= {shape=regular polygon, regular polygon sides=6, minimum size=1cm, draw, inner sep=0, anchor=south, fill=darkgray!85!white, rotate=0},numbr/.style= {white},arro/.style= {draw,red,thick}]
\pgfmathsetmacro\radius{4}
\pgfmathsetmacro\diameter{\radius*2}
\pgfmathsetmacro\shortdiameter{\radius*2-1}
\pgfmathsetmacro\largeradius{\radius+1}
\pgfmathsetmacro\shortradius{\radius-1}
% base hexagon (created in 2 sections and rotated 90 degrees)
\begin{scope}[rotate=90]
\foreach \j in {0,...,\radius}{%
\pgfmathsetmacro\end{\radius+\j}
\foreach \i in {0,...,\end}{%
\node[hexa] (h\i;\j) at ({(\i-\j/2)*sin(60)},{\j*0.75}) {}; %{\i;\j};
}
}
\foreach \j in {0,...,\shortradius}{%
\pgfmathsetmacro\end{\shortdiameter-\j}
\foreach \i in {0,...,\end}{%
\node[hexa] (h\i;\j) at ({(\i+\j/2-\shortradius*0.5)*sin(60)},{\j*0.75+\radius*0.75+0.75}) {}; %{\i;\j};
}
}
% color change(s)
\node[hexa, fill=blue!20!white] (h\radius;\radius) at ({(\radius-\radius/2)*sin(60)},{\radius*0.75}) {};
% manual numbering
\node (h4;4) at ({(4-4/2+0.5)*sin(60)},{4*0.75}) {0};
\node[numbr] (h4;3) at ({(4-3/2+0.5)*sin(60)},{3*0.75}) {1};
\node[numbr] (h3;3) at ({(3-3/2+0.5)*sin(60)},{3*0.75}) {2};
\node[numbr] (h3;4) at ({(3-4/2+0.5)*sin(60)},{4*0.75}) {3};
\node[numbr] (h3;0) at ({(3+0/2-\shortradius*0.5+0.5)*sin(60)},{0*0.75+\radius*0.75+0.75}) {4};
\node[numbr] (h4;0) at ({(4+0/2-\shortradius*0.5+0.5)*sin(60)},{0*0.75+\radius*0.75+0.75}) {5};
\node[numbr] (h5;4) at ({(5-4/2+0.5)*sin(60)},{4*0.75}) {6};
\node[numbr] (h5;3) at ({(5-3/2+0.5)*sin(60)},{3*0.75}) {7};
% ... etc ...
% special shape(s)
\pgfmathsetmacro\arrowlen{0.2}
\foreach \i in {4,...,7}{%
\draw[arro, ->, to path={-- (\tikztotarget)}]
({(\i-4/2+0.5)*sin(60)+\arrowlen/2},{4*0.75-\arrowlen}) to ({(\i-3/2+0.5)*sin(60)-\arrowlen/2},{3*0.75+\arrowlen});
}
\end{scope}
\end{tikzpicture}
\end{document}
答案1
这是一个可能的解决方案。
\documentclass[border=7pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\tikzset{
hexagone/.style={
draw, thick,
fill=blue!7,
shape=regular polygon,
regular polygon sides=6,
outer sep=0, inner sep=0,
minimum size=1cm,
label={[red]center:#1}
}
}
\begin{document}
\begin{tikzpicture}
\node[hexagone=0] at (0,0){};
\foreach \r in {1,...,3}
\foreach \t in {0,...,5}
\foreach[evaluate={\l=int(\r*(\r-1)*3+\r*\t+\u)}] \u in {1,...,\r}
\scoped[rotate=-\t*60]
\node[hexagone=\l] at (0+.75*\u,{0.43301270189*(2*\r-\u)}){};
\end{tikzpicture}
\end{document}
笔记:我还没有检查“环”的最大可能数量是多少,但对于 21,\foreach \r in {1,...,21}
它是有效的:)