我喜欢在 (DIN A0) 页面的背景中使用一组规则多边形。图案需要根据多边形线条的粗细和颜色进行调整。此外,它们应该以移位的方式排列,以便形成漂亮的图案(图片仅用于展示图案):
我的 MWE:
\documentclass[a0paper]{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{calc}
\begin{document}
% Text or other content goes here
Here is your text or other content that should appear before the hexagons.
% Here come the hexagons
\begin{tikzpicture}[remember picture,overlay, scale=0.1] % Scaling
% Define the size of the hexagon
\pgfmathsetmacro{\radius}{15} % Radius
% Calculate the horizontal and vertical spacing between hexagons
\pgfmathsetmacro{\horizontalSpacing}{3 * \radius * 0.866} % Smaller spacing
\pgfmathsetmacro{\verticalSpacing}{3 * \radius} % Smaller spacing
% Loop through rows and columns to draw hexagons
\foreach \row in {0,...,7} {
\foreach \col in {0,...,14} {
\pgfmathsetmacro{\x}{\col*\horizontalSpacing}
\pgfmathsetmacro{\y}{-\row*\verticalSpacing}
\draw[fill=none,draw=black!20] (\x,\y) +(30:\radius cm) \foreach \angle in {90,150,...,360} {
-- +(\angle:\radius cm)
} -- cycle;
}
}
\end{tikzpicture}
\end{document}
答案1
只需一点数学知识,您就可以得出以下适用于任何纸张尺寸的公式。最下方六边形的中心位于左下角:
\documentclass{article}
\usepackage{tikz}
\begin{document}
% Here come the hexagons
\begin{tikzpicture}[remember picture, overlay]
% calculate the number of necessary rows and columns
\pgfmathsetmacro{\nx}{ceil(\paperwidth/2cm)+1}
\pgfmathsetmacro{\ny}{ceil(\paperheight/((1+sin(30))*1cm))+1}
% anchor the whole picture at the lower left corner
\begin{scope}[shift={(current page.south west)}]
\foreach \row in {0,...,\ny} {
\foreach \col in {0,...,\nx} {
\draw[black!20]
% directly calculate the center of the hexagon
({cos(30)*2*(\col+mod(\row,2)/2)},{(1+sin(30))*\row})
+(30:1)
\foreach \angle in {90,150,...,360} {
-- +(\angle:1)
} -- cycle;
}
}
\end{scope}
\end{tikzpicture}%
% Text or other content goes here
Here is your text or other content that should appear before the hexagons.
\end{document}
但是等一下!我是怎么想到的({cos(30)*2*(\col+mod(\row,2)/2)},{(1+sin(30))*\row})
?让我们分析一下:
- 首先X坐标的一部分,即
cos(30)*2*(\col+mod(\row,2)/2)
因为我决定把缩放留给tikz图片,我选择六边形外接圆的半径为 1(以厘米为单位),这样六边形每个角到中心的距离都是 1。因此六边形的宽度为cos(30)*2
,这是三角学得出的结果。
现在我们还需要每隔一行将六边形移动一半宽度,这可以通过使用来实现,mod(\row,2)/2
因为mod(\row,2)
每个奇数输出 1,每个偶数输出 0 \row
。
- 这是坐标的一部分,
(1+sin(30))*\row
也可以用基本三角学方法计算。
您甚至可以省略一个\foreach
循环并仅绘制六边形的右侧,因为其他边无论如何都会在下一次迭代中绘制:
\documentclass{article}
\usepackage{tikz}
\begin{document}
% Here come the hexagons
\begin{tikzpicture}[remember picture, overlay]
\pgfmathsetmacro{\nx}{ceil(\paperwidth/2cm)+1}
\pgfmathsetmacro{\ny}{ceil(\paperheight/((1+sin(30))*1cm))+1}
\begin{scope}[shift={(current page.south west)}]
\foreach \row in {0,...,\ny} {
\foreach \col in {0,...,\nx} {
\draw[black!20]
({cos(30)*2*(\col+mod(\row,2)/2)},{(1+sin(30))*\row})
+(90:1) -- +(30:1) -- +(-30:1) -- +(-90:1);
}
}
\end{scope}
\end{tikzpicture}%
% Text or other content goes here
Here is your text or other content that should appear before the hexagons.
\end{document}
与上面输出相同(甚至可能更干净一点)。
使用scale=2
或其他缩放比例,您需要调整\nx
和的公式\ny
:
\documentclass{article}
\usepackage{tikz}
\begin{document}
% Here come the hexagons
\begin{tikzpicture}[remember picture, overlay, scale=2]
\pgfmathsetmacro{\nx}{ceil(\paperwidth/2cm)/2+1}
\pgfmathsetmacro{\ny}{ceil(\paperheight/((1+sin(30))*1cm))/2+1}
\begin{scope}[shift={(current page.south west)}]
\foreach \row in {0,...,\ny} {
\foreach \col in {0,...,\nx} {
\draw[black!20]
({cos(30)*2*(\col+mod(\row,2)/2)},{(1+sin(30))*\row})
+(90:1) -- +(30:1) -- +(-30:1) -- +(-90:1);
}
}
\end{scope}
\end{tikzpicture}%
% Text or other content goes here
Here is your text or other content that should appear before the hexagons.
\end{document}