3

I am just learning tikz with still image. This is my first trying to create animation.

I have this code:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{0}{0}
\definecolor{ocre}{RGB}{243,102,25}
\colorlet{mild}{ocre!50}
\begin{document}
    \begin{tikzpicture}[y={(0,-0.385cm)},z={(0,1cm)},scale=1.25]
        \fill[rotate around z=10, mild] (0,0) -- (0,0,2) -- (1,0,2) -- (1,0,0) -- cycle;
        \draw (1,0) arc (0:180:1);
        \draw[dashed] (-1,0) arc (180:360:1);
        \draw (0,0,2) circle (1);
        \draw[rotate around z=40, dashed] (1,0,0) -- (0,0) -- (0,0,2);
        \draw[rotate around z=40] (0,0,2) -- (1,0,2) -- (1,0,0);
        \draw (-1,0,0) -- (-1,0,2);
        \draw (1,0,0) -- (1,0,2);
        \draw[rotate around z=40, scale=0.8] (0,0,0.3) -- (0.3,0,0.3) -- (0.3,0,0);
        \begin{scope}[xshift=3cm]
            \tdplotsetrotatedcoords{0}{40}{0};
            \fill[mild,tdplot_rotated_coords] (0,0,0) arc (-90:90:1);
            \draw[tdplot_rotated_coords] (0,0,0) arc (-90:90:1);
            \tdplotsetrotatedcoords{0}{0}{0};
            \draw[tdplot_rotated_coords] (0,1,0) circle (1);
            \draw[dashed,tdplot_rotated_coords] (0,2,0) -- (0,0,0);
            \draw (1,0,1) arc (0:180:1);
            \draw[dashed] (-1,0,1) arc (180:360:1);
        \end{scope}
    \end{tikzpicture}
\end{document}

Notice that if I change the value of $z$ here:

    \fill[rotate around z=10, mild] (0,0) -- (0,0,2) -- (1,0,2) -- (1,0,0) -- cycle;

from z=20, to z=10, to z=0, z = -10, z = -20, etc. it will make the orange shade circling 360 degree. I want this animation. How to create it.

Including the orange shade of the sphere too, can we create the animation of the shade circling 360 degree?

1

AlexG
  • 54,894

2 Answers2

5

The tikzpicture can be parameterised by wrapping it in a command that takes one mandatory argument, the rotation angle about the z axis. Within the tikzpicture, the rotation angle to be parameterised is replaced with the command argument #1, i. e.:

