![如何在 TikZ 的 foreach 循环中平移和旋转来制作动画?](https://linux22.com/image/420173/%E5%A6%82%E4%BD%95%E5%9C%A8%20TikZ%20%E7%9A%84%20foreach%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E5%B9%B3%E7%A7%BB%E5%92%8C%E6%97%8B%E8%BD%AC%E6%9D%A5%E5%88%B6%E4%BD%9C%E5%8A%A8%E7%94%BB%EF%BC%9F.png)
我想将水平线段动画化为垂直线段,围绕中点旋转并平移。请参阅下面的 gif。
我不知道为什么翻译没有按我预期的那样进行。紫色部分应该移动到最后的蓝色位置。
平均能量损失
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\foreach \a in {0,1,...,10}{%
\begin{tikzpicture}
\useasboundingbox (-.2,-1.2) rectangle (4.2,2.2);
\draw[help lines] (0,0) grid (4,2);
\draw[red] (1,0) -- +(1,0);
\draw[blue] (2,1) -- +(0,1);
\draw[thick] (1,0)++(\a/10,\a/10) -- +(1,0); % translation
\draw[thick] let \p1=(1,0), \p2=(2,0) in [rotate around={-9*\a:(1.5,0)}] (\p1) -- (\p2) ; % rotation around mid point
\draw[thick,red!50!blue,shift={+(\a/10,\a/10)}] let \p1=(1,0), \p2=(2,0) in [rotate around={-9*\a:(1.5,0)}] (\p1) -- (\p2) ; % rotation + translation WRONG!!
\end{tikzpicture}
}
\end{document}
答案1
我不确定我是否理解了你的问题。这是你想要的吗?
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\foreach \a in {0,1,...,10}{%
\begin{tikzpicture}
\useasboundingbox (-.2,-1.2) rectangle (4.2,2.2);
\draw[help lines] (0,0) grid (4,2);
\draw[red] (1,0) -- +(1,0);
\draw[blue] (2,1) -- +(0,1);
%\draw[thick] (1,0)++(\a/10,\a/10) -- +(1,0); % translation
%\draw[thick,green!20!black] let \p1=(1,0), \p2=(2,0) in [rotate around={-9*\a:(1.5,0)}] (\p1) -- (\p2) ; % rotation around mid point
\draw[thick,red!50!blue,shift={(\a/20,\a*1.5/10)}
]
let \p1=(1,0),
\p2=(2,0) in
[rotate around={-9*\a:(1.5,0)}]
(\p1) -- (\p2) ; % rotation + translation WRONG!!
\end{tikzpicture}
}
\end{document}
答案2
我认为你应该添加这一行:
\draw [green,rotate around={-9*\a:(2,0.1*\a)}] (1,0.1*\a) -- (2,0.1*\a); %Correct rotation
所以它看起来像这样:
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\foreach \a in {0,1,...,10}{%
\begin{tikzpicture}
\useasboundingbox (-.2,-1.2) rectangle (4.2,2.2);
\draw[help lines] (0,0) grid (4,2);
\draw[red] (1,0) -- +(1,0);
\draw[blue] (2,1) -- +(0,1);
\draw[thick] (1,0)++(\a/10,\a/10) -- +(1,0); % translation
\draw[thick] let \p1=(1,0), \p2=(2,0) in [rotate around={-9*\a:(1.5,0)}] (\p1) -- (\p2) ; % rotation around mid point
\draw[thick,red!50!blue,shift={+(\a/10,\a/10)}] let \p1=(1,0), \p2=(2,0) in [rotate around={-9*\a:(1.5,0)}] (\p1) -- (\p2) ; % rotation + translation WRONG!!
%\draw [magenta,rotate around={-9*\a:(2,0)}] (1,0) -- (2,0); %rotation
%\draw [yellow] (1,0.1*\a) -- (2,0.1*\a);%translation vertical
\draw [green,rotate around={-9*\a:(2,0.1*\a)}] (1,0.1*\a) -- (2,0.1*\a); %Correct rotation
\end{tikzpicture}
}
\end{document}