5

I want to draw the follwing figure in TikZ. figure1

Of course, I was able to draw most of its components, but I can not draw the cylindrical part. Here is my effort so far.

MWE:

\documentclass[14pt]{memoir}
\usepackage{tikz, tikz-3dplot}
\usetikzlibrary{arrows.meta}
\begin{document}
\tdplotsetmaincoords{60}{-30}
\begin{tikzpicture}[tdplot_main_coords, scale=0.9]
\tdplotsetrotatedcoords{0}{45}{0}
\pgfmathsetmacro{\r}{3}
\pgfmathsetmacro{\x}{1.25}
\pgfmathsetmacro{\y}{sqrt(\r^2-\x^2)}
\draw [thick, -Stealth] (-1,0,0) -- (5,0,0) node [right] {$x$};
\draw [thick, -Stealth] (0,-5,0) -- (0,5,0) node [left] {$y$};
\filldraw [fill=cyan!40, very thick, draw=blue, canvas is xy plane at z=0, opacity=0.5] (0,-\r) arc [start angle=-90, end angle=90, radius=\r] -- cycle;
\filldraw [fill=red!50, very thick, draw=red, canvas is yz plane at x=\x] (-\y,0) rectangle (\y,\x);

\begin{scope}[tdplot_rotated_coords] \filldraw [fill=cyan!40, very thick, draw=blue, canvas is yz plane at x=0, opacity=0.5] (\r,0) arc [start angle=0, end angle=180, x radius=\r, y radius=\r*sqrt(2)] -- cycle; \end{scope}

\begin{scope}[canvas is xz plane at y=0] \draw [semithick] (\x-0.25,0) -- (\x-0.25,0.25) -- (\x,0.25); \draw [semithick] (0.47,0) arc [start angle=0, end angle=45, radius=0.47]; \node [font=\small] at (22.5:0.65) {$\theta$}; \draw [red, very thick] (0,0) -- (\x,\x) -- (\x,0) (\x,0) -- (0,0); \end{scope}

\draw [thick, -Stealth] (0,0,-1) -- (0,0,5) node [left] {$z$}; \fill (\x,-\y,0) circle [radius=3pt]; \node [right, font=\small] at (\x,-\y,0) {$(x,-\sqrt{r^2-x^2},0)$}; \draw [semithick, font=\small] (\x,1.25,\x) --++ (2,0,2) node [above, inner sep=1mm] {$2\sqrt{r^2-x^2}$}; \end{tikzpicture} \end{document}

and its output: figure2 How can I complete this figure? Any help would be appreciated!

The photo mentioned in my comment in the @user241266 's answer

fig

Mohammadi
  • 357
  • 1
  • 11

3 Answers3

4

You can use fillbetween to construct some intersection segments between the vertical line that runs through the rightmost points.

\documentclass[14pt]{memoir}
\usepackage{tikz, tikz-3dplot,pgfplots}
\usetikzlibrary{arrows.meta,bbox}
\usepgfplotslibrary{fillbetween}
\begin{document}
\tdplotsetmaincoords{60}{-30}
\begin{tikzpicture}[tdplot_main_coords, scale=0.9]
\tdplotsetrotatedcoords{0}{45}{0}
\pgfmathsetmacro{\r}{3}
\pgfmathsetmacro{\x}{1.25}
\pgfmathsetmacro{\y}{sqrt(\r^2-\x^2)}
\draw [thick, -Stealth] (-1,0,0) -- (5,0,0) node [right] {$x$};
\draw [thick, -Stealth] (0,-5,0) -- (0,5,0) node [left] {$y$};
\filldraw[local bounding box=bb1,bezier bounding box,
    name path=lower,
    fill=cyan!40, very thick, draw=blue, canvas is xy plane at z=0, opacity=0.5] (0,-\r) arc [start angle=-90, end angle=90, radius=\r] -- cycle;
\filldraw [fill=red!50, very thick, draw=red, canvas is yz plane at x=\x] (-\y,0) rectangle (\y,\x);

