[Tex/LaTex] Cylinder with specific orientation

3dtikz-pgf

I realize there are a couple of questions about cylinders in Tikz, but I haven't figured out how to add them to my image.

I have something like:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,3d,arrows,shapes}
\usepackage{tikz-3dplot}

\begin{document}
\tdplotsetmaincoords{70}{46+90} % rotate 60 degrees around x axis, then 105 degrees about z
\begin{tikzpicture}[>=stealth',           % arrow tip
                    tdplot_main_coords,   % 
                    scale=0.5               % scale
                    ]  
   % XYZ axis
   \draw[thick,->] (0,0,0) -- (6,0,0) node[anchor=north east]{\textbf{x}};
   \draw[thick,->] (0,0,0) -- (0,6,0) node[anchor=north]{\textbf{y}};
   \draw[thick,->] (0,0,0) -- (0,0,6) node[anchor=south]{\textbf{z}};
    \draw[dotted] (0,0,0) circle (15);
    \draw plot [mark=*, mark size=3] coordinates{(15,0,0)} node[anchor=south east]{S}; 

% my lousy attempt
 \node [cylinder,draw=black,thick,aspect=0.5,minimum height=1cm,minimum width=0.25cm,shape border      
   rotate=0,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!5] at (15,0,0){S};
\end{tikzpicture}

\end{document}

enter image description here

Where there is a 3D coordinate system, and a big circle with an specific coordinate "S" on where I want a cylinder, oriented towards (0,0,0). However I can't seem to achieve this orientation.

If the only way to approach this is to manually draw the cylinder, then I have a problem with joining the "lids". I can draw 2 filled circles in another orientation than XY plane (as @john Kormylo shows in the comments) but I have no idea how to "attach" those circles

Best Answer

As far as I know, this is not easily achieved. Because node shapes are always drawn on the canvas, but you need a three-dimensional one, we need to draw the cylinder by hand. This is easily done in the rotated reference frame tikz-3dplot has ready. I set this frame in such a way that the face of the cylinder is in this plane. The circular face is easily drawn then. The only problem is drawing the side lines at the right position. For this we need to find the right angle in this plane, for which I created a macro a while back here, which I call \rotatedtangentangle here.

Putting everything together yields:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,3d,arrows,shapes}
\usepackage{tikz-3dplot}

\begin{document}
\tdplotsetmaincoords{70}{46+90} % rotate 60 degrees around x axis, then 105 degrees about z
\begin{tikzpicture}[>=stealth',           % arrow tip
                    tdplot_main_coords,   % 
                    scale=0.5               % scale
                    ]

\newcommand{\rotatedtangentangle}[1]{%
    % find directions of projection
    \path[tdplot_rotated_coords] (1,0,0);
    \pgfgetlastxy{\axisxx}{\axisxy}
    \path[tdplot_rotated_coords] (0,1,0);
    \pgfgetlastxy{\axisyx}{\axisyy}
    \path[tdplot_rotated_coords] (0,0,1);
    \pgfgetlastxy{\axiszx}{\axiszy}
    % angle of tangent
    \pgfmathsetmacro{\rtang}{atan(-\axiszy/\axiszx)+180}
    \pgfmathsetmacro{\angkorr}{atan(\axisyy/\axisyx)/2}

    \pgfmathsetmacro{#1}{\rtang+\angkorr}
}%

   % XYZ axis
    \draw[thick,->] (0,0,0) -- (6,0,0) node[anchor=north east]{\textbf{x}};
    \draw[thick,->] (0,0,0) -- (0,6,0) node[anchor=north]{\textbf{y}};
    \draw[thick,->] (0,0,0) -- (0,0,6) node[anchor=south]{\textbf{z}};
    \draw[dotted] (0,0,0) circle (15);

    \tdplotsetthetaplanecoords{90} % create rotated frame
    \rotatedtangentangle{\tangent} % compute tanget angle
    % shift rotated frame to center of cylinder
    \coordinate (shift) at (15,0,0);
    \tdplotsetrotatedcoordsorigin{(shift)}
    % draw cylinder
    \begin{scope}[tdplot_rotated_coords]
        % draw the side lines and arc of the cylinder (here length 1, radius 0.5)
        \draw[fill=red!30]
            (0,0,-1) ++(\tangent:0.5) -- ++(0,0,1) arc (\tangent:\tangent-180:0.5) -- ++(0,0,-1);
        % draw the circular face (radius 0.5)
        \draw[fill=red!30] (0,0,-1) circle [radius=0.5];
        \node at (0,0,0) {S};
    \end{scope}

\end{tikzpicture}

\end{document}

enter image description here