5

I try to create pgf decoration that will be able to tell in each segment whether the its start is a sharp corner on the input path, but I can't figure how to do this.

Take for example the following MWE. It decorates path with curves. When it reaches a corner and there is no room for another curve, it just draws a simple line and draws a curve after the corner. How can I make it to draw another simple line after the corner and only than to start the curves?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations}


\pgfdeclaredecoration{examp}{A}
{   
    \state{A}[width=4mm,auto corner on length=4mm]
    {
        \pgfpathcurveto{\pgfpoint{0mm}{0}}{\pgfpoint{2mm}{5mm}}{\pgfpoint{4mm}{0}};
    }
    %
    \state{final}
    {
    \pgfpathlineto{\pgfpointdecoratedpathlast};
    }
}


\begin{document}
\begin{tikzpicture}

\draw[decoration={examp,path has corners=true},decorate] (0,0) -- (0,3) -- (3,3);

\end{tikzpicture}
\end{document}
  • You can create a state B which always starts with a straight line in each segment. And run A once and B all the way afterwards. But there are always strange cases that cannot be handled with a one-size-fits-for-all solution. – percusse Dec 17 '14 at 0:44
  • @percusse I see you left a comment while I was looking this up. I'm not sure if my answer is exactly what you had in mind or not. If it is, let me know that I should delete it so that you can answer. (Well, you can answer anyway, of course, but you know what I mean.) – cfr Dec 17 '14 at 1:12
  • @cfr As you know very well we don't compete here. It's a nice answer so no reason to delete. And I think I meant that way. Can you include some arcs and -| on the same path to see whether it goes through ? – percusse Dec 17 '14 at 1:13
  • @percusse Indeed. That's why I said it. I've extended the example. I'm not sure that is quite what it should do in the case of an arc but it seems to do something reasonable for |- and the curveto examples. I guess TiKZ splits the arc into distinct input segments... – cfr Dec 17 '14 at 1:25
5

Is this what you want?

[I see that while I was looking this up, percusse left a comment. I'm not sure if this is exactly what percusse had in mind or not. If so, let me know that I should delete this.]

sharp corner

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations}
\newlength{\prelength}

\pgfdeclaredecoration{examp}{A}
{
    \state{A}
      [
        width=4mm,
        switch if input segment less than={4mm to B},
      ]
    {
        \pgfpathcurveto{\pgfpoint{0mm}{0}}{\pgfpoint{2mm}{5mm}}{\pgfpoint{4mm}{0}};
    }
    \state{B}
      [
        width=\pgfdecoratedinputsegmentremainingdistance,
        repeat state=0,
        next state=C,
        persistent precomputation={%
          \setlength{\prelength}{\pgfdecoratedinputsegmentremainingdistance}
        },
      ]
    {
      \pgflineto{\pgfpointdecoratedinputsegmentlast};
    }
    \state{C}
      [
        width=\prelength,
        repeat state=0,
        next state=A,
      ]
    {
      \pgflineto{\pgfpoint{\prelength}{0}};
    }
    \state{final}
    {
    \pgfpathlineto{\pgfpointdecoratedpathlast};
    }
}


\begin{document}
\begin{tikzpicture}

\draw[decoration={examp},decorate] (0,0) -- (0,3) -- (3,3) |-  cycle;
\draw[decoration={examp},decorate, xshift=40mm] (0,0) -- (0,3) -- (3,3) arc (90:-90:1.5) [out=135, in=45] to cycle;

\end{tikzpicture}
\end{document}

sharp corners with |- and arcs

  • Yes! This is exactly what I was looking for! Thank you! Ido – Ido H. Dec 18 '14 at 11:58

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.