为了说明什么边缘着色是,我想给一个正多边形上色。我想使用 TikZ 生成形状,然后分别给多边形的边 (边缘) 上色,例如通过执行以下操作:
\draw[red] (polygon.side 1);
不幸的是,世界似乎不是这样运转的。目前,我的丑陋解决方法是使用点线、虚线和粗线进行过度绘制。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,positioning}
\begin{document}
\begin{tikzpicture}
\node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
\draw[thick] (polygon.corner 1) -- (polygon.corner 2);
\draw[dashed] (polygon.corner 2) -- (polygon.corner 3);
\draw[thick] (polygon.corner 4) -- (polygon.corner 5);
\draw[dashed] (polygon.corner 5) -- (polygon.corner 1);
\end{tikzpicture}
\end{document}
谢谢!
答案1
使用regular polygon colors
选项,无需额外的命令来绘制边。一切都在节点命令中完成。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{shapes.geometric, positioning}
\begin{document}
\begin{tikzpicture}[
regular polygon colors/.style 2 args={
append after command={%
\pgfextra
\foreach \i [count=\ni, remember=\ni as \lasti (initially #1)] in {#2}{
\draw[\i] (\tikzlastnode.corner \lasti) --(\tikzlastnode.corner \ni);}
\endpgfextra
}
},
]
\node[minimum size=2cm, regular polygon,
regular polygon sides=5,
regular polygon colors={5}{red,blue,orange,green,cyan}] (polygon) {};
\node[minimum size=2cm, regular polygon,
regular polygon sides=7,
regular polygon colors={7}{red, blue, orange, green, cyan, violet, brown},
right=of polygon] (polygon2) {};
\end{tikzpicture}
\end{document}
答案2
像这样?
\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick]
\foreach \i/\c in {90/red,162/blue,234/green,306/pink,378/orange}{
\draw[\c] (\i:1cm) -- (72+\i:1cm);
}
\end{tikzpicture}
\end{document}
编辑1:另一种选择,是为了有更好的连接线:
\begin{tikzpicture}[thick]
\foreach \i/\c in {90/red,162/blue,234/green,306/pink,378/orange}{
\fill[\c] (\i:1cm) -- (72+\i:1cm) -- (0,0) -- cycle;
}
\fill[white] (90:.95cm)--(162:.95cm)--(234:.95cm)--(306:.95cm)--(378:.95cm)--cycle;
\end{tikzpicture}
编辑2:只是为了好玩。
\begin{tikzpicture}
\foreach \r in {1,.9,...,.1}{
\pgfmathparse{30 * \r}%
\pgfmathsetmacro\percent{\pgfmathresult}
\foreach \i/\c in {90/red,162/blue,234/green,306/pink,378/orange}{
\fill[\c!\percent] (\i:\r cm) -- (72+\i:\r cm) -- (0,0) -- cycle;
}}
\end{tikzpicture}
答案3
这里有一个提案,定义了一种polygon side
可以使用的样式,例如\draw[red,polygon side=2];
将第二面画成红色。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[get next vertex/.code n args={2}{%
\pgfmathparse{int(1+mod(#1,5))}
\xdef#2{\pgfmathresult}},
polygon side/.style={get next vertex={#1}{\tmpnextn},
insert path={(polygon.corner #1)-- (polygon.corner \tmpnextn)}}
]
\node[draw, dotted, minimum size=2cm, regular polygon,
regular polygon sides=5] (polygon) {};
\foreach \X [count=\Y] in {red,blue,orange,green,cyan}
{\draw[\X,polygon side=\Y];}
\end{tikzpicture}
\end{document}
请注意,截至目前,角(或边)的数量以及对象(或节点)的名称polygon
都是硬编码的。当然,可以使用 pgfkeys 将它们存储在一些键中,如果您要求我这样做,我很乐意将其添加到此答案中。