# Draw the three cylindrical sections

I want to draw the three conic sections (circle, ellipse and rectangle) where the cylinder and cross sections are colored:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{animate}
\usepackage{filecontents}

\begin{document}

\begin{filecontents}{time2.txt}
::0x0
::1
::2
::3
\end{filecontents}

\begin{animateinline}[
begin={
\begin{tikzpicture}[line cap=round,line join=round,x=1.0cm,y=1.0cm,scale=0.6]
\clip(-3,0) rectangle (6,6);
},
end={\end{tikzpicture}},
controls,timeline=time2.txt]{1}

\draw [rotate around={0.:(1.5,1.)},line width=1.pt] (1.5,1.) ellipse (1.5897934007793624cm and 0.5267286371193529cm);
\draw [rotate around={0.:(1.5,5.)},line width=1.pt] (1.5,5.) ellipse (1.5897934007793588cm and 0.5267286371193518cm);
\draw [line width=1.pt] (-0.089793400779358,5.)-- (-0.089793400779362,1.);
\draw [line width=1.pt] (3.089793400779359,5.)-- (3.0897934007793624,1.);

\newframe %elipse
\draw [rotate around={-22.21759426951831:(1.5,3.14)},line width=1.pt,color=blue,fill=blue,fill opacity=0.2] (1.5,3.14) ellipse (1.6601578476760366cm and 0.6350780103265238cm);

\newframe %circunferencia
\draw [line width=1.pt] (3.089793400779359,5.)-- (3.0897934007793624,1.);
\draw [line width=1.pt,color=green,fill=green,fill opacity=0.2] (1.5,3) ellipse (1.57 and 0.6);

\newframe %rectangulo
\draw [line width=1.pt] (3.089793400779359,5.)-- (3.0897934007793624,1.);
\draw [line width=1.pt,color=orange,fill=orange,fill opacity=0.2](0.4,5.38) -- (0.39,1.38) -- (2.63,0.63) -- (2.62,4.63) -- cycle;

\end{animateinline}

\end{document}

• I guess you might be better off if you use tikz-3dplot instead of these rotate around commands. – user121799 Oct 30 '18 at 17:02
• Do not use the command, the manuals that I have seen are not good, I'm trying to understand the construction that you helped me with the "Draw the four conic sections" – Samuel Diaz Oct 30 '18 at 17:21

Here is some code based on tikz-3dplot, which is used to fix the view. This is done by the command \tdplotsetmaincoords{70}{\X}, and then \begin{scope}[tdplot_main_coords]. In addition, the 3d library is used to switch in xy planes at various z values.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d,backgrounds}
% small fix for canvas is xy plane at z % https://tex.stackexchange.com/a/48776/121799
\makeatletter
\tikzoption{canvas is xy plane at z}[]{%
\def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
\def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
\def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
\tikz@canvas@is@plane}
\makeatother
\begin{document}
\foreach \X in {5,15,...,355}
{\tdplotsetmaincoords{70}{\X}
\begin{tikzpicture}[declare function={h(\x,\y)=2+0.5*(\y+\y*cos(\x));}]
\path[use as bounding box] (-4,-2) rectangle (18,10);
\begin{scope}
\begin{scope}[tdplot_main_coords]
\begin{scope}[canvas is xy plane at z=0]
\path[fill=orange!50] (0,0) circle (3);
\coordinate (bl) at (0+\tdplotmainphi:3);
\coordinate (br) at (180+\tdplotmainphi:3);
\draw[dashed] (bl) arc(0+\tdplotmainphi:180+\tdplotmainphi:3);
\draw[thick] (br) arc(180+\tdplotmainphi:360+\tdplotmainphi:3);
\coordinate (rbl) at (0:3);
\coordinate (rbr) at (180:3);
\end{scope}
\begin{scope}[canvas is xy plane at z=8]
\draw[thick,fill=orange!30] (0,0) circle (3);
\coordinate (tl) at (0+\tdplotmainphi:3);
\coordinate (tr) at (180+\tdplotmainphi:3);
\coordinate (rtl) at (0:3);
\coordinate (rtr) at (180:3);
\end{scope}
\begin{scope}[on background layer]
\fill[orange!20] (bl) rectangle (tr);
\end{scope}
\begin{scope}[canvas is xy plane at z=4]
\path[fill=blue,opacity=0.3] (0,0) circle (3);
\end{scope}
\end{scope}
\end{scope}
\begin{scope}[xshift=7cm]
\begin{scope}[tdplot_main_coords]
\begin{scope}[canvas is xy plane at z=0]
\path[fill=orange!50] (0,0) circle (3);
\coordinate (bl) at (0+\tdplotmainphi:3);
\coordinate (br) at (180+\tdplotmainphi:3);
\draw[dashed] (bl) arc(0+\tdplotmainphi:180+\tdplotmainphi:3);
\draw[thick] (br) arc(180+\tdplotmainphi:360+\tdplotmainphi:3);
\coordinate (rbl) at (0:3);
\coordinate (rbr) at (180:3);
\end{scope}
\begin{scope}[canvas is xy plane at z=8]
\draw[thick,fill=orange!30] (0,0) circle (3);
\coordinate (tl) at (0+\tdplotmainphi:3);
\coordinate (tr) at (180+\tdplotmainphi:3);
\coordinate (rtl) at (0:3);
\coordinate (rtr) at (180:3);
\end{scope}
\begin{scope}[on background layer]
\fill[orange!20] (bl) rectangle (tr);
\end{scope}
\fill[green!60!black,opacity=0.3] plot[variable=\x,domain=-180:180,samples=90] ({3*cos(\x)},{3*sin(\x)},{h(\x,3)});
\end{scope}
\end{scope}
\begin{scope}[xshift=14cm]
\begin{scope}[tdplot_main_coords]
\begin{scope}[canvas is xy plane at z=0]
\path[fill=orange!50] (0,0) circle (3);
\coordinate (bl) at (0+\tdplotmainphi:3);
\coordinate (br) at (180+\tdplotmainphi:3);
\draw[dashed] (bl) arc(0+\tdplotmainphi:180+\tdplotmainphi:3);
\draw[thick] (br) arc(180+\tdplotmainphi:360+\tdplotmainphi:3);
\coordinate (rbl) at (0:3);
\coordinate (rbr) at (180:3);
\end{scope}
\begin{scope}[canvas is xy plane at z=8]
\draw[thick,fill=orange!30] (0,0) circle (3);
\coordinate (tl) at (0+\tdplotmainphi:3);
\coordinate (tr) at (180+\tdplotmainphi:3);
\coordinate (rtl) at (0:3);
\coordinate (rtr) at (180:3);
\end{scope}
\begin{scope}[on background layer]
\fill[orange!20] (bl) rectangle (tr);
\end{scope}
\fill[red,opacity=0.3] (rtl) -- (rtr) -- (rbr) -- (rbl);
\end{scope}
\end{scope}
\end{tikzpicture}
}
\end{document}


