Crossing paths in TikZ without intersection

The basic idea is to use a double line, with a white border. The problem is the default way tikz draws this is to draw the path completely a first time (the white border) and then completely a second time. The way around this is to use a decoration, more specifically the "show path construction" decoration. This decorates the path piecewise.

The code is

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}  

\tikzstyle{bridge path}=[rounded corners=10pt,decorate,decoration={show path construction,
lineto code={
    \draw [white,line width = 4pt,double=red,double distance=4pt]
            (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);},
curveto code={
      \draw [red,line width = 4pt] (\tikzinputsegmentfirst) .. controls
        (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
        ..(\tikzinputsegmentlast);
            }}]  

\begin{document}

\begin{tikzpicture} 

\draw[bridge path] 
    (0,2.5) -- (1.5,2.5) -- (1.5,0.5) -- (0.5,0.5)-- (0.5,1.5) -- (3,1.5);

\draw (0,0) grid[step=1] (3,3);

\end{tikzpicture}


\end{document}

The result is

enter image description here

Comment on Josef's comment that the code does not survive the integration into his style. From what I can tell, the problem occurs when one of the coordinates of the specified path is also one of the intersection points. I don't know why this occurs. As a quick fix, I would change the way the path is specified in Josef's edit: I would not say move left/right/up/down, but rather the give the coordinates of where the path curves (the corners). Maybe Andrew's code works better.


I was going to say that this is just what the knots TikZ library is for, except that in testing it then I found that it didn't cope well with rounded corners. Fortunately, it was a quick fix (which is now on CTAN).

\documentclass[border=5pt]{standalone}
%\url{http://tex.stackexchange.com/q/115923/86}
\usepackage{tikz}
\usetikzlibrary{knots}  

\begin{document}

\begin{tikzpicture} 
\begin{knot}[
  consider self intersections=true,
  flip crossing=1,
]
\strand[rounded corners=10pt,line width=4pt,red]
    (0,2.5) -- (1.5,2.5) -- (1.5,0.5) -- (0.5,0.5)-- (0.5,1.5) -- (3,1.5);
\end{knot}
\draw (0,0) grid[step=1] (3,3);
\end{tikzpicture}
\end{document}

The underlying mechanism is similar to that in Frédéric's answer: to use the double option to draw the overlying line. However, rather than drawing the path in segments what this method does is to redraw the upper part in the vicinity of the intersection. This means that the rest of the path is drawn "whole" and so has no artefacts on the joins.

self intersecting lines