3

In my MWE I have a simple path (circle) and I overlay the path - using a tikz postaction - with the same path but with another line with and another color.

The reference (thanks @MarkWibrow) of the tricky code snippets is in the code. But I do not think that this is relevant for this problem.

The overlay starts at 3 o'clock and goes from there counter-clockwise.

Question(s)

  1. How can I influence the direction (clockwise or counter-clockwise)
  2. How can I influence the starting point/origin/beginning of the path (here it's apparently 3 o'clock)?

Code

\documentclass[tikz,border=2mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations}

% taken from https://tex.stackexchange.com/questions/247742
% user https://tex.stackexchange.com/users/23215/mark-wibrow
\makeatletter
\tikzset{%
  get path length/.code={%
    \tikz@addoption{%
      \pgfgetpath\tikz@tmppath%
      \pgfprocessround\tikz@tmppath\tikz@tmppath%
      \pgf@decorate@parsesoftpath\tikz@tmppath\tikz@discard%
      \global\let#1=\pgf@decorate@totalpathlength%
    }%
  }
}
\makeatother

% taken from https://tex.stackexchange.com/questions/347336
% user https://tex.stackexchange.com/users/23215/mark-wibrow
\tikzset{myOwnStyle/.style={
  get path length=\pathlength,
  draw=black,
  line width=1mm,
  postaction={
    draw=red,
    line width=2mm,
    dash pattern= on \pathlength/100*#1 off \pathlength/100*(100-#1),
  }
}}


\begin{document}

\foreach \n in {0,...,100}{
\begin{tikzpicture}
    \draw[myOwnStyle=\n] (0,0) circle [radius = 10mm] node {\n\,\%};  
\end{tikzpicture} 
}

\end{document}

Output

enter image description here

If somebody is interested, I made the animated GIF by two steps:

  1. Convert multipage PDF to separate GIF files using this service.
  2. Converting the separate GIFs to an animated GIF using this or this (was much faster) service.

I just googled for these kind of service - I did not make any further reseach.

Update / Edit

After the very helpful answer of @Torbjørn T. it was clear that a circle was not a wise choice:) - here's a square.

\foreach \n in {0,1,...,100}{
\begin{tikzpicture}[x=1.5mm,y=1.5mm]]
    \draw[myOwnStyle=\n] (0,0) -- (10,0) -- (10,10) -- (0,10) -- cycle;
    \node at (5,5) {\n\,\%};  
\end{tikzpicture} 
}

enter image description here

  • 1
    To make animated GIF from PDF, you may use the convert utility (provided by ImageMagick): convert -delay 1 -density 150 doc.pdf doc.gif. – Paul Gaborit Jan 8 '17 at 1:46
  • @PaulGaborit Thanks - maybe that is more convenient. The online tools are great but it's "a lot" of manual steps. [...] 2 min later: Well, it did not work because of the transparency I guess. It looks like the different frames are "stacked" and then you couldn't read the numbers anymore. But I am sure that I will be able to google that. – Dr. Manuel Kuehner Jan 8 '17 at 1:47
  • 1
    You may use the fit and backgrounds libraries and append this code to the end of tikzpicture: \begin{pgfonlayer}{background} \node[fit=(current bounding box),fill=white]{}; \end{pgfonlayer}. – Paul Gaborit Jan 8 '17 at 1:56
  • @PaulGaborit Thanks again. I found another solution (stackoverflow.com/questions/14676119): -dispose previous did the trick. – Dr. Manuel Kuehner Jan 8 '17 at 1:58
  • 1
    With -dispose previous, the result is not anti-aliased... – Paul Gaborit Jan 8 '17 at 9:03
2

Here is a solution using dash phase (stored by the \myOwnStylephase macro) to define the starting point and with a second custom style (myOwnStyle revert) to change direction.

enter image description here

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations,fit,backgrounds}

% taken from http://tex.stackexchange.com/questions/247742
% user http://tex.stackexchange.com/users/23215/mark-wibrow
\makeatletter
\tikzset{%
  get path length/.code={%
    \tikz@addoption{%
      \pgfgetpath\tikz@tmppath%
      \pgfprocessround\tikz@tmppath\tikz@tmppath%
      \pgf@decorate@parsesoftpath\tikz@tmppath\tikz@discard%
      \global\let#1=\pgf@decorate@totalpathlength%
    }%
  }
}
\makeatother