\begin{scope}[tdplot_rotated_coords,local bounding box=bb2,bezier bounding box] \filldraw [fill=cyan!40, very thick, draw=blue, canvas is yz plane at x=0, name path global=upper,opacity=0.5] (\r,0) arc [start angle=0, end angle=180, x radius=\r, y radius=\r*sqrt(2)] -- cycle; \end{scope} \path[name path=aux1] (bb1.south east) -- (bb2.north east);

\path[%draw=magenta,thick,->, intersection segments={ of=lower and aux1, sequence={L1--R2} },name path=lowarc];

\path[%draw=orange,thick,->, fill=blue!60,opacity=0.5, intersection segments={ of=lowarc and upper, sequence={L*--R2} },name path=lowarc];

\begin{scope}[canvas is xz plane at y=0] \draw [semithick] (\x-0.25,0) -- (\x-0.25,0.25) -- (\x,0.25); \draw [semithick] (0.47,0) arc [start angle=0, end angle=45, radius=0.47]; \node [font=\small] at (22.5:0.65) {$\theta$}; \draw [red, very thick] (0,0) -- (\x,\x) -- (\x,0) (\x,0) -- (0,0); \end{scope}

\draw [thick, -Stealth] (0,0,-1) -- (0,0,5) node [left] {$z$}; \fill (\x,-\y,0) circle [radius=3pt]; \node [right, font=\small] at (\x,-\y,0) {$(x,-\sqrt{r^2-x^2},0)$}; \draw [semithick, font=\small] (\x,1.25,\x) --++ (2,0,2) node [above, inner sep=1mm] {$2\sqrt{r^2-x^2}$}; \end{tikzpicture} \end{document}

enter image description here

  • @@Beacause the bbox library is not installed on my system, I can not run your code. I will try to install it and test this code. I have a question. Given that my TeXLive version is for 2018, will all libraries be installed automaically if I update to the latest version? – Mohammadi May 05 '21 at 00:57
  • @Mohammadi Yes. You can (probably) also use the version from CTAN at https://ctan.org/pkg/tikz-bbox?lang=en. P.S. Via Google one can also find https://topanswers.xyz/tex?q=1659#a1894 –  May 05 '21 at 00:59
  • @user241266 I was able to compile your code by copying pgflibrarybbox.code.tex to the folder containing the code, but the output is different from what you uploded. I uploded the output photo in the question. What is wrong? – Mohammadi May 08 '21 at 02:37
4

It's almost always difficult to draw 3d revolution solids in tikz. But if one can use isometric perspective the cylinder is an easy exception. That is because we know where is the tangent points between the ellipses and their "limit" generatrix. Such points (T1 and T2 in my example) are at 45 degrees w.r.t the axis in the coordinate planes.

With that in mind we can do the following (the points P, T1 and T2 are drawn only as a reference, they can be erased or commented).

\documentclass[border=2mm]{standalone}
\usepackage    {tikz}
\usetikzlibrary{3d}

\pgfmathsetmacro\ip{0.5*sqrt(3)} % isometric perspective factor \tikzset% {% styles cylinder/.style= {fill=cyan,fill opacity=0.5}, rectangle/.style={fill=red!50,fill opacity=0.5}, reclines/.style= {draw=red!75!black,dashed}, }

