我想绘制两个波浪形轮廓,如下图所示,并填充中间的区域。我尝试了以下代码,但它在右侧产生了不想要的黑色垂直边缘。我该如何去除它?
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\useasboundingbox (-1,-1) rectangle (6,2);
\draw[fill=black!10] (0,1) to[out=0,in=180] ++(5,-0.6) -- ++(0,0.6) to[out=180,in=0] ++(-5,-0.6);
\end{tikzpicture}
\end{document}
我也尝试删除第五行--
之间的++(5,-0.6)
和++(0,0.6)
,它不会画出不需要的边缘,但它会弄乱填充。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\useasboundingbox (-1,-1) rectangle (6,2);
\draw[fill=black!10] (0,1) to[out=0,in=180] ++(5,-0.6) ++(0,0.6) to[out=180,in=0] ++(-5,-0.6);
\end{tikzpicture}
\end{document}
答案1
快速而粗略:先填充区域,然后绘制您想要绘制的内容。关闭部分路径的绘制并不容易(但可以做到,但需要付出更多努力)。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\useasboundingbox (-1,-1) rectangle (6,2);
\path[fill=black!10] (0,1) to[out=0,in=180] ++(5,-0.6) -- ++(0,0.6) to[out=180,in=0] ++(-5,-0.6);
\draw (0,1) to[out=0,in=180] ++(5,-0.6)++(0,0.6) to[out=180,in=0] ++(-5,-0.6);
\end{tikzpicture}
\end{document}
你能打开或关闭路径例如使用这个答案。
\documentclass[tikz]{standalone}
\pgfkeys{tikz/.cd,
edge options/.code={\tikzset{edge style/.style={#1}}},
}
\begin{document}
\begin{tikzpicture}[every edge/.append code = {% https://tex.stackexchange.com/a/396092/121799
\global\let\currenttarget\tikztotarget % save \tikztotarget in a global variable
\pgfkeysalso{append after command={to[edge style] (\currenttarget)}}},
every edge/.append style={edge style} ]
\useasboundingbox (-1,-1) rectangle (6,2);
\path[fill=black!10]
(0,1) [edge options={out=0,in=180,draw=black}] edge ++(5,-0.6)
-- ++(0,0.6)
[edge options={out=180,in=0,draw=black}] edge ++(-5,-0.6);
\end{tikzpicture}
\end{document}
答案2
使用 TikZ 库pgfplots.fillbetween
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{pgfplots.fillbetween}
\begin{document}
\begin{tikzpicture}
\draw[name path=A] (0, 0.3) to [out=0,in=180] ++(5,-0.6);
\draw[name path=B] (0,-0.3) to [out=0,in=180] ++(5, 0.6);
\tikzfillbetween[of=A and B] {fill=gray!30};
\end{tikzpicture}
\end{document}
答案3
为了比较,元帖子,其中填充和绘制是分开的,并且您可以直接连接路径变量。
\documentclass[border=5mm]{standalone}
\usepackage{luatex85}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);
z0 = (-72, 8.5);
path A, B;
A = z0 {right} .. {right} z0 rotated 180;
B = A reflectedabout(up, down);
fill A -- B -- cycle withcolor 7/8[blue, white];
draw A;
draw B;
endfig;
\end{mplibcode}
\end{document}
(编译为lualatex
...)