# how to draw multi position cylinder with tikz

I would like to draw cylinders (tikz shape) with different orientations. thus I will wish that the cylinder (2) is parallel to the x axis.

I'm tested different values for the parameters I can not thanks for your help

\documentclass{article}
\usepackage{tikz,esvect}
\usetikzlibrary{3d,calc,shapes}
\begin{document}
\begin{tikzpicture}[x={(-0.2cm,-0.4cm)}, y={(1cm,0cm)}, z={(0cm,1cm)}]
\draw[-latex] (0,0,0) coordinate(O) -- (5,0,0) node[above]{$\vv{x}$};
\draw[-latex] (0,0,0) coordinate(O) -- (0,5,0) node[above]{$\vv{y}$};
\draw[-latex] (0,0,0) coordinate(O) -- (0,0,5) node[right]{$\vv{z}$};

\tikzset{zxplane/.style={canvas is zx plane at y=#1,very thin}}
\tikzset{yxplane/.style={canvas is yx plane at z=#1,very thin}}

\begin{scope}[yxplane=3]
\coordinate(C) at (0,5);
\draw[dashed] (0,0) -- (C);
\draw[thick,blue] ($(C)+(-0.25,-0.5)$) --++(-0.5,0.5)--++(+0.5,0.5);

\node [cylinder,draw=black,thick,aspect=1.5,minimum height=2cm,minimum width=1cm,shape border rotate=90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C)-(0,0,-1)$){1};
\draw[thick,blue] ($(C)+(0.25,-0.5)$) --++(0.5,0.5)--++(-0.5,0.5);
\coordinate(C2) at (5,0);
\node [cylinder,draw=black,thick,aspect=1,minimum height=2cm,minimum width=1cm,shape border
rotate=-90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C2)-(0,0,-1)$){2};

\coordinate(C3) at (2.5,0);
\node [cylinder,draw=black,thick,aspect=0.5,minimum height=2cm,minimum width=1cm,shape border
rotate=0,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C3)-(0,0,-1)$){3};

\end{scope}
\begin{scope}[yxplane=0]

\coordinate(C4) at (2.5,0);
\node [cylinder,draw=black,thick,aspect=1.5,minimum height=2cm,minimum width=1cm,shape border
rotate=90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C4)-(0,0,-1)$){4};

\end{scope}

\draw[dashed]  (C2) -- (C3) --(C4);
\end{tikzpicture}

\end{document}

• In this problem, no matter how large or small I make the (vertical) cylinder, the y radius (screen coordinates) is set to .2cm, which is then scaled by the aspect ratio. – John Kormylo Sep 12 '15 at 22:25
• Also, the dashed ellipse is slightly slanted. This is because the x and y axes (virtual) are not really orthogonal. There is no possible POV in which orthogonal axes will appear as shown. – John Kormylo Sep 12 '15 at 22:28
• thanks @JohnKormylo. How can I draw this? – rpapa Sep 13 '15 at 8:05

Remember that node are drawn using screen coordinates, not canvas coordinates, so you will have to do all the 3d corrections directly.

To construct orthogonal axes it is easiest to pick two angles and and use polar transformations. You could also use tikz-3d, but it has its own drawbacks.

Given the axes, you can determine the ratio of the y radius to x radius for a given plane. For a vertical cylinder 1cm wide the x radius is .5cm and the y radius is .2cm times the aspect parameter. This means one should multiply the desired aspect by 2.5

Because the y axis is no longer horizontal, cylinders 2 and 3 were rotated slightly.

\documentclass{article}
\usepackage{tikz,esvect}
\usetikzlibrary{3d,calc,shapes}
\begin{document}

\def\mytheta{10}% POV angles
\def\myphi{20}

\pgfmathparse{sin(\mytheta)}
\let\xx=\pgfmathresult
\pgfmathparse{cos(\mytheta)}
\let\yx=\pgfmathresult
\pgfmathparse{sin(\myphi)}
\let\zxy=\pgfmathresult
\pgfmathparse{\yx * \zxy}
\let\xy=\pgfmathresult
\pgfmathparse{\xx * \zxy}
\let\yy=\pgfmathresult
\pgfmathparse{cos(\myphi)}
\let\zz=\pgfmathresult
\pgfmathparse{atan(\yy/\yx)}
\let\rotate=\pgfmathresult

\begin{tikzpicture}[x={(-\xx cm,-\xy cm)}, y={(\yx cm,-\yy cm)},
z={(0cm,\zz cm)}]
\draw[-latex] (0,0,0) coordinate(O) -- (5,0,0) node[above]{$\vv{x}$};
\draw[-latex] (0,0,0) coordinate(O) -- (0,5,0) node[above]{$\vv{y}$};
\draw[-latex] (0,0,0) coordinate(O) -- (0,0,5) node[right]{$\vv{z}$};

\tikzset{zxplane/.style={canvas is zx plane at y=#1,very thin}}
\tikzset{yxplane/.style={canvas is yx plane at z=#1,very thin}}

\begin{scope}[yxplane=3]
\coordinate(C) at (0,5);
\draw[dashed] (0,0) -- (C);
\draw[thick,blue] ($(C)+(-0.25,-0.5)$) --++(-0.5,0.5)--++(+0.5,0.5);

\pgfmathparse{2.5 * \zxy}
\let\aspect=\pgfmathresult
\node [cylinder,draw=black,thick,aspect={\aspect},minimum height=2cm,minimum width=1cm,shape border rotate=90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C)-(0,0,-1)$){1};
\draw[thick,blue] ($(C)+(0.25,-0.5)$) --++(0.5,0.5)--++(-0.5,0.5);
\coordinate(C2) at (5,0);
\pgfmathparse{2.5 * \zz}
\let\aspect=\pgfmathresult
\node [cylinder,draw=black,thick,aspect={\aspect},minimum height=2cm,minimum width=1cm,shape border rotate=-90,
rotate={-\rotate},cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C2)-(0,0,-1)$){2};

\coordinate(C3) at (2.5,0);
\pgfmathparse{2.5 *\xx}
\let\aspect=\pgfmathresult
\node [cylinder,draw=black,thick,aspect=0.5,minimum height=2cm,minimum width=1cm,rotate={-\rotate},
cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C3)-(0,0,-1)$){3};

\end{scope}
\begin{scope}[yxplane=0]
\pgfmathparse{2.5 * \zxy}
\let\aspect=\pgfmathresult
\coordinate(C4) at (2.5,0);
\node [cylinder,draw=black,thick,aspect={\aspect},minimum height=2cm,minimum width=1cm,shape border
rotate=90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at ($(C4)-(0,0,-1)$){4};

\end{scope}

\draw[dashed]  (C2) -- (C3) --(C4);
\end{tikzpicture}

\end{document}


• You don't have to do all the calculations. I mean, you could use tikz-3dplot if you wanted to. – cfr Apr 1 '17 at 3:46
• @cfr - The cylinder node uses screen coordinates. See also tex.stackexchange.com/questions/253027/… – John Kormylo Apr 1 '17 at 13:45
• What I meant was that tikz-3dplot could do at least some of the required calculations. That might be simpler overall, even if it means drawing the cylinder rather than using the node. Drawing a cylinder is, after all, pretty straightforward, whereas the calculations are relatively complex. – cfr Apr 1 '17 at 15:14