Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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

enter image description here

Code

\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}
share|improve this answer
    
Great! It's slow as it treats the general case with a huge number of intersections to be computed. I'd like to have a specific \addpiecewiselinear macro with just a list of paths as a mandatory argument and the "min/max" coordinates as optional arguments (clipping the range where the computations take place if any) –  green diod Sep 19 '12 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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