答案1
还有另一个可能的解决方案。
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,intersections,calc}
\begin{document}
\begin{tikzpicture}[]
\def\rot{-60}
\node[regular polygon, regular polygon sides=6, shape aspect=0.5, minimum width=4cm, minimum height=1cm, draw,shape border rotate=\rot] (reg) {};
\path [draw,name path=beam1] ($(reg.corner 3)!0.65!(reg.corner 4)$)coordinate(A)--($(reg.corner 5)!0.35!(reg.corner 6)$)coordinate(B);
\begin{scope}[rotate=\rot]
\path [name path=beam2](reg.side 4)--++(90:2);
\path [name intersections={of=beam1 and beam2,by={x}}];
\draw (reg.side 4)--(x);
\filldraw [opacity=0.3,cyan](A)--(B)--(reg.corner 5)--(reg.corner 4)--cycle;
\node[rotate=\rot] at (x)[anchor=north west]{label};
\node[rotate=\rot] at (x)[anchor=north east]{label};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
可能的解决方案:
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
mylabel/.style={rectangle, draw, minimum width=2cm, minimum height=6mm, inner sep=1pt,
fill=#1},
mylabel/.default={blue!30}
]
\node[regular polygon, regular polygon sides=6, draw, inner sep=1.7cm,
path picture={%
\node[mylabel, anchor=south west] at (path picture bounding box.south) {label};
\node[mylabel=red!30, anchor=south east] at (path picture bounding box.south) {label};
}
] (hexagon) {};
\end{tikzpicture}
\end{document}
如果您希望在其他侧面贴标签,这不是一个好的解决方案,因为path picture bounding box
之前使用的解决方案不会继承side
和corner
锚定。此替代方案应该重新使用hexagon
边框作为剪切路径,但我不知道该怎么做,所以我用手动路径来做。
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
mylabel/.style={rectangle, draw, minimum width=2cm, minimum height=6mm, inner sep=1pt,
fill=#1},
mylabel/.default={blue!30}
]
\node[regular polygon, regular polygon sides=6, draw, inner sep=1.7cm,
shape border rotate=30] (hexagon) {};
\begin{scope}
\clip (hexagon.side 2)--(hexagon.corner 3)--(hexagon.corner 4)--(hexagon.side 4)--cycle;
\node[mylabel, anchor=south west, rotate=-30] at (hexagon.side 3) {label};
\node[mylabel=red!30, anchor=south east, rotate=-30] at (hexagon.side 3) {label};
\end{scope}
\end{tikzpicture}
\end{document}