我想在平滑线和其他线之间进行绘制。我输入了以下代码。
\documentclass[dvipdfmx]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (O) at (0,0);
\coordinate (A) at (1,2);
\coordinate (B) at (2,1.5);
\coordinate (C) at (3,3);
\coordinate (D) at (4,2);
\draw[thick](0,2.5)--(4,2.5);
\draw[thick]plot[smooth,tension=0.7] coordinates {(O)(A)(B)(C)(D)};
\end{tikzpicture}
\end{document}
该图只是一张图片。我想沿着平滑的线条进行绘画,而不是沿着图中所示的锯齿状线条进行绘画。
答案1
您可以尝试软件包fillbetween
中自带的库pgfplots
:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots} % loads tikz
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
\coordinate (O) at (0,0);
\coordinate (A) at (1,2);
\coordinate (B) at (2,1.5);
\coordinate (C) at (3,3);
\coordinate (D) at (4,2);
\draw[thick, name path=A] (0,2.5) -- (4,2.5);
\draw[thick, name path=B] plot[smooth, tension=0.7] coordinates {(O) (A) (B) (C) (D)};
\begin{scope}[on background layer]
\fill[yellow, intersection segments={of=A and B, sequence={L1 -- R1[reverse]}}] -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}
或者,您可以使用该intersections
库:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections, backgrounds}
\begin{document}
\begin{tikzpicture}
\coordinate (O) at (0,0);
\coordinate (A) at (1,2);
\coordinate (B) at (2,1.5);
\coordinate (C) at (3,3);
\coordinate (D) at (4,2);
\draw[thick, name path=A] (0,2.5) -- (4,2.5);
\draw[thick, name path=B] plot[smooth, tension=0.7] coordinates {(O) (A) (B) (C) (D)};
\begin{scope}[on background layer]
\clip plot[smooth, tension=0.7] coordinates {(O) (A) (B) (C) (D)} |- (0,3) -- cycle;
\fill[yellow, name intersections={of=A and B}] (intersection-1) rectangle (0,0);
\end{scope}
\end{tikzpicture}
\end{document}
为了避免重复代码,你可以使用insert code
(如建议在这个很好的答案中):
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections, backgrounds}
\tikzset{draw curve/.style={insert path={
plot[smooth, tension=0.7] coordinates {(O) (A) (B) (C) (D)}
}}}
\begin{document}
\begin{tikzpicture}
\coordinate (O) at (0,0);
\coordinate (A) at (1,2);
\coordinate (B) at (2,1.5);
\coordinate (C) at (3,3);
\coordinate (D) at (4,2);
\draw[thick, name path=A] (0,2.5) -- (4,2.5);
\draw[thick, name path=B, draw curve];
\begin{scope}[on background layer]
\clip[draw curve] |- (0,3) -- cycle;
\fill[yellow, name intersections={of=A and B}] (intersection-1) rectangle (0,0);
\end{scope}
\end{tikzpicture}
\end{document}