[Tex/LaTex] Tikz: How to indicate/display overlapping lines

colortikz-pgf

I have the following simple picture:

enter image description here

Is there a way to better illustrate the small overlapping interall of LS and LD? I know I could define this area by another line for example and use different dashed version of red and green. But is there a best-practice for all cases of overlapping lines which for example shifts of the lines or splits it automatic into red and green sections? Or at least a color mixture option? Maybe one can use for overlapping areas transparent lines? But this still leaves the question of automatic detection of overlapping issues.

Thank you

\begin{center}
\begin{tikzpicture}[ultra thick]
    % Axis and coordinates
    \coordinate (y) at (0,5);
    \coordinate (o) at (0,0);
    \coordinate (x) at (7,0);

    % Axis labels, basic line and curve
    \draw[<->,line width=1.5pt] (y) node[left, label={[align=left] $w$}] -- (o) -- (x) node[below]{$L^S, L^D$};

        \node[left] at (0,0) {0};
        \node[left] at (0,1) {z};
        \node[left] at (0,4) {$\overline{w}$};

        % dashed line and curve
        \draw[red] (0,1) -- (1.5,1) node[right, red] {$L^S$};
        \draw[red] (1.5,1) -- (3,4);
        \draw[red] (3,4) -- (6,4);
        \draw[gray, dashed, thin]  (5.5,0) node[below, black] {$\overline{L}$} -- (5.5,4) ;

        \draw[ForestGreen] (0,4) -- (3.5,4);
        \draw[ForestGreen] (3.5,4) -- (5,1) node[left, ForestGreen] {$L^D$};

\end{tikzpicture}
\end{center}

Best Answer

Updated Answer

Inspired by "Z-level" in TikZ

