6

How to reverse the order of the nodes in an arbitrary path in TikZ? I want to fill a shape bounded by shifted versions of the same path, constructed in TikZ using Bezier curves (not plotting a specific mathematical function as in Reversing a path in TikZ ).

The only way I've figured out to do this is to use define a set of points using LaTeX \def, and then have a \foreach \fill over this set of points, but repeated a few times this is very clumsy and verbose compared to simply constructing a named path (or even \def'd path description) once, and then shifting it and reversing the order it is traversed in fill operations etc. It would also be nice to be able to select a sub-path easily... all in all something like in this hand-drawn sketch, but with the upper and lower halves delimited by Γ filled with different colours or patterns.

sketch

In MetaPost I recall these operations being a relative breeze (it's been a while since I used it), but in other ways it is clumsier than TikZ.

6

Not sure if this is quite what you want, but it can reverse a path.

Firstly a method using pic to shift the path...

\documentclass[tikz,border=10]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter
\tikzset{%
  reverse path/.style={
    decoration={show path construction,
      reverse path,
      moveto code={\pgfpathmoveto{\pgf@decorate@inputsegment@first}},
      lineto code={\pgfpathlineto{\pgf@decorate@inputsegment@last}},
      curveto code={\pgfpathcurveto{\pgf@decorate@inputsegment@supporta}%
        {\pgf@decorate@inputsegment@supportb}{\pgf@decorate@inputsegment@last}},
      closepath code={\pgfpathclose}
    },
    decorate
  }
}
\begin{document}
\begin{tikzpicture}[very thick,
bezier path/.pic={
  \draw [#1] (-1,1)
    .. controls ++( 45:1/2) and ++(225:1/2) .. ( 1, 1)
    .. controls ++(315:1/2) and ++(135:1/2) .. ( 1,-1)
    .. controls ++(225:1/2) and ++( 45:1/2) .. (-1,-1);
}]

\draw ( 90:2) pic {bezier path={red}};
\draw (210:2) pic {bezier path={green, ->}};
\draw (330:2) pic {bezier path={blue,  ->, reverse path}};
\end{tikzpicture}
\end{document}

enter image description here

...and the following using the insert path key will produce the same results:

\documentclass[tikz,border=10]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter
\tikzset{%
  reverse path/.style={
    decoration={show path construction,
      reverse path,
      moveto code={\pgfpathmoveto{\pgf@decorate@inputsegment@first}},
      lineto code={\pgfpathlineto{\pgf@decorate@inputsegment@last}},
      curveto code={\pgfpathcurveto{\pgf@decorate@inputsegment@supporta}%
        {\pgf@decorate@inputsegment@supportb}{\pgf@decorate@inputsegment@last}},
      closepath code={\pgfpathclose}
    },
    decorate
  }
}
\begin{document}
\begin{tikzpicture}[very thick,
bezier path/.style={insert path={
    [shift={#1}] (-1,1)
    .. controls ++( 45:1/2) and ++(225:1/2) .. ( 1, 1)
    .. controls ++(315:1/2) and ++(135:1/2) .. ( 1,-1)
    .. controls ++(225:1/2) and ++( 45:1/2) .. (-1,-1)
}}]

\draw [bezier path={( 90:2)}, red];
\draw [bezier path={(210:2)}, green, ->];
\draw [bezier path={(330:2)}, blue,  ->, reverse path];
\end{tikzpicture}
\end{document}
  • Very nice! In a strange way if we reverse circle with -> it gets the arrow, but not a rectangle. Why ? A small suggestion: if we split reverse path in reverse and reverse path/.style={reverse,decorate}, we will be able to reverse only part of the path using decorate{ ... }. And this could be really useful for filling (finer control than even odd rule). Typo: \end{document} is missing in the first code. – Kpym Jan 18 '15 at 20:03

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.