曲线合成 - 将两条曲线相加得到另一条曲线

曲线合成 - 将两条曲线相加得到另一条曲线

情况 1:假设我有几个坐标 (A)、(B)、(C)、(W)、(X)、(Y) 和 (Z)(可能通过交叉、旋转等获得)。

我画两个分段线性曲线一方面穿过(A)、(B)、(C),另一方面穿过(W)、(X)、(Y)和(Z)。

情况 2:我有两条由两个函数定义的曲线,比如 f(x) = x + 3 和 g(x) = - x + 2。

情况 3:其中一条曲线由坐标定义,另一条曲线由函数定义

在所有这些情况下,是否有办法“添加”两条曲线以获得第三条曲线(例如 h(x) = f(x) + g(x))?

为了简单起见,我目前感兴趣的所有曲线都具有以下优良特性:分段线性但如果有一个全面的解决方案,请随意放松这一点。

案例 1 的示例:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}

\begin{document}

\begin{tikzpicture}
\newcounter{i}

% Curve 'one' .. is there a way to give it a name? 
% Coordinates saved for further labeling, and so on.
% How to do it smoothly?
\begin{scope}[red] 
  \setcounter{i}{0}%
  \foreach \point in {(0,4),(4,1),(10,6)} {%
    \node[coordinate] (one-\arabic{i}) at \point { };%
    \fill (one-\arabic{i}) circle (0.1);%
    \stepcounter{i}
  }
  \draw (one-0) -- (one-1) -- (one-2);
\end{scope}

% Curve 'two' .. 
% If I don't need to save the coordinates for later use, 
% there must be a better way to draw?
\begin{scope}[green] 
  \setcounter{i}{0}%
  \foreach \point in {(0,1),(3,5),(6,5),(10,2)} {%
    \node[coordinate] (two-\arabic{i}) at \point { };%
    \fill (two-\arabic{i}) circle (0.1);%
    \stepcounter{i}
  }
  \draw (two-0) -- (two-1) -- (two-2) -- (two-3);
\end{scope}

%%% What I want is the "sum" of curves one and two ...
\end{tikzpicture}

\end{document}

答案1

这是一个使用intersectionscalc库的解决方案。

方法

  1. 画出并命名(通过name path)第一个连续的小路 (X范围为0至10)。

  2. 画出并命名(通过name path)第二个连续的小路 (X范围为0至10)。

  3. ForeachX在 0,...,100 中:

    1. 画一条垂直线并命名X/10 (范围为路径的 y 最小值到 y 最大值)。
    2. 搜索name intersections此线与第一条路径之间以及此线与第二条路径之间的交点(通过)(您应该为每条路径找到一个交点)。
    3. 计算这两个交点的y 坐标(通过let操作)并在(x/10, sum)名为 处创建一个新的坐标sum-x
  4. 通过点画一条线(sum-0)(sum-100)

结果

一些添加的曲线

代码

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\begin{document}
\begin{tikzpicture}[line width=1pt]
  % a grid
  \draw[help lines] (0,-.5) grid (10,10);
  % x axis
  \draw[-latex,thick] (0,0) -- (10,0) node[right]{$x$};

  % red line
  \def\lineone{(0,4),(4,1),(8,6),(10,6)}
  \foreach \point[count=\c] in \lineone {%
    \coordinate[at=\point] (one-\c);%
    %\fill[red] (one-\c) circle (0.1);%
  }
  \draw[red,name path=one] (one-1) \foreach \i in {2,...,\c}{-- (one-\i)} node[right]{one};

  % blue line
  \def\linetwo{(0,1),(3,5),(4,2),(6,5),(10,2)}
  \foreach \point[count=\c] in \linetwo {%
    \coordinate[at=\point] (two-\c);%
    %\fill[blue] (two-\c) circle (0.1);%
  }
  \draw[blue,name path=two] (two-1) \foreach \i in {2,...,\c}{-- (two-\i)} node[right]{two};

  % one + two
  \foreach \c in {0,...,100} {
    \pgfmathsetmacro{\x}{\c/10}
    \path[name path=line] (\x,0) -- (\x,6);
    \path[name intersections={of=one and line,name=newone}];
    \path[name intersections={of=two and line,name=newtwo}];
    \path let \p1=(newone-1), \p2=(newtwo-1) in 
    (\x1,\y1+\y2) coordinate (sum-\c);
  }
  \draw[red!50!blue]
  (sum-0) \foreach \x in {1,...,100}{-- (sum-\x)} node[right]{one + two};

  % a green function
  \draw[green!50!black,name path=three]
  plot[domain=0:10,samples=100,smooth] (\x,{sin(3*\x r)+2}) node[right]{three};

  % one + three
  \foreach \c in {0,...,100} {
    \pgfmathsetmacro{\x}{\c/10}
    \path[name path=line] (\x,0) -- (\x,6);
    \path[name intersections={of=one and line,name=newone}];
    \path[name intersections={of=three and line,name=newthree}];
    \path let \p1=(newone-1), \p2=(newthree-1) in 
    (\x1,\y1+\y2) coordinate (sum-\c);
  }
  \draw[red!50!green!50!black]
  (sum-0) \foreach \x in {1,...,100}{-- (sum-\x)} node[right]{one + three};

  % a orange function
  \draw[orange,name path=four]
  plot[domain=0:10,samples=100,smooth] (\x,{cos(1*\x r)+4}) node[right]{four};

  % four + three
  \foreach \c in {0,...,100} {
    \pgfmathsetmacro{\x}{\c/10}
    \path[name path=line] (\x,0) -- (\x,6);
    \path[name intersections={of=four and line,name=newfour}];
    \path[name intersections={of=three and line,name=newthree}];
    \path let \p1=(newfour-1), \p2=(newthree-1) in 
    (\x1,\y1+\y2) coordinate (sum-\c);
  }
  \draw[orange!50!green!50!black]
  (sum-0) \foreach \x in {1,...,100}{-- (sum-\x)} node[right]{three + four};

\end{tikzpicture}
\end{document}

相关内容