\fill[rotate around z=#1, mild] (0,0) -- (0,0,2) -- (1,0,2) -- (1,0,0) -- cycle;

Then, the command is placed in the body of a loop making command, such as \multiframe of pkg animate, which passes a sequence of rotation angles.

enter image description here

Note that two ; where too much in the original implementation, leading to a Missing character: There is no ; in font nullfont! warning. Also, the code was slightly modified to allow for dashed hidden lines for rotation angles > 180°.

Complete code:

\documentclass[margin=1pt]{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{0}{0}
\definecolor{ocre}{RGB}{243,102,25}
\colorlet{mild}{ocre!50}

% parameterised tikzpicture with one parameter (rot angle about z) \NewDocumentCommand\parampicture{m}{% \begin{tikzpicture}[y={(0,-0.385cm)},z={(0,1cm)},scale=1.25] \fill[rotate around z=#1, mild] (0,0) -- (0,0,2) -- (1,0,2) -- (1,0,0) -- cycle; \draw (1,0) arc (0:180:1); \draw[dashed] (-1,0) arc (180:360:1); \draw (0,0,2) circle (1); \draw[rotate around z=#1, dashed] (1,0,0) -- (0,0) -- (0,0,2); \draw[rotate around z=#1] (0,0,2) -- (1,0,2); \draw[rotate around z=#1, \ifnum#1>180 dashed\fi] (1,0,2) -- (1,0,0); \draw (-1,0,0) -- (-1,0,2); \draw (1,0,0) -- (1,0,2); \draw[rotate around z=#1, scale=0.8] (0,0,0.3) -- (0.3,0,0.3) -- (0.3,0,0); \begin{scope}[xshift=3cm] \tdplotsetrotatedcoords{0}{#1}{0} \fill[mild,tdplot_rotated_coords] (0,0,0) arc (-90:90:1); \draw[tdplot_rotated_coords, \ifnum#1>180 dashed\fi] (0,0,0) arc (-90:90:1); \tdplotsetrotatedcoords{0}{0}{0} \draw[tdplot_rotated_coords] (0,1,0) circle (1); \draw[dashed,tdplot_rotated_coords] (0,2,0) -- (0,0,0); \draw (1,0,1) arc (0:180:1); \draw[dashed] (-1,0,1) arc (180:360:1); \end{scope} \end{tikzpicture}% }

\usepackage[export]{animate}

\begin{document} \begin{animateinline}[autoplay,loop]{25} \multiframe{360}{i=0+1}{ \parampicture{\i} } \end{animateinline} \end{document}

To get a PDF animation for Okular or Acrobat Reader, remove the export option from animate.

AlexG
  • 54,894
  • Wow, thanks so much – Freya the Goddess Jan 20 '23 at 05:10
  • I get errors: Package pgfkeys Error: I do not know the key '/tikz/##1>180 dashed' and I am going to ignore it. Perhaps you misspelled it. ...ot_rotated_coords, \ifnum#1>180 dashed\fi] – Freya the Goddess Jan 20 '23 at 05:19
  • this too: Package xkeyval Error:export' undefined in families anim@pkg'. \expandafter\@anim@setpkgkeys\@anim@pkgopts – Freya the Goddess Jan 20 '23 at 05:19
  • Illegal parameter number in definition of \pgfmath@expression. \draw[rotate around z=#1, scale=0.8] – Freya the Goddess Jan 20 '23 at 05:19
  • Did you copy and paste the code block below "Complete Code" unmodified into a text file something.tex and process it with pdflatex? This should work without error using an up-to-date TeX installation, such as MiKTeX or TeXLive-2022, or maybe using an online service such as Overleaf. – AlexG Jan 20 '23 at 11:21
  • I am using TeX Studio. with Linux OS. TexLive I am using is 2021. Tell me a link or tutorial to upgrade TexLive then.. it must be the outdated version of mine. – Freya the Goddess Jan 21 '23 at 05:49
  • I tested with texlive-2021 and it worked too. The "export undefined in families anim@pkg" error is somewhat disturbing. It looks as if an ancient animate.sty package file is loaded. The export option first appeared in 8/2018. Could you please look for animate.sty files on your computer and delete those outside the TeXLive installation? – AlexG Jan 23 '23 at 08:14
2

This is only a suggestion but too long to be a comment. The animation offered by AlexG is very good, and my animation is essentially the same.

What I'm changing is the 3d view for one more realistic, specially for the sphere. For this I'd use 3d and perspective TikZ libraries. I'll need intersections too, to set the visibility with a \clip.

This is my approach:

\documentclass[tikz,border=2mm]{standalone}
\usepackage[export]{animate}
\usetikzlibrary{3d,intersections,perspective}

\definecolor{ocre}{RGB}{243,102,25} \tikzset {% styles not visible/.style={draw=gray,very thin},% <--- change it for dashed if you prefer section/.style={not visible,fill=ocre,fill opacity=0.5}, }

% parameterised tikzpicture with three parameters % #1 = rotation angle around z, % #2 = 3d view angle azimuth, % #3 = 3d view angle elevation > 0 !!! \NewDocumentCommand\parampicture{mmm}{% \begin{tikzpicture}[line cap=round,line join=round,3d view={#2}{#3}] % cylinder \draw[not visible] (#2-180:1) arc (#2-180:#2-360:1); \begin{scope}[rotate around z=#1+#2,canvas is xz plane at y=0] \fill[section] (0,0) rectangle (1,2); \draw[not visible] (0,0.25) -| (0.25,0); \draw (0,2) -- (1,2) \ifnum#1>180 -- (1,0)\fi; \end{scope} \draw (#2-180:1) ++ (0,0,2) --++ (0,0,-2) arc (#2-180:#2:1) --++ (0,0,2); \draw (0,0,2) circle (1);
% sphere \begin{scope}[shift={(#2:2.5)}] \path[name path=sphere] (0,0,1) circle (0.999cm);% Ooops, TikZ precision!!! \draw[not visible] (0,0,0) -- (0,0,2); \begin{scope}[rotate around z=#2,canvas is xy plane at z=1] \draw[not visible] (1,0) arc (0: 180:1); \draw (1,0) arc (0:-180:1); \end{scope} \begin{scope}[rotate around z=#1+#2,canvas is xz plane at y=0] \draw[section,name path=semicircle] (0,0) arc (-90:90:1); \path[name intersections={of=sphere and semicircle}]; \clip[overlay] (0,1) -- (intersection-1) --++ (2,0) |- (-1,3) -- cycle; \draw (0,0) arc (-90:90:1); \end{scope} \draw (0,0,1) circle (1cm); \end{scope} \end{tikzpicture}% }

\begin{document} \begin{animateinline}[autoplay,loop]{25} % better to avoid a frame with i=90 \multiframe{90}{i=0+4}{\parampicture{\i}{130}{30}} \end{animateinline} \end{document}

enter image description here

Juan Castaño
  • 28,426