To make an animation (here), I used a found code (here: @Jake answers). It works but not for all increments. It works when increments are 0.02 to 0.02, but not when increments are 0.04 to 0.04, 0.05 to 0.05 or 0.1 to 0.1.
Here with an increment of 0.04, the animation does not make the complete turn:
Here, with an increment of 0.02, the animation makes the complete turn but the path is not closed:
First problem:
- Why doesn't it work with any increment?
- How do I make the decoration work for any increment?
Edition: By replacing \foreach
by \multido
the increments do not pose any more problems, the complete turn is carried out.
\multido{\npos=0.00+0.05}{21}{
%\multido{\npos=0.00+0.04}{26}{
%\multido{\npos=0.00+0.1}{11}{
%\foreach \npos in {0,.1,...,1}{%
\begin{tikzpicture}[scale=1]
\fill[green!40](0,0) rectangle (4,4);
\draw[start segment=\npos,blue,ultra thick] (0,0) -- (4,0) -- (4,4) -- (0,4) --cycle ;
\end{tikzpicture}
}
Edition 2: Ruixi Zhang's solution allows you to go around the entire path using a \foreach
loop.
%\foreach \x [evaluate =\x as \npos using \x/50] in {0,1,...,50}{% increment of 0.02
%\foreach \x [evaluate =\x as \npos using \x/25] in {0,1,...,25}{% increment of 0.04
\foreach \x [evaluate =\x as \npos using \x/10] in {0,1,...,10}{% increment of 0.1
\begin{tikzpicture}[scale=1]
\fill[green!40](0,0) rectangle (4,4);
\draw[start segment=\npos,blue,ultra thick] (0,0) -- (4,0) -- (4,4) -- (0,4) --cycle ;
\end{tikzpicture}
Second problem:
How to close the path?
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{decorations}
\pgfdeclaremetadecoration{start}{initial}{
\state{initial}[width={\pgfmetadecoratedpathlength*\pgfdecorationsegmentlength},next state=final]{
\decoration{lineto}
}
\state{final}{}
}
\tikzset{start segment/.style={decoration={start,raise=2mm,segment length=#1},decorate}}
\begin{document}
\foreach \rpos in {0,.02,.04,...,1}{%
\begin{tikzpicture}[scale=1]
\fill[green!40](0,0) rectangle (4,4);
\draw[start segment=\rpos,blue,ultra thick] (0,0) -- (4,0) -- (4,4) -- (0,4) --cycle ;
\end{tikzpicture}
}
\end{document}
- How do I close it completely?