7

I have a hobby curve that has regions above and below the horizontal axis. I would like to illustrate the difference between net and total area by being able to reflect the portion below the horizontal axis in the horizontal axis, i.e., take the absolute value of the hobby curve. Here is my code:

\documentclass{article}
\usepackage{pgf,tikz,amsmath,pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{arrows,hobby}
\begin{document}
    \pgfdeclarelayer{pre main}
    \begin{tikzpicture}[scale=1.0,>=latex, use Hobby shortcut]
        \pgfsetlayers{pre main,main}
        \draw[name path=f,-] (0,3) .. (3,0) .. (6,-2);
        \draw[->,thick] (-1.25,0) -- (7,0) node[above] {\footnotesize $t$};
        \draw[->,thick] (-1,-3) -- (-1,4) node[below right]{\footnotesize $v(t)$};
        \path[name path=xaxis] (0,0) -- (6,0);
        \node[below] at (0,0) {\footnotesize $t_0$};
        \node[above] at (6,0) {\footnotesize $t_1$};
        \node at (1,1) {\tiny $\text{Area }=A_1$};
        \node at (5,-0.75) {\tiny $\text{Area }=A_2$};
        \tikzfillbetween[of=f and xaxis,split]{lightgray}
    \end{tikzpicture}
\end{document}
  • The easiest way would be to draw it twice clipped to the upper region (and the second draw being reflected). The joins between the segments wouldn't be neat if you looked at them closely, but would that be a problem? – Loop Space Aug 14 '14 at 18:20
  • That wouldn't be a problem. Could you post your solution? – DJJerome Aug 14 '14 at 21:55
6

The idea I suggested in the comments was to use a clip to only draw the region above the x-axis and then to draw and fill the path twice, once reflected in the x-axis.

That doesn't work.

It works fine for the path but the fill ignores the clip and fills below the axis as well as above. (This is presumably a feature of the fillbetween library.)

So here's another solution which exploits the fact that one of your specified points is on the x-axis. This means that taking the absolute value of the coordinates specifying the path will result in the correct path (if the intersections of your path and the x-axis were not specified coordinates then this wouldn't work).

\documentclass{article}
%\url{http://tex.stackexchange.com/q/194436/86}
\usepackage{pgf,tikz,amsmath,pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{arrows,hobby}

\makeatletter
\tikzset{abs value/.code={%
    \tikz@addmode{%
      \pgfsyssoftpath@getcurrentpath\tikz@absvalue@tmppath%
      \let\tikz@absvalue@tmppathA=\pgfutil@empty%
      \expandafter\tikz@absycoord\tikz@absvalue@tmppath\pgf@stop
      \pgfsyssoftpath@setcurrentpath\tikz@absvalue@tmppathA%
    }%
  }%
}
\def\tikz@absycoord#1#2#3#4{%
  \pgfmathparse{abs(#3)}%
  \expandafter\def\expandafter\tikz@absvalue@tmppathA\expandafter{\tikz@absvalue@tmppathA#1{#2}}%
  \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@absvalue@tmppathA\expandafter\expandafter\expandafter{\expandafter\tikz@absvalue@tmppathA\expandafter{\pgfmathresult   pt}}%
  \ifx#4\pgf@stop
  \let\@next=\pgfutil@gobble
  \else
  \let\@next=\tikz@absycoord
  \fi
  \@next#4%
}
\makeatother

\begin{document}
    \pgfdeclarelayer{pre main}
    \begin{tikzpicture}[scale=1.0,>=latex, use Hobby shortcut]
        \pgfsetlayers{pre main,main}
        \draw[name path=f,-] (0,3) .. (3,0) .. (6,-2);
        \draw[->,thick] (-1.25,0) -- (7,0) node[above] {\footnotesize $t$};
        \draw[->,thick] (-1,-3) -- (-1,4) node[below right]{\footnotesize $v(t)$};
        \path[name path=xaxis] (0,0) -- (6,0);
        \node[below] at (0,0) {\footnotesize $t_0$};
        \node[above] at (6,0) {\footnotesize $t_1$};
        \node at (1,1) {\tiny $\text{Area }=A_1$};
        \node at (5,-0.75) {\tiny $\text{Area }=A_2$};
        \tikzfillbetween[of=f and xaxis,split]{lightgray}
\draw[abs value,magenta,ultra thick,name path=f-above,-] (0,3) .. (3,0) .. (6,-2);
\tikzfillbetween[of=f-above and xaxis,split]{cyan}
    \end{tikzpicture}
\end{document}

Absolute valued path

  • This is a very interesting approach and it yields a solution. The constraint that the path has a specified x-intercept isn't much of a constraint in what I'm doing. Very nice. – DJJerome Aug 14 '14 at 23:39

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.