填充贝塞尔曲线下方的区域

填充贝塞尔曲线下方的区域

我使用以下代码来填充函数和 x 轴之间的区域颜色,并且它可以正常工作。

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\foreach \x in {1.000,1.001,...,4.500}   
\draw[purple!15!white] (\x,{0})--(\x, {0.5*cos(deg(0.5*pi*\x)+0.5*pi)+2});
\draw[very thick, ->] (-1,0) -- (6,0)node[pos=1,below]{$x$};
\draw[very thick, ->] (0,-1) -- (0,4)node[pos=1,left]{$y$};
\node[below left](o) at (0,0){$0$};
\draw[ultra thick,purple,smooth,domain=0.5:5,samples=257,]
plot (\x, {0.5*cos(deg(0.5*pi*\x)+0.5*pi)+2});
\end{tikzpicture}
\end{document}

在此处输入图片描述

我怎样才能做同样的事情,使用贝塞尔曲线(例如 \draw[ultra thick, purple,smooth] (0.5,1.5)..controls(2,-0.5) 和 (3.5,4.5)..(5,2.5);)而不是 0.5余弦(度(0.5pi*\x)+0.5*pi)+2?

答案1

用大约 3500 条细条纹填充一个区域是我最后的手段。只是没有深入研究 PGFPlots'fillbetween图书馆(看)和整个权力spath3图书馆我们可以通过使用适当的来做到这一点\clip

current subpath start一个动态坐标,它引用当前子路径的起点——感谢 Caption Obvious——这是最后一个移至操作降落。这就是cycle我们要去的地方。

然而,我们不想直接循环到该点,而是想将其下方的所有内容都包含在内,直到= 0.
该坐标(0,0 -| current subpath start)为的基点current subpath start

代码

\documentclass[a4paper, 10pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
  scale=1.2,
  declare function={
    trig(\x)=.5*cos(deg(.5*pi*\x)+.5*pi)+2;
  }]
\fill[purple!15] plot[smooth, domain=1:4.5, samples=257] (\x, trig \x)
                 |- (0,0-|current subpath start) -- cycle;

\draw[very thick, ->] (-1,0) -- (6,0)node[below]{$x$};
\draw[very thick, ->] (0,-1) -- (0,4)node[left] {$y$};
\node[below left] {$0$};
\draw[ultra thick, purple, smooth, domain=0.5:5, samples=257]
  plot (\x, trig \x);
\end{tikzpicture}

\begin{tikzpicture}[
  scale=1.2,
  my curve/.style={
    insert path={(0.5,1.5)..controls(2,-0.5) and (3.5,4.5)..(5,2.5)}}
]
\begin{scope}
  \clip (1,0) rectangle (4.5, 4); % the y value of 4 needs to be carefully chosen
  \fill[purple!15, my curve] |- (0,0-|current subpath start) -- cycle;
\end{scope}
\draw[very thick, ->] (-1,0) -- (6,0) node[below]{$x$};
\draw[very thick, ->] (0,-1) -- (0,4) node[left] {$y$};
\node[below left] {$0$};
\draw[ultra thick, purple, my curve];
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

在此处输入图片描述

答案2

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[scale=1.2]
        \begin{scope}
            \clip (0.5,0) -- (0.5,1.5) ..controls (2,-0.5) and (3.5,4.5).. (5,2.5) |- cycle;
            \foreach \x in {1.000,1.001,...,4.500}   
                \draw[purple!15!white] (\x,{0}) -- ++(0,4);
        \end{scope}
        \draw[very thick, ->] (-1,0) -- (6,0)node[pos=1,below]{$x$};
        \draw[very thick, ->] (0,-1) -- (0,4)node[pos=1,left]{$y$};
        \node[below left] (o) at (0,0){$0$};
        \draw[ultra thick, purple, smooth]
            (0.5,1.5) ..controls (2,-0.5) and (3.5,4.5).. (5,2.5);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

由于plot可以用于路径,这里有一个简单的解决方案:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1.2]
  \fill[purple!15!white](1,0) 
  -- plot[smooth,domain=1:4.5,samples=257](\x,{0.5*cos(deg(0.5*pi*\x)+0.5*pi)+2})
  -- (4.5,0) -- cycle;
  \draw[very thick, ->] (-1,0) -- (6,0)node[pos=1,below]{$x$};
  \draw[very thick, ->] (0,-1) -- (0,4)node[pos=1,left]{$y$};
  \node[below left](o) at (0,0){$0$};
  \draw[ultra thick,purple,smooth,domain=0.5:5,samples=257,]
  plot (\x, {0.5*cos(deg(0.5*pi*\x)+0.5*pi)+2});
\end{tikzpicture}
\end{document}

在此处输入图片描述

两个图之间使用相同的方法(declare function为了易读性使用):

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1.2]
  \tikzset{
    declare function={
      f1(\x) = 0.5*cos(deg(0.5*pi*\x)+0.5*pi)+1;
      f2(\x) = 0.5*cos(deg(2*pi*\x)+0.5*pi)+3;
    },
    samples=256,
  }
  \fill[purple!15!white] plot[smooth,domain=1:4.5](\x,{f1(\x)})
                     -- (4.5,0) -- (1,0) -- cycle;
  \fill[cyan!15!white] plot[smooth,domain=1.5:4](\x,{f1(\x)})
                   --  plot[smooth,domain=4:1.5](\x,{f2(\x)}) -- cycle;
  \draw[very thick, ->] (-1,0) -- (6,0)node[pos=1,below]{$x$};
  \draw[very thick, ->] (0,-1) -- (0,4)node[pos=1,left]{$y$};
  \node[below left](o) at (0,0){$0$};
  \draw[ultra thick,purple] plot[smooth,domain=0.5:5] (\x,{f1(\x)});
  \draw[ultra thick,cyan]   plot[smooth,domain=0.5:5] (\x,{f2(\x)});
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容