• Thank you, very useful construction, which documentation recommends me to learn how to use tikz-3dplot. – Samuel Diaz Oct 30 '18 at 17:37
• @SamuelDiaz There is a manual. And you can learn from the examples on this site. The only feature of tikz-3dplot I am using here is to set the view with \tdplotsetmaincoords{70}{\X}, where 70 is the theta and \X the phi angle. And then I use the 3d library to switch to the xy planes at different z values. – user121799 Oct 30 '18 at 17:41
• @Marmota, how do you get the image published? – Samuel Diaz Oct 31 '18 at 4:03
• @SamuelDiaz What do you mean? If you're wondering about the conversion to an animated gif, I do convert -density 300 -delay 4 -loop 0 -alpha remove multipage.pdf animated.gif, see here. – user121799 Oct 31 '18 at 4:05
• For the record: Jake's patch is now incorporated in v3.1 of TikZ. – Stefan Pinnow Jan 15 at 19:13

I share my construction, thanks to marmot.

\documentclass{standalone}
\usepackage{tikz}

\usepackage{tikz-3dplot}
\usetikzlibrary{3d,backgrounds}
\usepackage{animate}
\usepackage{filecontents}

\begin{document}

\begin{filecontents}{time.txt}
::0x0
::1
::2
::3
\end{filecontents}

\tdplotsetmaincoords{70}{0}

\begin{animateinline}[
begin={
\begin{tikzpicture}[declare function={h(\x,\y)=2+0.5*(\y+\y*cos(\x));}];
\path[use as bounding box] (-2.2,-1) rectangle (2.5,6.5);
},
end={\end{tikzpicture}},
controls,timeline=time.txt]{1}

\begin{scope}[tdplot_main_coords]
%base inferior
\begin{scope}[canvas is xy plane at z=0]
\path[fill=orange!50] (0,0) circle (2);
\coordinate (bl) at (0:2);
\coordinate (br) at (180:2);
\draw[dashed] (bl) arc(0:180:2);
\draw[thick] (br) arc(180:360:2);
%rectangulo
\coordinate (rbl) at (60:2);
\coordinate (rbr) at (240:2);
\end{scope}

%Base superior
\begin{scope}[canvas is xy plane at z=6]
\draw[thick,fill=orange!30] (0,0) circle (2);
\coordinate (tl) at (0:2);
\coordinate (tr) at (180:2);
%rectangulo
\coordinate (rtl) at (60:2);
\coordinate (rtr) at (240:2);
\end{scope}

%superficie lateral
\begin{scope}[on background layer]
\fill[orange!20] (bl) rectangle (tr);
\end{scope}

\end{scope}

\newframe   %circulo
\begin{scope}[tdplot_main_coords]
\begin{scope}[canvas is xy plane at z=3]
\path[fill=blue,opacity=0.3] (0,0) circle (2);
\end{scope}
\end{scope}

\newframe    %elipse
\begin{scope}[tdplot_main_coords]
\fill[green!60!black,opacity=0.3] plot[variable=\x,domain=-180:180,samples=90] ({2*cos(\x)},{2*sin(\x)},{h(\x,2)});
\end{scope}

\newframe      %rectangulo
\begin{scope}[tdplot_main_coords]
\fill[red,opacity=0.3] (rtl) -- (rtr) -- (rbr) -- (rbl);
\end{scope}

\end{animateinline}

\end{document}


• Looks nice! ;-) – user121799 Oct 31 '18 at 4:06