\begin{document} \begin{tikzpicture}[scale=0.75,line cap=round,line join=round,semithick,% x={({\ip cm,0.5 cm})},y={(-\ip cm,0.5 cm)},z={(0 cm,1cm)}] % dimensions \def\r{3} % radius \def\h{3} % height \pgfmathsetmacro\th{atan(\h/\r)} % theta angle \def\px{1.25} % point P, x \pgfmathsetmacro\py{sqrt(\r\r-\px\px)} % point P, y \pgfmathsetmacro\pz{\pxtan(\th)} % point P, z \pgfmathsetmacro\tx {\rcos(45)} % tangent points T1 and T2, x \pgfmathsetmacro\ty{-\rsin(45)} % tangent points T1 and T2, y \pgfmathsetmacro\tz{\txtan(\th)} % tangent point T2, z \pgfmathsetmacro\a {\r/sin(\th)} % ellipse semimajor axis % x, y axis \draw[-latex] (-1,0,0) -- (3.5,0,0) node [above] {$x$}; \draw[-latex] (0,-\r-1,0) -- (0,\r+1,0) node [above] {$y$}; % bottom semicircle \begin{scope}[canvas is xy plane at z=0] \fill[cylinder] (0,\r) arc (90:-90:\r) -- cycle; \draw[blue,dashed] (0,\r) arc (90:-45:\r); \draw[blue] (\tx,\ty) arc (-45:-90:\r); \end{scope} % rectangle \begin{scope}[canvas is yz plane at x=\px] \fill[rectangle] (-\py,0) rectangle (\py,\pz); \draw[reclines] (\py,\pz) -- (\py,0) -- (-\py,0); \end{scope} % triangle \begin{scope}[canvas is xz plane at y=0] \draw[reclines] (0,0) -- (\px,0) -- (\px,\pz); \draw[thin] (\px-0.2,0) |- (\px,0.2); \draw(0.3,0) arc (0:\th:0.3) node [right,yshift=0.15cm] {$\theta$}; \end{scope} % cylindric surface \fill[cylinder] {[canvas is xy plane at z=0] (0,-\r) arc (-90:-45:\r)} -- (\tx,\ty,\tz) {[rotate around y=-\th,canvas is xy plane at z=0] arc (-45:-90:\a cm and \r cm)}; \draw[blue] (\tx,\ty,0) -- (\tx,\ty,\tz); % top semiellipse \draw[blue,cylinder,rotate around y=-\th,canvas is xy plane at z=0] (0,\r) arc (90:-90:\a cm and \r cm) -- cycle; \draw[red!75!black] (0,0,0) -- (\px,0,\pz); \draw[red!75!black] (\px,-\py,0) -- (\px,-\py,\pz) -- (\px,\py,\pz); % points (for reference), erase or comment them \fill (\px,\py,\pz) circle (1.5pt) node [left] {$P$}; \fill (\tx,\ty,0) circle (1.5pt) node [right] {$T_1$}; \fill (\tx,\ty,\tz) circle (1.5pt) node [right] {$T_2$}; % z axes \draw[-latex] (0,0,-1) -- (0,0,\h) node [above] {$z$}; \end{tikzpicture} \end{document}

enter image description here

Juan Castaño
  • 28,426
  • Thank you for your answer, but I am looking for a general and comprehensive solution to such figures. – Mohammadi May 06 '21 at 01:35
  • Excuse me, I just read your code exactly. That is very professional and I learned a lot from that. I have some questions. 1. How to change point of view? 2. What does the isometric perspective factor do? 3.Please explain what x={({\ip cm,0.5 cm})},y={(-\ip cm,0.5 cm)},z={(0 cm,1cm)} does? – Mohammadi May 08 '21 at 04:03
  • @Mohammadi, that code set the direction an the scale of the x, y, z axes. For example x axis is defined with the "unitary" vector (0.866,0.5) because \ip=0.866.... In this post I explain the topic a little: https://tex.stackexchange.com/questions/593344/how-to-rotate-a-3d-axis-some-options-of-the-axis-doesnt-work/593507#593507. You can change the point of view changing these vectors (or with tikz-3dplot package), but then you'll need to calculate again the generatix points T1 and T2, and it will be difficult. That's why I use isometric perspective, if possible. – Juan Castaño May 08 '21 at 05:12
0

Thanks to @Juan Castaño and @user241266's answer, I was able to find another way to draw the cylindrical section by using the definition of cylindrical coordinates on page 137 of the TikZ manual,

% definition of cylindrical coordinates
\makeatletter
\define@key{cylindricalkeys}{angle}{\def\myangle{#1}}
\define@key{cylindricalkeys}{radius}{\def\myradius{#1}}
\define@key{cylindricalkeys}{z}{\def\myz{#1}}
\tikzdeclarecoordinatesystem{cylindrical}%
{%
\setkeys{cylindricalkeys}{#1}%
\pgfpointadd{\pgfpointxyz{0}{0}{\myz}}{\pgfpointpolarxy{\myangle}{\myradius}}
}

And I used the \foreach. MWE:

\documentclass[12pt, border=1mm]{standalone}
\usepackage{tikz, tikz-3dplot}
\usetikzlibrary{arrows.meta}
\makeatletter
\define@key{cylindricalkeys}{angle}{\def\myangle{#1}}
\define@key{cylindricalkeys}{radius}{\def\myradius{#1}}
\define@key{cylindricalkeys}{z}{\def\myz{#1}}
\tikzdeclarecoordinatesystem{cylindrical}%
{%
\setkeys{cylindricalkeys}{#1}%
\pgfpointadd{\pgfpointxyz{0}{0}{\myz}}{\pgfpointpolarxy{\myangle}{\myradius}}
}
\begin{document}
\tdplotsetmaincoords{60}{-30}
\begin{tikzpicture}[tdplot_main_coords, scale=0.9, line cap=round, line join=round]
\tdplotsetrotatedcoords{0}{45}{0}
\pgfmathsetmacro{\r}{3}
\pgfmathsetmacro{\x}{1.25}
\pgfmathsetmacro{\y}{sqrt(\r^2-\x^2)}
\draw [thick, -Stealth] (-1,0,0) -- (5,0,0) node [right] {$x$};
\draw [thick, -Stealth] (0,-5,0) -- (0,5,0) node [left] {$y$};
\filldraw [fill=cyan!40, very thick, draw=blue, canvas is xy plane at z=0, opacity=0.5] (0,-\r) arc [start angle=-90, end angle=90, radius=\r] -- cycle;
\foreach \a in {-90,-88,...,88}
\fill [cyan!67, opacity=2/3] (cylindrical cs:angle=\a, radius=\r, z=0) -- (cylindrical cs:angle=\a+2, radius=\r, z=0) -- (cylindrical cs:angle=\a+2, radius=\r, z={3*abs(cos(\a+2))}) -- (cylindrical cs:angle=\a, radius=\r, z={3*abs(cos(\a))}) -- (cylindrical cs:angle=\a, radius=\r, z=0);
\filldraw [fill=red!50, very thick, draw=red, canvas is yz plane at x=\x] (-\y,0) rectangle (\y,\x);
\draw [thick, -Latex, canvas is xy plane at z=0] (0,0) -- (\x,-\y);
\node [left, font=\small] at (0,\r,0) {$r$};
\node [left, font=\small] at (0,-\r,0) {$-r$};

\begin{scope}[tdplot_rotated_coords] \filldraw [fill=cyan!40, very thick, draw=blue, canvas is yz plane at x=0, opacity=0.5] (\r,0) arc [start angle=0, end angle=180, x radius=\r, y radius=\r*sqrt(2)] -- cycle; \end{scope}

\begin{scope}[canvas is xz plane at y=0] \draw [semithick] (\x-0.25,0) -- (\x-0.25,0.25) -- (\x,0.25); \draw [semithick] (0.47,0) arc [start angle=0, end angle=45, radius=0.47]; \node [font=\small] at (22.5:0.65) {$\alpha$}; \draw [red, very thick] (0,0) -- (\x,\x) -- (\x,0) (\x,0) -- (0,0); \end{scope}

\draw [thick, -Stealth] (0,0,-1) -- (0,0,5) node [left] {$z$}; \fill (\x,-\y,0) circle [radius=2.75pt]; \node [right, font=\small] at (\x,-\y,0) {$(x,-\sqrt{r^2-x^2},0)$}; \draw [semithick, font=\small] (\x,1.25,\x) --++ (2,0,2) node [above, inner sep=1mm] {$2\sqrt{r^2-x^2}$}; \path [canvas is xy plane at z=0, font=\small] (0,0) -- node [sloped,midway, above, inner sep=1mm] {$r$} (\x,-\y); \path [thick, -Latex, canvas is xy plane at z=0, font=\small] (0,0) -- node [sloped, midway, below, inner sep=1mm] {$x$} (1.3*\x,0); \end{tikzpicture} \end{document}

output:

wedge

Mohammadi
  • 357
  • 1
  • 11