我刚刚开始学习使用静态图像制作 tikz。这是我第一次尝试制作动画。
我有这个代码:
\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}
请注意,如果我在这里改变 $z$ 的值:
\fill[rotate around z=10, mild] (0,0) -- (0,0,2) -- (1,0,2) -- (1,0,0) -- cycle;
从 z=20 到 z=10,再到 z=0,z = -10,z = -20 等。它将使橙色阴影旋转 360 度。我想要这个动画。如何创建它。
包括球体的橙色阴影,我们可以创建阴影 360 度旋转的动画吗?
答案1
可以tikzpicture
通过将其包装在一个带有一个强制参数的命令中来实现参数化,该命令是是轴。在 中tikzpicture
,要参数化的旋转角度被命令参数替换#1
,即:
\fill[rotate around z=#1, mild] (0,0) -- (0,0,2) -- (1,0,2) -- (1,0,0) -- cycle;
然后,将该命令放在循环命令的主体中,例如\multiframe
pkg animate
,它传递一系列旋转角度。
;
请注意,原始实现中的两个太多了,导致出现Missing character: There is no ; in font nullfont!
警告。此外,代码经过了轻微修改,以允许旋转角度 > 180° 时出现虚线隐藏线。
完整代码:
\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}
要获取适用于 Okular 或 Acrobat Reader 的 PDF 动画,请export
从中删除选项animate
。
答案2
这只是一个建议,但太长了,不能作为评论。动画片由...提供亚历克斯非常好,和我的动画本质上是一样的。
我要改变的是 3D 视图,使其更加逼真,特别是球体。为此,我将3d
使用perspective
Ti钾Z 库。我也需要使用intersections
来设置可见性\clip
。
这是我的方法:
\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}