4

I would like to decorate a circle in a way such that the starting and the ending points of the decoration coincides in a nice way. Of course, they always coincide, but I would like to have the element of the decoration repeated an integer number of times. Is there an easy way to achieve it? Of course, adjusting the amplitude and/or the segment length of the decoration can help, but it needs a manual fine tuning, which I would like to avoid.

I looked in the pgf manual, in particular chapter 24, but I did not find my answer. As side remark, section 24.3 contains part of my code with the same "problem" and nothing in section 24.4 (Adjusting decorations) or 48.2 (Path Morphing Decorations) was for me useful.

enter image description here

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
    \begin{tikzpicture}
        \draw[decorate, decoration=zigzag] (0,0) circle (1cm);
        \draw[decorate, decoration=bumps]   (3,0) circle (1cm);
        \draw[red, thick] (1,0) circle (3mm) (4,0) circle (3mm); % <- Just to stress the problem
    \end{tikzpicture}
\end{document}
Axel Krypton
  • 1,083

1 Answers1

5

One ad hoc proposal is to set the segment length to pi mm (for circles of radius 1cm).

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
    \begin{tikzpicture}
        \draw[decorate, decoration={zigzag,segment length=3.14mm}] (0,0) circle (1cm);
        \draw[decorate, decoration={bumps,segment length=3.14mm}]   (3,0) circle (1cm);
        \draw[red, thick] (1,0) circle (3mm) (4,0) circle (3mm); % <- Just to stress the problem
    \end{tikzpicture}
\end{document}

enter image description here

UPDATE: A more complete solution for zigzag.

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{decorations,decorations.pathmorphing}
\pgfkeys{/tikz/.cd,
    pattern repetition/.store in=\PatternRepetition,
    pattern repetition=20
}

\pgfdeclaremetadecoration{closing zigzag}{initial}{%
 \state{initial}[width=0pt, next state=zigzag] {
    \pgfmathdivide{\pgfmetadecoratedpathlength}{\PatternRepetition}
    \let\pgfmetadecorationsegmentlength\pgfmathresult
    \pgfset{/pgf/decoration/segment length=\pgfmathresult pt}
  }
\state{zigzag}[width=\pgfmetadecorationsegmentlength, next state=zigzag] {
\decoration{zigzag}
}
\state{final}
  {
    \pgfpathlineto{\pgfpointdecoratedpathlast}
  }
}

% does not quite work yet 
\pgfdeclaremetadecoration{closing bumps}{initial}{%
 \state{initial}[width=0pt, next state=bumps] {
    \pgfmathdivide{\pgfmetadecoratedpathlength}{\PatternRepetition}
    \pgfset{/pgf/decoration/segment length=\pgfmathresult pt}
    \pgfmathdivide{\pgfmetadecoratedpathlength}{\PatternRepetition}
    \let\pgfmetadecorationsegmentlength\pgfmathresult
  }
\state{bumps}[width=\pgfmetadecorationsegmentlength, next state=bumps] {
\decoration{bumps}
}
\state{final}
  {
    \pgfpathlineto{\pgfpointdecoratedpathlast}
  }
}

\newlength{\totallength}
\begin{document}
    \begin{tikzpicture}
    \draw[decorate, decoration={closing zigzag}] (0,0) circle (1cm);
    \tikzset{pattern repetition=55}
    \draw[decorate, decoration={closing zigzag}]   (3,0) circle (1cm and 2cm);
    \draw[red, thick] (1,0) circle (3mm) (4,0) circle (3mm); % <- Just to stress the problem
    \end{tikzpicture}
\end{document}

enter image description here

  • Well, yes, ad hoc solution, but easily generalizable. Fair enough! I would have expected tikz to provide something to somehow make this available through a kind of interface or key, though. – Axel Krypton Apr 09 '18 at 15:26
  • @AxelKrypton Well, you'll always have to know the circumference of the cyclic shape you'd like to decorate, right? In the case of a circle, that's of course simple, but what if some one draws some complicated parametric plot? Or is your idea to get the decoratedpathlength and set the segment length to be an integer fraction thereof? That would be a nice thing, which I'll be happy to do a bit later. (BTW, +1) –  Apr 09 '18 at 15:32
  • You are absolute correct and I was also thinking that it should be possible to play with the decoratedpathlength in order to guarantee an integer repetition of the decoration item. This is what I had in mind when I wrote that I would have expected tikz to somehow provide a feature like that (but probably there are good reasons which go beyond my knowledge if such a feature is missing). If you feel like later to provide an example to how to generalize the code to a more complicate case, I will be happy to read and use it. And, of course +1, :) – Axel Krypton Apr 09 '18 at 15:39
  • @AxelKrypton I'll do it later, if I succeed. Just adopting the trick from your nice answer doesn't work in my first attempt, and now I'll have to do other things. Of course it is perfectly fine if you do it yourself, after all I'd only use a trick you invented. ;-) –  Apr 09 '18 at 15:44