# How to get trace to get a cylinder when I rotate a rectangle?

I am trying to rotate a rectangle to make trace and get a cylinder. My code

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


How to get the result?

• Is this your result? Commented Aug 26, 2019 at 9:50
• Yes. Thank you very much. Commented Aug 26, 2019 at 15:57

This exploits that coordinates are global. Even though we draw a new picture in each frame, the coordinates A-\Z and B-Z are still known. And it is arguably simpler to draw the rectangle in the yz plane, and to use rotated coordinates.

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


ADDENDUM: A static version for minthien_2016, doesn't fit in a comment.

\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]
\end{scope}
\end{tikzpicture}
\end{document}


ADDENDUM 2: A more TikZy version with a 3d cylinder pic in which the height and radius are stored in pgf keys. It also draws a "full cylinder".

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


• +1 Very cool ...
– user179355
Commented Aug 26, 2019 at 14:58
• Thank you very much. Commented Aug 27, 2019 at 23:43
• I am very like this answer. Commented Aug 29, 2019 at 3:39