# How to draw 3d cylinder directed in x axis with TikZ?

How to draw cylinder directed in x axis?

\documentclass[tikz]{standalone}
\usetikzlibrary{3d}

\begin{document}

\begin{tikzpicture}[x={(1cm,0.4cm)}, y={(8mm, -3mm)}, z={(0cm,1cm)}, line cap=round, line join=round]

%   % Main Axes
\draw[->] (0,0,0) -- (2,0,0) node[below] {$x$};
\draw[->] (0,0,0) -- (0,2,0) node[below left] {$y$};
\draw[->] (0,0,0) -- (0,0,2) node[above] {$z$};

% Big Axis
\draw[line cap=round, -latex, very thick] (-0.5,0,0) -- (10,0,0);

\begin{scope}[canvas is yz plane at x=5.5]
\draw[] (0,0)  circle (1);
\end{scope}

\begin{scope}[canvas is yz plane at x=8.5]
\draw[] (0,0)  circle (1);
\end{scope}

\end{tikzpicture}


In the future, it should look like picture below. But for now I have a problem with drawing a transparent cylinder along the desired axis in 3d.

• I believe this example from the TikZ manual is somewhat related (there's a sine in it, too). Aug 23, 2022 at 13:45
• Where does the figure come from? Aug 24, 2022 at 7:07
• Are you interested by drawing a cylinder and then being able to change the point of view? After all you need a figure that lives in 3d, so what you represent is dependent of the observer's position. Aug 25, 2022 at 7:21

Here's a start for that cylinder: What's in the foreground needs to be drawn last.

The angle 125 for the point of that tangent on the circles is guessed. I'm sure there's a way to calculate that from the given coordinate system vectors x, y and z.

## Code

\documentclass[tikz]{standalone}
\usetikzlibrary{3d}
\begin{document}
\begin{tikzpicture}[
x={(1cm,0.4cm)},
y={(8mm, -3mm)},
z={(0cm,1cm)},
line cap=round,
line join=round,
delta angle=-180,
front/.style={canvas is yz plane at x=5.5},
back/.style={canvas is yz plane at x=8.5},
]

% Main Axes
\draw[->] (0,0,0) -- (2,0,0) node[below] {$x$};
\draw[->] (0,0,0) -- (0,2,0) node[below left] {$y$};
\draw[->] (0,0,0) -- (0,0,2) node[above] {$z$};

% back gray arc
\draw[back, gray] (125:1) arc[start angle=125, delta angle=180];
% bix axis (through and behind cylinder)
\draw[line cap=round, -latex, very thick] {
[front] (0,0)} -- (10,0,0);

% cylinder
\draw[fill=pink,fill opacity=.5] {
[back]
(125:1) arc[start angle=125]
}{
[front]
-- (125+180:1) arc[end angle=125]
} --cycle;
% front arc
\draw[front]      (125:1) arc[start angle=125];
% foreground axis
\draw[very thick, line cap=rect] (-0.5,0,0) -- (5.5,0,0);
\end{tikzpicture}
\end{document}


## Output

You can use 3dtools to make cylinder.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
\begin{document}
\begin{tikzpicture}[3d/install view={phi=130,theta=70,psi=0},declare function={r=2;h=8;}]
\path[3d/record physical components]
(1,0,0) coordinate (ez')
(0,1,0) coordinate (ex')
(0,0,1) coordinate (ey');
\begin{scope}[x={(ex')},y={(ey')},z={(ez')}]
\pic{3d/frustum={r=r,R=r,h=h}};
\end{scope}
\draw[3d/hidden] (0,0,-r) -- (0,0,r) (0,0,0) -- (h,0,0) ;
\draw[3d/hidden] (0,0,0) -- (0,r,0);
\node[above left] (O) at  (0,0,0) {$O$};
\draw[3d/visible, - latex] (h,0,0) -- (h+ 3,0,0) node[above]{$x$};
\draw[3d/visible, - latex] (0,0,r) -- (0,0,r+1) node[above]{$z$};
\draw[3d/visible, - latex] (0,r,0) -- (0,r+1,0) node[above]{$y$};
\end{tikzpicture}
\end{document}


Let's draw the figure as it is. All codes is run on http://asymptote.ualberta.ca/.

Step 4. (will be added later)

Step 3. Now come the cylinder along the X-axis, I just use unitcylinder of the module three with some geometric transformations.

import graph3;
unitsize(5mm,2cm,1cm);
size3(8cm);
currentprojection=orthographic(Z-Y,zoom=.9,center=true);

// the sin wave and its bounding box on the XY-plane
triple f(real t) {return (t,sin(t),0);}
real xmin=0, xmax=16pi;
path3 gf=graph(f,xmin,xmax,operator..);
picture pic;
draw(pic,gf,red+2pt);
triple A=(0,-1,0), u=(0,2,0),v=(xmax,0,0);
path3 b=plane(u,v,A);
draw(surface(b),yellow+opacity(.5));
draw(b,blue);

// The cylinder along the X-axis
real h=8pi;
surface cyl=shift(12pi,0,0)*scale(h,3,3)*rotate(-90,Y)*unitcylinder;
draw(cyl,pink+opacity(.4));


Step 2. from 2D to 3D, use the graph of

triple f(real t) {return (t,sin(t),0);}


unitsize(5mm,2cm,1cm);
size3(8cm);
import graph3;
triple f(real t) {return (t,sin(t),0);}
path3 gf=graph(f,0,16pi,operator..);
picture pic;
draw(pic,gf,red+2pt);
triple A=(0,-1,0), u=(0,2,0),v=(16pi,0,0);
path3 b=plane(u,v,A);
draw(surface(b),yellow+opacity(.5));
draw(b,blue);


Step 1. 2D version use the graph of

real f(real x) {return sin(x);}


unitsize(5mm,2cm);
size(8cm);
import graph;
// the function y=f(x)=sin(x);
real f(real x) {return sin(x);}
guide gf=graph(f,0,16pi,operator..);
picture pic;
draw(pic,gf,red+2pt);
filldraw(box(point(pic,SW),point(pic,NE)),yellow+white+opacity(.5),blue);