3

In TikZ manual, cylinders are said to be "a 2-dimensional representation of a cylinder". The result is that they can be rotated only in a plane.

This is my code:

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

  \draw[->] (0,-4,0) -- (0,4,0) node[above right] {$x$};
  \draw[->] (-4,0,0) -- (4,0,0) node[below right] {$y$};
  \draw[->] (0,0,4) -- (0,0,-4) node[below right] {$z$};
  \node (a) [draw, cylinder, shape aspect=1.8, rotate=180, minimum height=25mm, minimum width=12mm] {};

\end{tikzpicture}

\end{document}

And this is the output:

enter image description here

Question 1

Using TikZ, currently the cylinder axis coincides with the labelled y axis. How to make it coincide with the z axis instead? Any value given to rotate can not achieve this.


I also tried this code with tikz-3dplot:

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{shapes.geometric}
\usepackage{tikz-3dplot}

\begin{document}

\tdplotsetmaincoords{70}{30}
\begin{tikzpicture}[tdplot_main_coords]

\draw[->] (0,-4,0) -- (0,4,0) node[above right] {$x$};
\draw[->] (-4,0,0) -- (4,0,0) node[below right] {$y$};
\draw[->] (0,0,4) -- (0,0,-4) node[below right] {$z$};

\node (a) [draw, cylinder, shape aspect=1.8, rotate=180, minimum height=25mm, minimum width=12mm] {};

\end{tikzpicture}

\end{document}

But the result is very unsatisfying:

enter image description here

Question 2

How to draw this with tikz-3dplot?

3

You could use transform canvas for that.

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}

\begin{tikzpicture}

  \draw[->] (0,-4,0) -- (0,4,0) node[above right] {$x$};
  \draw[->] (-4,0,0) -- (4,0,0) node[below right] {$y$};
  \draw[->] (0,0,4) -- (0,0,-4) node[below right] {$z$};
  \begin{scope}[transform canvas={rotate=45}]
  \node (a) [draw, cylinder, shape aspect=1.8, rotate=180, minimum height=25mm, minimum width=12mm] {};
  \end{scope}

\end{tikzpicture}

\end{document}

enter image description here

Notice, however, that this is a 2D object, so do not expect too much, and transform canvas comes with a price, to see that try e.g. \draw[->] (a.south) -- ++(-1,-1);. You'd be better off if you use e.g. tikz-3dplot for this.

As for your 3dplot question: I guess you need to draw the cylinder by hand.

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{shapes.geometric}
\usepackage{tikz-3dplot}

\begin{document}

\tdplotsetmaincoords{70}{30}
\begin{tikzpicture}[tdplot_main_coords]

\draw[->] (0,-4,0) -- (0,4,0) node[above right] {$x$};
\draw[->] (-4,0,0) -- (4,0,0) node[below right] {$y$};
\draw[->] (0,0,4) -- (0,0,-4) node[below right] {$z$};

\draw plot[variable=\x,domain=0:360,samples=180] ({cos(\x)},-1.25,{sin(\x)});
\draw plot[variable=\x,domain=-45:135,samples=180] ({cos(\x)},1.25,{sin(\x)});

\foreach \x in {135,-45}
{\draw ({cos(\x)},-1.25,{sin(\x)}) -- ({cos(\x)},1.25,{sin(\x)});}
%\node (a) [draw, cylinder, shape aspect=1.8, rotate=180, minimum height=25mm, minimum width=12mm] {};

\end{tikzpicture}

\end{document}

enter image description here

The angles -45 and 135 are just guessed, not optimal, and need to be adjusted when changing the view angle. This could be automatized but would certainly deserve a new question.

  • It works and thank you. Again, I can not realize the differences between TikZ and a specific tikz-3dplot code. I edited the question with a new attempt with tikz-3dplot. It was not good, because I don't know it well. If you want, you can update your code, too, with a tikz-3dplot alternative example. – BowPark May 15 '18 at 18:16
  • @BowPark I made an update to explain in more detail what I mean. Ultimately it boils down to the statement that TikZ doesn't have a true 3D engine. So you either have to cheat or switch to asymptote. – user121799 May 15 '18 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.