Curve synthesis - Adding many (mostly piecewise linear) curves

It's a follow-up to a previous question. If I could have a macro out of the solution over there, it would have been great but then I couldn't make it.

1. The following code (to be added to the one in Paul Gaborit's answer in the previous thread) is not working yet, how to fix it?

% 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};

2. How to pass the number of samples (here 100) as input? A better way would be to be able to pass a list of x-coordinates (the kink location) where the vertical intersecting lines would be located.

In case of piecewise linear curves, an even better way would be indeed a way to merge all the x-coordinates of all the curves, sort them, and just intersect at those points.

-

Here an enhanced version of my answer with a generic method to add several curves (named paths).

You will notice the slowness of the whole! For these calculations, TeX is really not the right tool.

Result

Code

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

% 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);
\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
\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
\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