我想要绘制如下图像:
我使用以下 Tikz 代码完成了此操作:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepgflibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
[hexagon/.style={regular polygon, regular polygon sides=6,
draw, minimum size=1cm, anchor=center}]
\foreach \x in {0,...,2}
\node at (\x, 5) [hexagon]{};
\foreach \x in {0,...,3}
\node at (\x-0.5, 4.12) [hexagon]{};
\foreach \x in {0,...,4}
\node at (\x-1, 3.25) [hexagon]{};
\foreach \x in {0,...,3}
\node at (\x-.5, 2.38) [hexagon]{};
\foreach \x in {0,...,2}
\node at (\x, 1.52) [hexagon]{};
\end{tikzpicture}
\end{document}
有没有更好的方法来编码,使用更少的 Tikz 手动计算近似值?此外,如我们所见,线条以一种奇怪的不精确方式重叠。我也可以避免这种情况吗?
答案1
我意识到这个问题与以前绘制六边形的问题略有不同......那么,这个解决方案如何:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepgflibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
[hexagon/.style={regular polygon, regular polygon sides=6,
draw, minimum size=1cm, anchor=center}]
\foreach[evaluate=\y] \y in {-2,...,2}
{
\pgfmathparse{5-abs{ \y }}
\foreach \x in {1,...,\pgfmathresult}
\node at (abs \y /2 +\x, sqrt 3 * \y/2) [hexagon]{};
}
\end{tikzpicture}
\end{document}
得出的结果是: