我想使用 TikZ 绘制以下图片:
我已经这样做了
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=2]
\foreach \x in {0,60,...,300} {
\draw[fill] (\x:1 cm) -- (\x + 60:1 cm);
\draw[fill] (\x:1 cm) -- (\x + 180:1 cm);}
\foreach \x in {30,90,...,270} {
\draw[fill] (\x:0.866 cm) -- (\x + 180:0.866 cm);}
\end{tikzpicture}
\begin{tikzpicture}[scale=2]
\foreach \x in {0,72,...,288} {
\draw[fill] (\x:1 cm) -- (\x + 72:1 cm);
\draw[fill] (\x:1 cm) -- (\x + 180:0.809 cm);}
\end{tikzpicture}
\end{document}
但我想知道如何将这些图片放在一起并填充多边形内的空间。
这是我的解决方案:
\documentclass{report}
\usepackage{tikz}
\definecolor{fondo}{rgb}{0.898,0.996,0.898}
\definecolor{diagonal}{rgb}{0.466,0,0}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=2]
\begin{scope}
\draw[fill=fondo,very thick] (0:1 cm) -- (72:1 cm) -- (144:1 cm) -- (216:1 cm) -- (288:1 cm) -- cycle;
\foreach \x in {0,72,...,288} {
\draw[diagonal,thin] (\x:1 cm) -- (\x + 180:0.809 cm);}
\end{scope}
\begin{scope}[xshift=3cm]
\draw[fill=fondo,very thick] (0:1 cm) -- (60:1 cm) -- (120:1 cm) -- (180:1 cm) -- (240:1 cm) -- (300:1 cm) -- cycle;
\foreach \x in {0,60,...,300} {
\draw[diagonal,thin] (\x:1 cm) -- (\x + 180:1 cm);}
\foreach \x in {30,90,...,270} {
\draw[diagonal,thin] (\x:0.866 cm) -- (\x + 180:0.866 cm);}
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}
答案1
您可以使用 将两个图形放在一起scope
。要填充多边形,您需要确保您确实有一个多边形(即,一个 中至少有三条线段\draw
)。我在下面的代码中做到了这一点:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=2]
\begin{scope}
\foreach \x in {0,60,...,300} {
\draw[fill=blue!25] (0, 0) -- (\x:1 cm) -- (\x + 30:0.866 cm) -- cycle;
\draw[fill=blue!50] (0, 0) -- (\x + 30:0.866 cm) -- (\x + 60:1 cm) -- cycle;
}
\end{scope}
\begin{scope}[xshift=2cm]
\foreach \x in {0,72,...,288} {
\draw[fill=blue!25] (0, 0) -- (\x:1 cm) -- (\x + 36:0.809 cm) -- cycle;
\draw[fill=blue!50] (0, 0) -- (\x + 36:0.809 cm) -- (\x + 72:1 cm) -- cycle;
}
\end{scope}
\end{tikzpicture}
\end{document}
(该cycle
关键字的意思只是“画一条线段回到\draw
语句中的初始点。”)
而且,仅仅为了我自己的乐趣,这里有一个适用于任何 n 边形的概括。
\documentclass{standalone}
\usepackage{tikz}
\newcommand\polygon[2][]{
\pgfmathsetmacro{\angle}{360/#2}
\pgfmathsetmacro{\startangle}{-90 + \angle/2}
\pgfmathsetmacro{\y}{cos(\angle/2)}
\begin{scope}[#1]
\foreach \i in {1,2,...,#2} {
\pgfmathsetmacro{\x}{\startangle + \angle*\i}
\draw[fill=blue!35] (0, 0) -- (\x:1 cm) -- (\x + \angle/2:\y cm) -- cycle;
\draw[fill=blue!50] (0, 0) -- (\x + \angle/2:\y cm) -- (\x + \angle:1 cm) -- cycle;
}
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\polygon{5}
\polygon[xshift=2.2cm]{6}
\polygon[xshift=4.4cm]{7}
\polygon[xshift=6.6cm]{8}
\end{tikzpicture}
\end{document}
答案2
使用形状可以获得类似的结果regular polygon
。在这种情况下,pic
有助于在一个宏中绘制节点并用这些条纹填充它。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\tikzset{
bicolor polygon/.pic = {
\node[regular polygon, regular polygon sides=#1,
minimum size=4cm, draw, fill=blue!50,
outer sep=0pt] at (0,0) (-node){};
\foreach \i in {1,2,...,#1}
\draw[fill=blue!25] (-node.corner \i)
--(-node.side \i)--(-node.center)--cycle;
}}
\begin{tikzpicture}
\foreach \i [count=\d from 0] in {5,6,...,10}
\draw (4cm*\d,0) pic (a) {bicolor polygon=\i};
\end{tikzpicture}
\end{document}