我想用 Tikz 在球体上画一个圆柱形孔,如下图所示。 通过搜索网站,我可以找到两个有用的链接。通过使用Henri Menke 对 tikz-3dplot 灰色阴影球体的回答,即可绘制球体。
\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}
并通过改变Tathagat agrawal 对使用 PGFPlots 绘制圆柱体和球体的回答,我就能画出圆柱形部分了。
\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}
答案1
这是用 tikz 制作的替代方案,使用了等距透视。我认为这种透视和阴影填充可以清晰地显示图片,但与原始绘图中使用的并不完全相同。
\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.5*sqrt(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}
编辑:根据 SebGlav 的建议,我添加了一些虚线。我\usetikzlibrary{babel}
也加了一些,以防万一。