TikZ-PGF 3D – Calculating Area Under 3D Surfaces Using TikZ

3dtikz-pgf

I'm trying to draw the area under a cone (sqrt(x^2+y^2)), insider a cylinder (x^2+y^2 = 2x) using tikz.

My problem is that the area is not being well defined. My code produces this weird shapes:

View: {130}{30}

enter image description here

View: {30}{30}

enter image description here

I'm trying to get something like this:

enter image description here

\documentclass[10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[bullet/.style={circle,fill,inner sep=0.5pt}, declare function={f(\x,\y)=(\x^2+\y^2)^0.5;}]
 \begin{axis}[view={30}{30},colormap/blackwhite,axis lines=middle,%
    zmax=2.5,zmin=0,xmin=-2.5,xmax=3.5,ymin=-2.5,ymax=2.5,%
    xlabel=$x$,ylabel=$y$,zlabel=$z$,
    xtick=\empty,ytick=\empty,ztick=\empty]
    
  \addplot3[surf,shader=interp,domain=0:2,domain y=-2:2,opacity=0.4] {f(x,y)};
  
  %\draw [fill=white!50!blue, opacity=0.3, smooth, samples=100, domain=0:2] plot(\x, {sqrt(2*\x-\x*\x)}) -- plot[domain=2:0] (\x,{-sqrt(2*\x-\x*\x)}) -- cycle; 
  
  \draw [blue, domain=0:2, smooth, variable=\x] plot (\x, {sqrt(2*\x-\x*\x)});
  \draw [blue, domain=0:2, smooth, variable=\x] plot (\x, {-sqrt(2*\x-\x*\x)});
  
  \draw [fill=white!50!blue, opacity=0.3, smooth, samples=100, domain=0:2] plot(\x, {sqrt(2*\x-\x*\x)},0) -- plot[domain=2:0] (\x,{sqrt(2*\x-\x*\x)},{f(\x,{sqrt(2*\x-\x*\x)})}) -- cycle; 
  
   \draw [red] (1,2) node[]{$D$};
 \end{axis}

\end{tikzpicture}
\end{document}

Best Answer

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[declare function={f(\x,\y)=(\x^2+\y^2)^0.5;}]
 \begin{axis}[
view={30}{30},
colormap/blackwhite,
axis lines=middle,
xmin=-2.5, xmax=3.5,
ymin=-2.5, ymax=2.5,
zmax=2.5, zmin=0,
xtick=\empty, ytick=\empty, ztick=\empty,
]
\addplot3[surf,shader=interp,domain=0:2,domain y=-2:2,opacity=0.4] {f(x,y)};
\draw[blue] (1,0,0) circle[radius=1];  
\draw[red] plot[domain=0:360, variable=\ang, smooth] ( {cos(\ang)+1} , {sin(\ang)} , {f({cos(\ang)+1},{sin(\ang)})} );
\draw[cyan] ( {cos(210)+1} , {sin(210)} , 0 ) -- ( {cos(210)+1} , {sin(210)} , {f({cos(210)+1},{sin(210)})} );
\draw[cyan] ( {cos(30)+1} , {sin(30)} , 0 ) -- ( {cos(30)+1} , {sin(30)} , {f({cos(30)+1},{sin(30)})} );
\end{axis}
\end{tikzpicture}
\end{document}

Surface graph with fill lines

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[declare function={f(\x,\y)=(\x^2+\y^2)^0.5;}]
 \begin{axis}[
view={30}{30},
colormap/blackwhite,
axis lines=middle,
xmin=-2.5, xmax=3.5,
ymin=-2.5, ymax=2.5,
zmax=2.5, zmin=0,
xtick=\empty, ytick=\empty, ztick=\empty,
]
\addplot3[surf,shader=interp,domain=0:2,domain y=-2:2,opacity=0.4] {f(x,y)};
\draw[blue] plot[domain=0:360, variable=\ang, smooth] ( {cos(\ang)+1} , {sin(\ang)} , {f({cos(\ang)+1},{sin(\ang)})} );
\draw[blue] (1,0,0) circle[radius=1];  
\fill[white!50!blue, opacity=0.3, smooth, samples=100]
( {cos(210)+1} , {sin(210)} , 0 ) -- ( {cos(210)+1} , {sin(210)} , {f({cos(210)+1},{sin(210)})} ) -- 
plot[domain=210:390, variable=\ang, smooth] ( {cos(\ang)+1} , {sin(\ang)} , {f({cos(\ang)+1},{sin(\ang)})} ) --
( {cos(30)+1} , {sin(30)} , 0 ) -- ( {cos(30)+1} , {sin(30)} , {f({cos(30)+1},{sin(30)})} ) --
plot[domain=390:210, variable=\ang, smooth] ( {cos(\ang)+1} , {sin(\ang)} , 0 );
\end{axis}
\end{tikzpicture}
\end{document}

Surface graph with fill