% taken from http://tex.stackexchange.com/questions/347336
% user http://tex.stackexchange.com/users/23215/mark-wibrow
\tikzset{
  myOwnStyle phase/.store in=\myOwnStylephase,
  myOwnStyle phase=0,
  myOwnStyle/.style={
    get path length=\pathlength,
    draw=black,
    line width=1mm,
    postaction={
      draw=red,
      line width=2mm,
      dash pattern= on \pathlength/100*#1 off \pathlength/100*(100-#1),
      dash phase=-\pathlength/100*\myOwnStylephase,
    }
  },
  myOwnStyle revert/.style={
    get path length=\pathlength,
    draw=black,
    line width=1mm,
    postaction={
      draw=red,
      line width=2mm,
      dash pattern=on 0pt off \pathlength/100*(100-#1) on \pathlength/100*#1 off 0pt,
      dash phase=\pathlength/100*\myOwnStylephase,
    }
  },
}


\begin{document}
\foreach \n in {0,0,0,0,4,...,100,100,100,100}{
  \begin{tikzpicture}%[line cap=round]
    \draw[myOwnStyle=\n]
    (0,0) ++(90:10mm) arc(90:-270:10mm) (0,0) node {\n\,\%};

    \draw[myOwnStyle=\n,myOwnStyle phase=25]
    (3,0) ++(90:10mm) arc(90:-270:10mm) (3,0) node {\n\,\%};

    \draw[myOwnStyle revert=\n]
    (0,-3) ++(90:10mm) arc(90:-270:10mm) (0,-3) node {\n\,\%};

    \draw[myOwnStyle revert=\n,myOwnStyle phase=25]
    (3,-3) ++(90:10mm) arc(90:-270:10mm) (3,-3) node {\n\,\%};

    \draw[myOwnStyle=\n]
    (-1,-7) -- ++(2,0) -- ++(0,2) -- ++(-2,0) -- cycle (0,-6) node {\n\,\%};

    \draw[myOwnStyle=\n,myOwnStyle phase=12.5]
    (2,-7) -- ++(2,0) -- ++(0,2) -- ++(-2,0) -- cycle (3,-6) node {\n\,\%};

    \begin{pgfonlayer}{background}
      \node[fit=(current bounding box),inner sep=2mm,fill=white]{};
    \end{pgfonlayer}
  \end{tikzpicture} 
}
\end{document}
  • Thanks - and the [line cap=round] is also helpful! And I like {0,0,0,0,4,...,100,100,100,100} because it makes an pleasant pause in the animation. Is it correct that revert and phase are not possible together (option with two arguments) or is there too much of logic? – Dr. Manuel Kuehner Jan 8 '17 at 13:04
  • 1
    To use revert and phase together, you may use \draw[myOwnStyle revert=\n,myOwnStyle phase=12.5] .... Or do you want a different syntax? – Paul Gaborit Jan 8 '17 at 13:43
  • I didn't understand that this is possible (combining the styles) since every of the three styles (myOwnStyle, myOwnStyle revert, and myOwnStyle phase) hat a postaction and so on. It's obvious that I have to try harder to understand the code (it's complicated to me but that just means that I do not have a programming or math background}) - again, thanks a lot! – Dr. Manuel Kuehner Jan 8 '17 at 15:41
  • Is this correct? myOwnStyle phase ist "just" an option/parameter whereas myOwnStyle is a style that uses the option/parameter myOwnStyle phase. And myOwnStyle revert is a style again that also can use the option/parameter myOwnStyle phase. – Dr. Manuel Kuehner Jan 8 '17 at 15:46
  • 1
    @Dr.ManuelKuehner Correct. – Paul Gaborit Jan 8 '17 at 15:54
3

For the specific case of a circle, you can just change flip it around with xscale=-1 to change the direction, and rotate it to change the starting point, e.g. \draw[myOwnStyle=\n,rotate=30,xscale=-1]. This does not apply to sorts of shapes, of course.

\documentclass[tikz,border=2mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations}

% taken from http://tex.stackexchange.com/questions/247742
% user http://tex.stackexchange.com/users/23215/mark-wibrow
\makeatletter
\tikzset{%
  get path length/.code={%
    \tikz@addoption{%
      \pgfgetpath\tikz@tmppath%
      \pgfprocessround\tikz@tmppath\tikz@tmppath%
      \pgf@decorate@parsesoftpath\tikz@tmppath\tikz@discard%
      \global\let#1=\pgf@decorate@totalpathlength%
    }%
  }
}
\makeatother

% taken from http://tex.stackexchange.com/questions/347336
% user http://tex.stackexchange.com/users/23215/mark-wibrow
\tikzset{myOwnStyle/.style={
  get path length=\pathlength,
  draw=black,
  line width=1mm,
  postaction={
    draw=red,
    line width=2mm,
    dash pattern= on \pathlength/100*#1 off \pathlength/100*(100-#1),
  }
}}


\begin{document}

\foreach \n in {0,...,100}{
\begin{tikzpicture}
    \draw[myOwnStyle=\n,rotate=30,xscale=-1] (0,0) circle [radius = 10mm] node {\n\,\%};  
\end{tikzpicture} 
}

\end{document}
  • Thanks. The problem is that this only works for objects that are symmetrical to rotation. But in this case it is a good idea! – Dr. Manuel Kuehner Jan 7 '17 at 22:24
  • @Dr.ManuelKuehner Yes, that is true. – Torbjørn T. Jan 7 '17 at 22:25
  • I updated the question with a better shape - the circle was a stupid choice, sorry. – Dr. Manuel Kuehner Jan 8 '17 at 1:39

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.