![如何画一圈六边形?](https://linux22.com/image/322753/%E5%A6%82%E4%BD%95%E7%94%BB%E4%B8%80%E5%9C%88%E5%85%AD%E8%BE%B9%E5%BD%A2%EF%BC%9F.png)
答案1
我不这样做tikz
,但无论如何我都觉得这很有趣。因此,我创建了一个\fillcrit
绘制橙色六边形的宏,使用序言顶部的几个调整后的参数值。然后,在中tikzpicture
,我只需执行\node at (6,6) {\fillcrit};
前创建六边形的循环(这样它就不会覆盖可能放置在其中的任何文本)。
编辑:我已经将每个六边形的坐标添加到图片中,以显示橙色六边形不会覆盖六边形标签,并验证橙色六边形是否以 (6,6) 为中心,即指定的位置。
\documentclass[border=2mm, tikz]{standalone}
\usepackage{marvosym,stackengine,scalerel}
%RELEVANT PARAMETERS FOR \fillcrit
\setstackgap{L}{8.65mm}% baselineskip for orange hex stack
\def\colorhexht{8.65mm} % height of orange hex itself
\def\boxraze{-3.6pt} % vertical shift of \Hexasteel for proper vertical centering
%THE FOLLOWING SHOULD NOT NEED ADJUSTMENT
\def\hshft{-.14\dimexpr\Lstackgap}% negative shift to get orange hex columns to mesh together
%%%
\def\hex{\color{orange}{\makebox[\Lstackgap]{\raisebox{\boxraze}{\scaleto{%
\mbox{\Hexasteel}}{\colorhexht}}}}}
\def\fillcrit{%
\Centerstack{\hex{} \hex}\kern\hshft%
\Centerstack{\hex{} \hex{} \hex}\kern\hshft%
\Centerstack{\hex{} \hex}%
}
\usetikzlibrary{shapes.geometric}
\begin{document}
%
% x=3*(minimum size)/2
% x=\sqrt{3/4}*(minimum size)/2
%
\begin{tikzpicture}[x=7.5mm,y=4.34mm]
% some styles
\tikzset{
box/.style={
regular polygon,
regular polygon sides=6,
minimum size=10mm,
inner sep=0mm,
outer sep=0mm,
rotate=0,
draw
}
}
\tikzset{
fillbox/.style={
regular polygon,
regular polygon sides=6,
minimum size=10mm,
inner sep=0mm,
outer sep=0mm,
rotate=0,
draw
}
}
\node at (6,6) {\fillcrit};
\foreach \i in {0,...,5}
\foreach \j in {0,...,5} {
\node[box] at (2*\i,2*\j) {%
\makebox[0pt]{\the\numexpr\i+\i,\the\numexpr\j+\j}};
\node[box] at (2*\i+1,2*\j+1) {%
\makebox[0pt]{\the\numexpr\i+\i+1,\the\numexpr\j+\j+1}};
}
\end{tikzpicture}
\end{document}
上述代码已重新编辑,将调整参数的数量从 4 个减少到 3 个(\hshft
以前可调的值现在固定为 ,.14\Lstackgap
约为\Lstackgap/(4*sqrt[3])
)。因此,\colorhexht
橙色框的高度可以重置为 ,例如 8.1 毫米,而无需任何额外的参数调整,以获得以下结果:
答案2
如果您了解元素(0,0)
位于左下角,每个元素x+1
向右移动一列并向y+2
上移动一行,那么很容易构建一个\fillaround
命令来填充(在六角形网格顶部绘制)中心六边形及其周围的所有六个元素。
\documentclass[border=2mm, tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
%#1 - optional argument to change fill color and dimensions
%#2 - x coordinate of central hexagon
%#3 - y coordinate of central hexagon
\newcommand{\fillaround}[3][fill=green!30]{
\node[filledhex,#1] at (#2,#3) {};
\node[filledhex,#1] at (#2,#3+2) {};
\node[filledhex,#1] at (#2-1,#3+1) {};
\node[filledhex,#1] at (#2-1,#3-1) {};
\node[filledhex,#1] at (#2,#3-2) {};
\node[filledhex,#1] at (#2+1,#3-1) {};
\node[filledhex,#1] at (#2+1,#3+1) {};
}
%
% x=\sqrt{3/4}*minimum size
% y=3/4*minimum size
%
\begin{tikzpicture}[x=7.5mm,y=4.34mm]
% some styles
\tikzset{
box/.style={
regular polygon,
regular polygon sides=6,
minimum size=10mm,
inner sep=0mm,
outer sep=0mm,
rotate=0,
draw
},
filledhex/.style={
box,
minimum size=9mm,
draw=none,
fill=green!30
}
}
\foreach \i in {0,...,5}
\foreach \j in {0,...,5} {
\node[box] at (2*\i,2*\j) {};
\node[box] at (2*\i+1,2*\j+1) {};
}
\fillaround{4}{4}
\fillaround[fill=red!50, minimum size=8mm]{8}{8}
\end{tikzpicture}
\end{document}