2

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}

2 Answers 2

2
\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

2

A plain TikZ solution could be this:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{3d,calc,perspective}

\tikzset
{
  axis/.style={thick,-latex},
  cone back/.style={draw=brown,left color=gray!50,right color=brown},
  cone front/.style={draw=brown,left color=brown},
  cylinder front/.style={draw=blue,left color=blue!50,fill opacity=0.5},
  cylinder back/.style={draw=blue,left color=gray!50,right color=blue!50,fill opacity=0.5},
}

\begin{document}
\begin{tikzpicture}[isometric view,rotate around z=180,line cap=round,line join=round]
% coordinates
\coordinate (O)  at (0,0,0);
\coordinate (L1) at ($(1,0,0)+(315:1)$);
\coordinate (L2) at ($(L1)+(0,0,{sqrt(2+sqrt(2))})$);
\coordinate (L3) at ($(L1)+(0,0,3)$);
\coordinate (L4) at (3,0,3);
\coordinate (R1) at ($(1,0,0)+(135:1)$);
\coordinate (R2) at ($(R1)+(0,0,{sqrt(2-sqrt(2))})$);
\coordinate (R3) at ($(R1)+(0,0,3)$);
\coordinate (R4) at (0,3,3);
\coordinate (A)  at (2,0,2);
\coordinate (B)  at (intersection of L1--L3 and O--L4);
% axes
\draw[axis] (O) -- (4,0,0) node [left]  {\strut$x$};
\draw[axis] (O) -- (0,4,0) node [right] {\strut$y$};
% cone, back
\draw[cone back] (O) -- (R4) arc (90:360:3) -- cycle;
\draw[blue] plot[domain=135:315,samples=181] ({1+cos(\x)},{sin(\x)},{sqrt(2+2*cos(\x))});
% cylincer, back bottom
\draw[cylinder back] (L1) arc (315:135:1) -- (R3) arc (135:315:1) -- cycle;
% z-axis
\draw[axis] (O) -- (0,0,4) node [above] {$z$};
% cylincer, front top
\draw[cylinder front] (L3) arc (-45:135:1) -- (R2) --
 plot[domain=135:0,samples=136] ({1+cos(\x)},{sin(\x)},{sqrt(2+2*cos(\x))}) -- (B) -- cycle;
\draw[blue] plot[domain=315:360,samples=46] ({1+cos(\x)},{sin(\x)},{sqrt(2+2*cos(\x))});
% cone front
\draw[cone front] (O) -- (R4) arc (90:0:3) -- cycle;
% cylinder, front bottom
\draw[cylinder front] (B) -- (L1) arc (-45:135:1) -- (R2) --
 plot[domain=135:0,samples=136] ({1+cos(\x)},{sin(\x)},{sqrt(2+2*cos(\x))});
% to show the coordinates (remove this if you don't need it)
\foreach\j in {L,R} \foreach\i in {1,2,3,4} 
  \fill (\j\i) circle (0.75pt);
\foreach\j in {A,B}
  \fill (\j)   circle (0.75pt);
\end{tikzpicture}
\end{document}

And its output: enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .