这是上一个问题。如果我能从那里的解决方案中获得一个宏,那就太好了,但我做不到。
以下代码(将添加到上一个主题中 Paul Gaborit 的回答中的代码)尚未运行,如何修复它?
% one + two + 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=inter}]; % How to initialize sum? \foreach \curve in {two,three}{ \path[name intersections={of=curve and line,name=newinter}]; \path let \p1=(inter-1), \p2=(newinter-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 + two + three};
如何传递样本数量(此处为 100)作为输入?更好的方法是能够传递垂直相交线所在的 x 坐标(扭结位置)列表。
对于分段线性曲线,更好的方法确实是合并所有曲线的 x 坐标,对它们进行排序,然后只在这些点相交。
答案1
这是我的答案的增强版本,使用通用方法添加多条曲线(命名路径)。
你会注意到整体的缓慢!对于这些计算,TeX 确实不是合适的工具。
结果
代码
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\newcommand\addcurves[7]{
% samples, xmin, xmax, ymin, ymax, list of paths, prefix
\bgroup
\edef\samples{#1}
\edef\xmin{#2}
\edef\xmax{#3}
\edef\ymin{#4}
\edef\ymax{#5}
\def\listofpaths{#6}
\edef\prefix{#7}
%
\foreach \c in {0,...,\samples} {
\pgfmathsetmacro{\x}{\c/\samples * (\xmax-\xmin)+\xmin}
% verticale line
\path[name path=line] (\x,\ymin) -- (\x,\ymax);
% initialize sum (y=0)
\coordinate (\prefix-\c) at (\x,0);
% add each path
\foreach \curve in \listofpaths {
\path[name intersections={of=line and \curve,name=inter}];
\path let \p1=(inter-1), \p2=(\prefix-\c) in
(\x2,\y1+\y2) coordinate (\prefix-\c);
}
}
\egroup
}
\begin{document}
\begin{tikzpicture}[line width=1pt]
\def\samples{100}
% a grid
\draw[help lines] (0,-.5) grid (10,10);
% x axis
\draw[-latex,thick] (0,0) -- (10,0) node[right]{$x$};
% one (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};
% two (blue line)
\def\linetwo{(0,1),(3.33,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
\addcurves{\samples}{0}{10}{0}{6}{one,two}{sum}
\draw[red!50!blue]
(sum-0) \foreach \x in {1,...,\samples}{-- (sum-\x)} node[right]{one + two};
% three (a green function)
\draw[green!50!black,name path=three]
plot[domain=0:10.001,samples=\samples,smooth]
(\x,{sin(3*\x r)+2}) node[right]{three};
% one + three
\addcurves{\samples}{0}{10}{0}{6}{one,three}{sum}
\draw[red!50!green!50!black]
(sum-0) \foreach \x in {1,...,\samples}{-- (sum-\x)} node[right]{one + three};
% four (orange function)
\draw[orange,name path=four]
plot[domain=0:10.001,samples=\samples,smooth]
(\x,{cos(1*\x r)+4}) node[right]{four};
% four + three
\addcurves{\samples}{0}{10}{0}{6}{three,four}{sum}
\draw[orange!50!green!50!black]
(sum-0) \foreach \x in {1,...,\samples}{-- (sum-\x)} node[right]{three + four};
% one + two + three
\addcurves{\samples}{0}{10}{0}{6}{one,two,three}{sum}
\draw[red!50!green!50!black] (sum-0)
\foreach \x in {1,...,\samples}{-- (sum-\x)} node[right]{one + two + three};
\end{tikzpicture}
\end{document}