\pgfdeclarelayer{front}
\pgfdeclarelayer{back}
\pgfsetlayers{back,main,front}
\tikzset{% https://tex.stackexchange.com/questions/20425/z-level-in-tikz
    on layer/.code={\pgfonlayer{#1}\tikzset{every picture}\begingroup\aftergroup\endpgfonlayer\aftergroup\endgroup},
    also in front/.style 2 args={#1,postaction={on layer=front,draw,#1,#2}},
    also behind/.style 2 args={#1,postaction={on layer=back,draw,#1,#2}},
    every picture/.style={line width=6},
}

\tikz{
    \draw[also in front={red}{opacity=.5}](-2,0)--(1,0)--(2,1);
    \draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
    \draw[also in front={red}{dashed}](-2,0)--(1,0)--(2,1);
    \draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
    \draw[also in front={red}{line width=.5\pgflinewidth}](-2,0)--(1,0)--(2,1);
    \draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
    \draw[also in front={red}{line width=.5\pgflinewidth}](-2,0)--(1,0)--(2,1);
    \draw[also behind={}{green},also in front={green,dashed}{line width=.5\pgflinewidth,dash phase=3pt}](-2,-1)--(-1,0)--(2,0);
}

\tikz{
    \let\tikzerror\relax
    \draw[also in front={red}{clip,draw=none,postaction={draw}}](-2,0)--(1,0)--(2,1);
    \draw[green](-2,-1)--(-1,0)--(2,0);
}

\tikz{
    \let\tikzerror\relax
    \path(-2,-1)(2,1);
    \draw[also behind={}{red},preaction={draw=red,dashed},also in front={clip,draw=none}{postaction={draw=red,dashed,dash phase=3pt}}](-2,0)--(1,0)--(2,1);
    \draw[also behind={green}{}](-2,-1)--(-1,0)--(2,0);
}

and a crazy one. (basically copy the definition of snake)

\pgfdeclaredecoration{DNA}{initial}
{
  \state{initial}[switch if less than=+.625\pgfdecorationsegmentlength to final,
                  width=+.3125\pgfdecorationsegmentlength,
                  next state=down]
  { 
    \pgfpathcurveto
    {\pgfqpoint{.125\pgfdecorationsegmentlength}{0pt}}
    {\pgfqpoint{.1875\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
    {\pgfqpoint{.3125\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
  }
  \state{down}[switch if less than=+.8125\pgfdecorationsegmentlength to end down,
               width=+.5\pgfdecorationsegmentlength,
               next state=up]
  {
    \def\pgfpathcurveto##1##2##3{\pgfpathmoveto{##3}}
    \pgfpathcosine{\pgfqpoint{.25\pgfdecorationsegmentlength}{-1\pgfdecorationsegmentamplitude}}
    \pgfpathsine{\pgfqpoint{.25\pgfdecorationsegmentlength}{-1\pgfdecorationsegmentamplitude}}
  }               
  \state{up}[switch if less than=+.8125\pgfdecorationsegmentlength to end up,
             width=+.5\pgfdecorationsegmentlength,
             next state=down]
  {
    \pgfpathcosine{\pgfqpoint{.25\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
    \pgfpathsine{\pgfqpoint{.25\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
  }               
  \state{end down}[width=+.3125\pgfdecorationsegmentlength,
                   next state=final]
  {
    \pgfpathcurveto
    {\pgfqpoint{.125\pgfdecorationsegmentlength}{\pgfdecorationsegmentamplitude}}
    {\pgfqpoint{.1875\pgfdecorationsegmentlength}{0pt}}
    {\pgfqpoint{.3125\pgfdecorationsegmentlength}{0pt}}
  }  
  \state{end up}[width=+.3125\pgfdecorationsegmentlength,
                 next state=final]
  {
    \pgfpathcurveto
    {\pgfqpoint{.125\pgfdecorationsegmentlength}{-\pgfdecorationsegmentamplitude}}
    {\pgfqpoint{.1875\pgfdecorationsegmentlength}{0pt}}
    {\pgfqpoint{.3125\pgfdecorationsegmentlength}{0pt}}
  }  
  \state{final}
  {
    \pgfpathlineto{\pgfpointdecoratedpathlast}
  }
}
\tikz[line width=2]{
    \draw[red,decorate,decoration={snake}](-2,0)--(1,0)--(2,1);
    \draw[green,decorate,decoration={snake}](-2,-1)--(-1,0)--(2,0);
    \draw[red,decorate,decoration={DNA}](-2,0)--(1,0)--(2,1);
}

Old answer

Just before this question got closed, here is a TikZ-specific approach inspired by this:

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\pgfdeclaredecoration{penciline}{initial}{
    \state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,auto corner on length=1mm,]{
        \pgfpathcurveto%
        {% From
            \pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}
                            {\pgfdecorationsegmentamplitude}
        }
        {%  Control 1
        \pgfmathrand
        \pgfpointadd{\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{0pt}}
                        {\pgfqpoint{-\pgfdecorationsegmentaspect\pgfdecoratedinputsegmentremainingdistance}%
                                        {\pgfmathresult\pgfdecorationsegmentamplitude}
                        }
        }
        {%TO 
        \pgfpointadd{\pgfpointdecoratedinputsegmentlast}{\pgfpoint{1pt}{1pt}}
        }
    }
    \state{final}{}
}
\begin{tikzpicture}[ultra thick,decoration=penciline]
    % Axis and coordinates
    \coordinate (y) at (0,5);
    \coordinate (o) at (0,0);
    \coordinate (x) at (7,0);

    % Axis labels, basic line and curve
    \draw[<->,line width=1.5pt] (y) node[left, label={[align=left] $w$}]{} -- (o) -- (x) node[below]{$L^S, L^D$};
        \node[left] at (0,0) {0};
        \node[left] at (0,1) {z};
        \node[left] at (0,4) {$\overline{w}$};
        % dashed line and curve
        \draw[red,decorate] (0,1) -- (1.5,1)node[right, red] {$L^S$}(1.5,1) -- (3,4) -- (6,4);
        \draw[gray, dashed, thin]  (5.5,0) node[below, black] {$\overline{L}$} -- (5.5,4) ;
        \draw[green,decorate] (0,4) -- (3.5,4) -- (5,1) node[left, green] {$L^D$};
\end{tikzpicture}
\end{document}
Related Question