如何在 TikZ 中的 foreach 循環中進行平移和旋轉以製作動畫?

如何在 TikZ 中的 foreach 循環中進行平移和旋轉以製作動畫?

我想將水平段動畫化為垂直段,圍繞中點旋轉它並平移它。請參閱下面的動圖。

我不知道為什麼翻譯沒有達到我的期望。紫色部分應移動到最終的藍色位置。

在此輸入影像描述

微量元素

\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}

相關內容