使用 tikzfillbetween 填充曲线之间的区域

使用 tikzfillbetween 填充曲线之间的区域

我需要用填充几条曲线之间的区域tikzfillbetween,但我不想填充内圆。

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}[scale=1]
\def\R{1}
\def\s{0.2}

\draw [black, line width=1pt,samples=100,domain=0:2*pi, name path=A] 
plot ({\R*cos(\x r)}, {\R*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=-pi/6:pi/6, name path=B] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=pi/6:pi/2, name path=C] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=pi/2:5/6*pi, name path=D] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=5*pi/6:7*pi/6, name path=E] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=7*pi/6:3*pi/2, name path=F]
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=3*pi/2:11*pi/6, name path=G] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\tikzfillbetween[of=A and B]{blue};
\tikzfillbetween[of=A and C]{red};
\tikzfillbetween[of=A and D]{blue};
\tikzfillbetween[of=A and E]{red};
\tikzfillbetween[of=A and F]{blue};
\tikzfillbetween[of=A and G]{red};

\end{tikzpicture}

\end{document}

答案1

我以前从未使用过pgfplots,我注意到您的代码是通过围绕原点旋转来制作 6 次相同的曲线。所以我使用了两个foreach循环:一个循环用蓝色着色,另一个循环用红色着色,代码与您相同。

其结果是:

地块

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}[scale=1]
\def\R{1}
\def\s{0.2}

\foreach \d in {-pi/6:pi/6,pi/2:5/6*pi,-pi/2:-5*pi/6
}{
\draw [black, line width=1pt,samples=100,domain=\d, name path=B] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});
\draw [black, line width=1pt,samples=100,domain=\d, name path=A] 
plot ({\R*cos(\x r)}, {\R*sin(\x r)});
\tikzfillbetween[of=B and A]{blue};
}

\foreach \d [count=\c] in {pi/2:pi/6,5*pi/6:7*pi/6,3*pi/2:11*pi/6
}{
\draw [black, line width=1pt,samples=100,domain=\d, name path=C] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});
\draw [black, line width=1pt,samples=100,domain=\d, name path=A] 
plot ({\R*cos(\x r)}, {\R*sin(\x r)});
\tikzfillbetween[of=A and C]{red};
}

\end{tikzpicture}

\end{document}

答案2

我对这个问题的目的有点困惑。如果你想使用fillbetween你正在加载的库,请切换到下面的代码。如果你只是想用不同的颜色填充不同的位,只需这样做

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\R{1}
\def\s{0.2}
\foreach \X in {0,...,5}
{\ifodd\X
\draw [fill=red, line width=1pt,samples=100,domain={-pi/6+\X*pi/3}:{pi/6+\X*pi/3}] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x
r)});
\else
\draw [fill=blue, line width=1pt,samples=100,domain={-pi/6+\X*pi/3}:{pi/6+\X*pi/3}] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x
r)});
\fi}
\draw[fill=white,line width=1pt] (0,0) circle (\R);
\end{tikzpicture}
\end{document}

另一方面,这是针对这种特殊情况的特殊答案,并且在您切换到更多不同形状的那一刻就会失败。可以使用您正在加载的pgfplots库来设置始终有效的东西fillbetween。为了实现您想要的效果,请考虑填充交叉序列而不是完整路径。A很长,如果你填满整个东西,圆圈就会被填满。

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}[scale=1]
\def\R{1}
\def\s{0.2}

\draw [black, line width=1pt,samples=100,domain=0:2*pi, name path=A] 
plot ({\R*cos(\x r)}, {\R*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=-pi/6:pi/6, name path=B] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=pi/6:pi/2, name path=C] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=pi/2:5/6*pi, name path=D] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=5*pi/6:7*pi/6, name path=E] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=7*pi/6:3*pi/2, name path=F]
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\draw [black, line width=1pt,samples=100,domain=3*pi/2:11*pi/6, name path=G] 
plot ({(\R + abs(\s*cos(3*\x r)))*cos(\x r)}, {(\R + abs(\s*cos(3*\x r)))*sin(\x r)});

\fill [blue,intersection segments={of=A and B,
                sequence={A0 -- B1[reverse]}}];
\fill [red,intersection segments={of=A and C,
                sequence={A1 -- B1[reverse]}}];
\fill [blue,intersection segments={of=A and D,
                sequence={A1 -- B1[reverse]}}];
\fill [red,intersection segments={of=A and E,
                sequence={A1 -- B1[reverse]}}];
\fill [blue,intersection segments={of=A and F,
                sequence={A1 -- B1[reverse]}}];
\fill [red,intersection segments={of=A and G,
                sequence={A1 -- B1[reverse]}}];

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容