4

I want to draw a cylindrical hole in a sphere with Tikz which the picture is below. wanted By searching the site, I could to find two useful links. By Using the Henri Menke's answer to Gray shaded sphere with tikz-3dplot, the sphere can be drawn.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
  [
    axis equal, axis lines=none,
    domain=0:180, samples=40,
    y domain=0:360, samples y=40,
    colormap/blackwhite,
    view={100}{10},
  ]
  \addplot3
  [
    surf,
    z buffer=sort,
    shader=flat,
    point meta={acos(z/sqrt(x*x+y*y+z*z)) + atan2(y,x)}
  ] (
      {sin(x)*cos(y)},
      {sin(x)*sin(y)},
      {cos(x)}
  );
  \end{axis}
\end{tikzpicture}
\end{document}

figure1 And by changing the Tathagat agrawal's answer to Draw cylinder and sphere using PGFPlots, I was able to draw the cylindrical part.

\documentclass{standalone} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.9, colormap/blackwhite}

\begin{document} \begin{tikzpicture} \begin{axis}[ axis equal, axis lines=middle, view={110}{25}, domain=0:5, y domain=0:2*pi, xmin=-1.5, xmax=1.5, ymin=-1.5, ymax=1.5, zmin=-2, zmax=2, samples=35, xlabel=$x$, ylabel=$y$, zlabel={$z$}, ] \addplot3 [surf, z buffer=sort, opacity=0.6, domain=0:1, color=black!90] ( {cos(deg(y))}, {sin(deg(y))}, {x} ); \addplot3[surf, domain=45:90, domain y=0:360, z buffer=sort, opacity=0.6, color=black!50] ( {sqrt(2)cos(y)cos(x)}, {sqrt(2)sin(y)cos(x)}, {sqrt(2)*sin(x)});

\addplot3 [surf, z buffer=sort, opacity=0.6, domain=0:1, color=black!90] ( {cos(deg(y))}, {sin(deg(y))}, {-x} ); \addplot3[surf, domain=45:90, domain y=0:360, z buffer=sort, opacity=0.6, color=black!50] ( {sqrt(2)cos(y)cos(x)}, {sqrt(2)sin(y)cos(x)}, {-sqrt(2)*sin(x)}); \end{axis} \end{tikzpicture} \end{document}

figure2 How to achieve the desired figure by combining thess two figures?

Mohammadi
  • 357
  • 1
  • 11

1 Answers1

14

This is an alternative made in tikz and using isometric perspective. I think that this perspective and the shaded fillings show the picture clearly, but is not exactly the one used in the original drawing.

\documentclass[border=2mm]{standalone}
\usepackage    {tikz}
\usetikzlibrary{3d}
\usetikzlibrary{calc}
\usetikzlibrary{babel} % for issues with some babel packages

% isometric axes \pgfmathsetmacro\xx{1/sqrt(2)} \pgfmathsetmacro\xy{1/sqrt(6)} \pgfmathsetmacro\zy{sqrt(2/3)}

