[Tex/LaTex] Is it possible to draw a 3d cylinder using TikZ-3dplot

3dtikz-3dplottikz-pgf

I want to draw a clyinder between two planes. I want to specify the distance between the planes and position and radius of the cylinder.

Result

I would like to use tikz-3dplot, because it allows a free rotation of the Viewpoint if everything is implemented with it.

There are a few Threads in which the creation of 3D bodies in TikZ, Drawing simple 3D cylinders in TikZ and Drawing 3D cylinder have been discussed.

Unfortunately those solution seem not to work with tikz-3dplot, because they all manipulate the 'Viewpoint' themself. This leads to the Problem, that a manipulation of the viewpoint with \tdplotsetmaincoords only rotates all Objects except the Cylinders.

Result at the moment with tikz
Result at the moment with Tikz - front view

Another User had a very similar question which yielded no answer yet.
How to draw a 3d cylinder using TikZ-3dplot?

To summarize: I want to draw a cylinder in 3d using tikz-3dplot. At the Moment i can draw the circle at the end of the cylinder and a line from border to border, which only recognizes rotation around the x-axis. The Question is how can i put the lines correctly at the side of the cylinder.
Solution at the moment

Example of Code:

\documentclass[convert = false, tikz]{standalone}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}

\usepackage{amsmath}
\usepackage{tikz-3dplot}

\begin{document}

%Configure here Rotation around x and y axis
\pgfmathsetmacro{\x}{60}
\pgfmathsetmacro{\y}{100}

\tdplotsetmaincoords{\x}{\y}

\begin{tikzpicture}[ tdplot_main_coords]

  \coordinate (O) at (0, 0, 0);

    \draw[-latex] (O) -- (-8, 0, 0) node[font = \small, pos = 1.1] {\(x\)};
    \draw[-latex] (O) -- (0, 6, 0) node[font = \small, pos = 1.1] {\(y\)};
    \draw[-latex] (O) -- (0, 0, 6) node[font = \small, pos = 1.1] {\(z\)};

    % define size of planes
    \pgfmathsetmacro{\planesize}{4}

    %plane1
    \filldraw[blue, opacity = .2]
         (0, 0, 0)
          -- (0, \planesize, 0)
          -- (0, \planesize, \planesize)
          -- (0, 0, \planesize)
          -- cycle;


    %Define Plane in which the circles of the cylinder are drawn.
    \tdplotgetpolarcoords{0}{1}{0}
    \tdplotsetthetaplanecoords{\tdplotresphi}

    %\tdplotdrawarc[coordinate system, draw styles]{center}{r}{angle start}{angle end}{label options}{label}
    %circle on first plane
    \pgfmathsetmacro{\radius}{0.5}
    \tdplotdrawarc[tdplot_rotated_coords]{(1,1,0)}{\radius}{0}%
    {360}{}{}

    %circle on second plane  
    \tdplotdrawarc[tdplot_rotated_coords]{(1,1,4)}{\radius}{0}%
    {360}{}{}

    %line from middle to middle
    \draw[tdplot_rotated_coords] (1,1,0) -- (1, 1, 4) node[font = \small, pos = 1.1] {};

    %First Edge of cylinder
     \draw[tdplot_rotated_coords] 
     ({1+sin(\x)*\radius},
      {1+cos(\x)*\radius},0)
       -- 
     ({1+sin(\x)*\radius},
      {1+cos(\x)*\radius},
       4) 
     node[font = \small, pos = 1.1] {};


    %plane2
    \filldraw[blue, opacity = .2]
             (-4, 0, 0)
              -- (-4, \planesize, 0)
              -- (-4, \planesize, \planesize)
              -- (-4, 0, \planesize)
              -- cycle;
\end{tikzpicture}
\end{document}

Best Answer

Attempted with pgfplots:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
    view={45}{30},
    axis equal image,
    axis lines=center,
    xtick=\empty,ytick=\empty,ztick=\empty,
    colormap={white}{color=(white)color=(white)},
    mesh/interior colormap={blue}{color=(blue!20)color=(blue!20)},
    z buffer=sort]
    \addplot3 [domain=0:8,y domain=0:8,surf,opacity=0.6,shader=flat,samples=2] (x,8,y);
    \addplot3 [domain=0:8,y domain=0:2*pi,mesh,black,samples=25] ({4+1*cos(deg(y))},{8},{4+1*sin(deg(y))});
    \addplot3 [domain=0:8,y domain=0:2*pi,surf,shader=interp,samples=25] ({4+1*cos(deg(y))},{x},{4+1*sin(deg(y))});
    \addplot3 [domain=0:8,y domain=0:8,surf,opacity=0.6,shader=flat,samples=2] (x,0,y); 
    \addplot3 [domain=0:8,y domain=0:2*pi,mesh,black,samples=25] ({4+1*cos(deg(y))},{0},{4+1*sin(deg(y))});
    \end{axis}
\end{tikzpicture}

\end{document}

Result:

enter image description here

Remark:

Commands

\addplot3 [domain=0:8,y domain=0:8,surf,opacity=0.6,shader=flat,samples=2] (x,0,y); 

and

\addplot3 [domain=0:8,y domain=0:8,surf,opacity=0.6,shader=flat,samples=2] (x,8,y); 

draw planes y=0 and y=8. While command

\addplot3 [domain=0:8,y domain=0:2*pi,surf,shader=interp,samples=25] ({4+1*cos(deg(y))},{x},{4+1*sin(deg(y))});{x},{4+1*sin(deg(y))});

drawing a cylinder (a+bcos(y),x,a+bsin(y)), where a=4 and b=1. I also added:

\addplot3 [domain=0:8,y domain=0:2*pi,mesh,black,samples=25] ({4+1*cos(deg(y))},{0},{4+1*sin(deg(y))});
\addplot3 [domain=0:8,y domain=0:2*pi,mesh,black,samples=25] ({4+1*cos(deg(y))},{8},{4+1*sin(deg(y))});

to mark where the cylinder and plane intercept, as pgfplots couldn't handle that automatically.

You can rotate the view point freely by changing the view={...}{...} option. For example, view={60}{30} will give you:

enter image description here

Most of the options of pgfplots are quite self-explanatory, refer to the package document for further detail.