创建六边形样式

创建六边形样式

我想为六边形创建一个样式,如下所示:

在此处输入图片描述

但是,我不知道该怎么做。这是我目前的代码:

\begin{tikzpicture}[
    ]

    \newdimen\R
    \R=2.7cm
    \draw (0:\R)
    \foreach \x in {60,120,...,360} {  -- (\x:\R) }
        -- cycle (360:\R)  
        -- cycle (300:\R) 
        -- cycle (240:\R) 
        -- cycle (180:\R) 
        -- cycle  (120:\R)
        -- cycle  (60:\R) ;
\end{tikzpicture}

只画一个六边形。谢谢你的帮助

答案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之前使用的解决方案不会继承sidecorner锚定。此替代方案应该重新使用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}

在此处输入图片描述

相关内容