我正在尝试旋转一个矩形来制作轨迹并得到一个圆柱体。我的代码
\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\pgfmathsetmacro{\myr}{3}
\pgfmathsetmacro{\h}{4}
\foreach \X in {10,20,...,350}
{\tdplotsetmaincoords{70}{\X}
\begin{tikzpicture}[tdplot_main_coords,scale=1,line cap=butt,line join=round]
\begin{scope}[canvas is xy plane at z=0]
\coordinate (O) at (0,0);
\coordinate (A) at (0,\myr);
\end{scope}
\begin{scope}[canvas is xy plane at z=\h]
\coordinate (O') at (0,0);
\coordinate (B) at (0,\myr);
\end{scope}
\foreach \v/\position in { B/below,O/below,A/below,O'/above} {\draw[draw =black, fill=black] (\v) circle (1pt) node [\position=0.2mm] {$\v$};
}
\draw[thick] (O) -- (A) -- (B) -- (O') -- cycle;
\end{tikzpicture}}
\end{document}
如何得到结果?
答案1
这利用了坐标是全局的这一特点。即使我们在每一帧中绘制一张新图片,坐标A-\Z
和B-Z
仍然是已知的。而且可以说在 yz 平面中绘制矩形并使用旋转坐标更简单。
\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\pgfmathsetmacro{\myr}{3}
\pgfmathsetmacro{\h}{4}
\tdplotsetmaincoords{70}{0}
\foreach \X [count=\Z] in {0,10,...,360}
{
\begin{tikzpicture}[tdplot_main_coords,scale=1,line cap=butt,line join=round]
\path[tdplot_screen_coords,use as bounding box] (-1.2*\myr,-2) rectangle (1.2*\myr,1.25*\h);
\tdplotsetrotatedcoords{\X}{0}{0}
\begin{scope}[tdplot_rotated_coords,canvas is yz plane at x=0]
\path (0,0) coordinate (O)
(\myr,0) coordinate (A-\Z)(\myr,0) coordinate (A)
(0,\h) coordinate (O')
(\myr,\h) coordinate (B-\Z) (\myr,\h) coordinate (B);
\end{scope}
\foreach \v/\position in {B/below,O/below,A/below,O'/above}
{\draw[draw =black, fill=black] (\v) circle (1pt)
node [\position=0.2mm] {$\v$};
}
\draw[thick] (O) -- (A-\Z) -- (B-\Z) -- (O') -- cycle;
\ifnum\Z>1
\draw plot[smooth,samples at={1,...,\Z}] (B-\x);
\ifnum\Z>8
\ifnum\Z>27
\draw[dashed] plot[smooth,samples at={1,...,9}] (A-\x);
\draw[dashed] plot[smooth,samples at={28,...,\Z}] (A-\x);
\draw plot[smooth,samples at={9,...,28}] (A-\x);
\draw (-\myr,0,0) -- (-\myr,0,\h);
\draw (\myr,0,0) -- (\myr,0,\h);
\else
\draw[dashed] plot[smooth,samples at={1,...,9}] (A-\x);
\draw plot[smooth,samples at={9,...,\Z}] (A-\x);
\draw (-\myr,0,0) -- (-\myr,0,\h);
\fi
\else
\draw plot[smooth,samples at={1,...,\Z}] (A-\x);
\fi
\fi
\end{tikzpicture}}
\end{document}
附录:minthien_2016 的静态版本不适合评论。
\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{0}
\begin{tikzpicture}[tdplot_main_coords,scale=1,line cap=butt,line join=round]
\pgfmathsetmacro{\myr}{3}
\pgfmathsetmacro{\h}{4}
\begin{scope}[canvas is xy plane at z=0]
\draw[dashed] (\myr,0) arc(0:180:\myr);
\draw (\myr,0) arc(0:-180:\myr);
\end{scope}
\foreach \X [evaluate=\X as \mixture using {int(50+50*sin(\X))}]
in {-80,-70,...,270}
{\tdplotsetrotatedcoords{\X}{0}{0}
\begin{scope}[tdplot_rotated_coords,canvas is yz plane at x=0]
\draw[fill=blue!\mixture!red,fill opacity=0.1] (0,0) rectangle (\myr,\h);
\end{scope}}
\begin{scope}[canvas is xy plane at z=\h]
\draw circle [radius=\myr];
\end{scope}
\end{tikzpicture}
\end{document}
附录 2:更多 Ti钾Zy 版本带有一个3d cylinder
图片,其中高度和半径存储在 pgf 键中。它还绘制了一个“完整圆柱体”。
\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\tikzset{pics/3d cylinder/.style={code={%
\tikzset{3d cylinder/.cd,#1}
\draw[left color=gray!90,right color=gray!60,middle color=gray!20]
plot[domain=\tdplotmainphi:\tdplotmainphi-180,variable=\t,smooth]
({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\t)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\t)},0)
--
plot[domain=\tdplotmainphi-180:\tdplotmainphi,variable=\t,smooth]
({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\t)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\t)},
\pgfkeysvalueof{/tikz/3d cylinder/h})
-- cycle;
\draw[fill=gray!30]
plot[domain=0:360,variable=\t,smooth cycle]
({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\t)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\t)},
\pgfkeysvalueof{/tikz/3d cylinder/h});
}},3d cylinder/.cd,r/.initial=1,h/.initial=1}
\begin{document}
\tdplotsetmaincoords{70}{0}
\foreach \X [count=\Z] in {0,10,...,360}
{\begin{tikzpicture}[tdplot_main_coords,scale=1,
line cap=butt,line join=round,3d cylinder/r=3,3d cylinder/h=4]
\path[tdplot_screen_coords,use as bounding box]
(-1.2*\pgfkeysvalueof{/tikz/3d cylinder/r},
-0.33*\pgfkeysvalueof{/tikz/3d cylinder/h}) rectangle
(1.2*\pgfkeysvalueof{/tikz/3d cylinder/r},
1.25*\pgfkeysvalueof{/tikz/3d cylinder/h});
\draw[fill=gray!80]
plot[domain=\tdplotmainphi+90:\tdplotmainphi+90+\X,variable=\t,smooth]
({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\t)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\t)},0)
-- (0,0,0) -- cycle;
\ifnum\X<190
\draw[fill=gray!80] plot[domain=\tdplotmainphi+90:\tdplotmainphi+90+\X,variable=\t,smooth]
({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\t)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\t)},0)
--
plot[domain=\tdplotmainphi+90+\X:\tdplotmainphi+90,variable=\t,smooth]
({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\t)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\t)},
\pgfkeysvalueof{/tikz/3d cylinder/h})
-- cycle;
\fi
\draw[fill=gray!30]
plot[domain=\tdplotmainphi+90:\tdplotmainphi+90+\X,variable=\t,smooth]
({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\t)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\t)},
\pgfkeysvalueof{/tikz/3d cylinder/h})
-- (0,0,\pgfkeysvalueof{/tikz/3d cylinder/h}) -- cycle;
\ifnum\X>90
\ifnum\X<280
\clip plot[domain=\tdplotmainphi+180:\tdplotmainphi+90+\X,variable=\t,smooth]
({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\t)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\t)},0)
--({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\tdplotmainphi+90+\X)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\tdplotmainphi+90+\X)},
\pgfkeysvalueof{/tikz/3d cylinder/h})
-- (0,0,\pgfkeysvalueof{/tikz/3d cylinder/h})
-- plot[domain=\tdplotmainphi+90:\tdplotmainphi+180,variable=\t,smooth]
({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\t)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\t)},
\pgfkeysvalueof{/tikz/3d cylinder/h})
-- cycle;
\pic{3d cylinder};
\else
\clip (0,0,\pgfkeysvalueof{/tikz/3d cylinder/h})
-- ({\pgfkeysvalueof{/tikz/3d cylinder/r}*cos(\tdplotmainphi+90+\X)},
{\pgfkeysvalueof{/tikz/3d cylinder/r}*sin(\tdplotmainphi+90+\X)},
\pgfkeysvalueof{/tikz/3d cylinder/h})
-| (current bounding box.south east)
-| (current bounding box.north west)
-| cycle;
\pic{3d cylinder};
\fi
\fi
\end{tikzpicture}}
\end{document}