答案1
这是一种非常不灵活的方法(但至少六边形的边长由 PGFMath 函数给出a
)。
\ifnum
和混乱\ifodd
确保没有在错误的地方绘制三角形并且不会多次绘制。
这\columnCheck
是为了再次交替每三行和每四行的颜色。
代码
\documentclass[tikz]{standalone}
\tikzset{
polygon path/.style n args={3}{% 1: phase, 2: radius, 3: n
insert path={plot[sharp cycle, samples at={0, ..., \pgfinteval{#3-1}}]
({#1+\x*360/(#3)}:{#2})}}}
\begin{document}
\begin{tikzpicture}[
declare function={a=.75;}, thick,
@/.style args={#1=#2}{fill #1/.style={fill={#2}}},
@/.list={
-30=brown, 90=brown, 30=yellow, 150=yellow, -90=yellow,% for the triangles
00=green, 01=red, 10=orange, 11=blue},% for the hexagons
@/.style args={#1=#2}{
polygon path #1/.style={polygon path={-30+#2*60}{a/sqrt 3}{3}}},
@/.list={-30=0, 90=0, 30=1, 150=1, -90=1}]
\foreach[evaluate={\cols=int(isodd(\r)?7:6);}] \r in {1, ..., 4}
\foreach[evaluate={\columnCheck=int(mod((\r-1)/2,2));}]\c in {1, ..., \cols}{
\tikzset{shift={({2*a*(\c+iseven(\r)*.5)},2*a*.86602540378*\r)}}
\draw[
fill \ifodd\r\space1\else0\fi\ifodd\pgfinteval{\c+\columnCheck} 1\else0\fi,
polygon path={0}{a}{6}];
\unless\ifnum\c=7
\foreach[expand list] \ang in {-30, 30%
\ifnum\r<4 \ifnum\c=1 \ifodd\r , 90\fi\fi\fi
\ifnum\c=1 \ifnum\r>1 \ifodd\r ,-90\else\ifnum\r=4 , 150\fi\fi\fi\fi
} \draw[shift={(\ang:sqrt 3/1.5*a)}, fill \ang, polygon path \ang];
\fi
}
\end{tikzpicture}
\end{document}