5

Currently implementing the solution of Curve synthesis - Adding two curves to get another one. I would like to smooth out the resulting curve. The present code:

% red line
\path[red,name path=one] plot [smooth] coordinates {(0,0) (7.5,0) (8,6) (8.5,0) (10,0)};

% function
\path[blue,name path=three]
plot[domain=0:10,samples=100,smooth] (\x,{0.5*rand+2});

% red line + function
\foreach \c in {0,...,100} {
\pgfmathsetmacro{\x}{\c/10}
\path[name path=line] (\x,-10) -- (\x,20);
\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[blue] (sum-0) \foreach \x in {1,...,100}{ -- (sum-\x)};

What \foreach syntax should i use instead of:

\draw[blue] (sum-0) \foreach \x in {1,...,100}{ -- (sum-\x)};

to get the list of coordinates [...] in between braces so that i can do:

\draw[blue] plot [smooth] coordinates{ [...] };
8

EDIT New answer

You can use \xdef to collect the coordinates in to a variable as follows:

\def\pts{}
\foreach \x in {0,...,100} { \xdef\pts{\pts (sum-\x)}; }

In this way \pts ends up holding (sum-0) (sum-1) ... (sum-100) which is just right for the coordinates argument of plot.

Sample output

\documentclass{article}

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

\begin{document}

\begin{tikzpicture}
% red line
\path[draw,red,name path=one] plot [smooth] coordinates {(0,0) (7.5,0) (8,6) (8.5,0) (10,0)};
% function
\path[draw,blue,name path=three]
plot[domain=0:10,samples=100,smooth] (\x,{0.5*rand+2});
red line + function
\foreach \c in {0,...,100} {
  \pgfmathsetmacro{\x}{\c/10}
  \path[name path=line] (\x,-1) -- (\x,4);
  \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);
}
\def\pts{}
\foreach \x in {0,...,100} { \xdef\pts{\pts (sum-\x)}; }
\draw[black] plot[smooth] coordinates {\pts};
\end{tikzpicture}

\end{document}

Note that I have change the range on the named path line so that diagram does not fill too much more than necessary and stays on page 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.