\begin{document} \begin{tikzpicture}[line cap=round,line join=round,% x={({-\xx cm,-\xy cm})},y={(\xx cm,-\xy cm)},z={(0 cm,\zy cm)}] % dimensions \def\a{2} % sphere radius \def\s{6} % cylinder vertical shift \pgfmathsetmacro\l{0.7\a} % square (plane) semi-length \pgfmathsetmacro\w{\a} % square (plane) semi-width \pgfmathsetmacro\h{0.5sqrt(3)\a} % cylinder semi-height \pgfmathsetmacro\r{0.5\a} % cylinder radius % coordinates \coordinate (T1) at ($(-45:\r)+(0,0,\s+\h)$); % cylinder tangent point (top left) \coordinate (T2) at ($(135:\r)+(0,0,\s+\h)$); % cylinder tangent point (top right) \coordinate (Y1) at ($( 45:\r)+(0,0,\h)$); % y-axis visibility \coordinate (Y2) at ($( 45:\r)+(0,0,\s-\h)$); % y-axis visibility % bottom plane \draw[canvas is xy plane at z=-\h,fill=orange!50] (-\l,-\w) rectangle (\l,\w); % sphere \draw[shading=ball,ball color=blue!50] (0,0,0) circle (\a cm); % top plane \begin{scope}[canvas is xy plane at z=\h] \draw[fill=orange,fill opacity=0.5] (-\l,-\w) rectangle (\l,\w); \draw[left color=gray,right color=white] (0,0) circle (\r); \end{scope} % cylinder \draw[blue,dashed] ($(T1)-(0,0,2\h)$) --++ (0,0,2\h-\s); \draw[blue,dashed] ($(T2)-(0,0,2\h)$) --++ (0,0,2\h-\s); \draw[left color=white,right color=gray] (T1) --++ (0,0,-2\h) {[canvas is xy plane at z=\s-\h] -- (-45:\r) arc (-45:135:\r)} -- (T2) {[canvas is xy plane at z=\s+\h] -- (135:\r) arc (135:-45:\r)}; \begin{scope} \clip[canvas is xy plane at z=\s+\h] (0,0) circle (\r); \draw[thick,shading=ball,ball color=blue!50] (0,0,\s) circle (\a cm); \end{scope} \draw[canvas is xy plane at z=\s+\h] (0,0) circle (\r); % labels and auxiliary lines \node at (\l,-\w,-\h) [left] {$y=-\frac{a\sqrt{3}}{2}$}; \node at (\l,-\w, \h) [left] {$y=\frac{a\sqrt{3}}{2}$}; \draw[dashed] (-\l ,\w , -\h) --++ (-1,0,0); \draw[dashed] (-\l ,\w , \h) --++ (-1,0,0); \draw[<->] (-\l-1,\w , -\h) --++ (0,0,2\h) node[midway,right] {$a\sqrt{3}$}; \draw[dashed] (0 ,\r ,\s-\h) --++ (0,1,0); \draw[dashed] (0 ,\r ,\s+\h) --++ (0,1,0); \draw[<->] (0 ,\r+1,\s-\h) --++ (0,0,2\h) node[midway,right] {$a\sqrt{3}$}; % axes \draw[red] (Y1) -- (Y2); \draw[red,-latex] (\a,0,0) -- (2\a,0,0) node [left] {$z$}; \draw[red,-latex] (0,\a,0) -- (0,2*\a,0) node [right] {$x$}; \draw[red,-latex] (0,0,\s+\a) --++ (0,0,\a) node [above] {$y$}; \draw[red,dashed] (0,0,0) -- (\a,0,0); \draw[red,dashed] (0,0,0) -- (0,\a,0); \draw[red,dashed] (0,0,0) -- (Y1); \draw[red,dashed] (Y2) -- (0,0,\s+\a); \fill[red] (0,0,0) circle (1pt); \fill[red] (\a,0,0) circle (1pt); \fill[red] (0,\a,0) circle (1pt); \fill[red] (0,0,\s+\a) circle (1pt); \fill[red] (0,0,\s-\a) circle (1pt); \end{tikzpicture} \end{document}

enter image description here Edit: As suggested by SebGlav I added some dashed lines. I added \usetikzlibrary{babel} too, just in case.

Juan Castaño
  • 28,426
  • 2
    +1 I think that the transparency you added is a serious improvement of the original picture. Maybe you could add dashed line from origin to the exit point of the axes, and alos into the cylinder. – SebGlav May 14 '21 at 10:55
  • @SebGlav, thanks for the suggestion. I think you are right, so I made an edit. – Juan Castaño May 14 '21 at 11:15
  • 2
    Very nice, great drawing. Plain and simple, customizable (Y) – SebGlav May 14 '21 at 11:34
  • I am totally agree with the comment of @SebGlav: +1. – Sebastiano May 14 '21 at 11:39
  • Very beautiful. I think if you draw the circle at the base of the cylinder, the shape will look more realistic. By adding this code: \begin{scope}[opacity=0.4] \clip[canvas is xy plane at z=\s-\h] (0,0) circle (\r); \draw[thick,shading=ball,ball color=blue!50] (0,0,\s-\h) circle (\a cm); \end{scope} – Mohammadi May 14 '21 at 13:55
  • @Mohammadi, you are probably right, but better to draw the "ball" outside the canvas and then clip it (as in the top base), else the canvas will be distorting the ball shading. – Juan Castaño May 14 '21 at 17:57
  • @JuanCastaño: Nice drawing. How can I produce this picture with x-axes drawn from origin to right. (i.e. usual xyz view)? It is not just a matter of drawing axes, but also calculations in these coordinates. – C.F.G Jul 04 '23 at 16:41
  • @C.F.G you'll need to redefine the axis (see where I define the 'isometric axes'). But that will mess the visibility so you'll need to draw again some parts, I'm afraid – Juan Castaño Jul 04 '23 at 16:49
  • 1
    @JuanCastaño: so I 'll ask it in new post. – C.F.G Jul 04 '23 at 16:52
  • @JuanCastaño: https://tex.stackexchange.com/q/690283/241755 – C.F.G Jul 04 '23 at 18:41