答案1
可以在 TikZ 中重新定义坐标系。示例使用 3D 坐标系。中间的六边形是原点(0, 0, 0)
。X轴向右下方移动,是轴在右上方,是轴至顶部。
完整示例:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\Unit{10mm}
\def\Num{3}
\pgfmathsetlengthmacro\xdiff{cos(30) * \Unit}
\pgfmathsetlengthmacro\ydiff{sin(30) * \Unit}
\tikzset{
x={(\xdiff, -\ydiff)},
y={(\xdiff, \ydiff)},
z={(0, \Unit)},
}
\pgfmathsetlengthmacro\radius{\Unit/2/cos(30)}
\draw
\foreach \x in {-\Num, ..., \Num} {
\foreach \z in {
\the\numexpr -\Num \ifnum\x>0 +\x \fi \relax,
...,
\the\numexpr \Num \ifnum\x<0 +\x \fi \relax
} {
(\x, 0, \z)
+(0:\radius)
\foreach \i in {1, ..., 5} {
-- +(60*\i:\radius)
}
-- cycle
}
}
;
\draw[very thick, blue]
(-1, 0) -- (0, 0, 1) -- (0, 1) -- (1, 0) --
(0, 0, -1) -- (-2, 0, -1) -- (-2, 0) -- (-2, 2) --
(0, 2) -- (2, 0) -- (0, 0, -2) -- (-3, 0, -2) --
(-3, 0)
;
\end{tikzpicture}
\end{document}
答案2
好吧,我很喜欢自动化代码,但在这种情况下它可能不值得。我认为你可以使用,plot
因为它更容易输入坐标/节点名称。
label=center:{h\count\n}
通过添加到节点选项,可以“看到”每个节点的节点名称。h
代表六边形,而其他两个变量将添加数字以使每个节点名称唯一。
此外,您可以根据需要使情节变得流畅或尖锐。
输出
平滑图
尖锐的情节
代码
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\tikzset{hexa/.style={shape=regular polygon,
regular polygon sides=6,
minimum size=1cm,
draw,
inner sep=0mm,
outer sep=0mm,
anchor=south,
fill=white},
hl/.style={line width=2pt,line cap=round}
}
\begin{document}
\begin{tikzpicture}[x=1cm, y=5mm]
\foreach \m [count=\count] in {1,2,3,4,3,4,3,4,3,4,3,2,1}{
\foreach \n in {1,...,\m}{
\node at ({(\n-\m/2)*sin(30)*3},{\count*sin(60)})
[hexa] (h\count\n) {};
}
}
% Add [smooth] to plot to make it smooth, such as 'plot[smooth]'
\draw[hl] plot coordinates {(h82) (h92) (h83) (h63) (h52) (h71) (h91) (h112) (h93) (h53) (h32) (h61) (h101)};
\end{tikzpicture}
\end{document}