如何使用 pgfplots 或 TikZ 绘制一个有孔的球体?

如何使用 pgfplots 或 TikZ 绘制一个有孔的球体?

我想画一个有孔的球体,如下所示:

有孔球体

到目前为止,我所做的是在 Inkscape 中创建所需形状的轮廓并导出为图片pstricks

自动生成的代码如下

\documentclass[10pt]{article}
\usepackage{pstricks}
\usepackage{auto-pst-pdf}
\begin{document}
\psset{xunit=.5pt,yunit=.5pt,runit=.5pt}
\begin{pspicture}(286.29050428,360.71140079)
{
 \pscustom[linewidth=2.83464567,linecolor=black]
 {
  \newpath
  \moveto(139.52774929,302.33545622)
  \curveto(180.48641764,305.30507638)(198.90888567,270.4308578)(199.85526803,242.00794504)
  \curveto(200.1155263,234.19166284)(186.99812409,224.51682819)(184.40553827,224.35110725)
  \lineto(104.9497852,222.87970693)
  \curveto(88.89492283,226.469763)(86.61774992,248.37367481)(89.50005543,260.40047544)
  \curveto(96.87234898,283.72077654)(115.91783433,300.07697685)(139.52774929,302.33545622)
 \closepath
\moveto(257.23997858,231.70812772)
  \curveto(263.21746016,212.03697559)(275.69856756,194.81507449)(280.78243276,176.53051764)
  \curveto(292.2452863,136.57533276)(278.2086463,87.90979575)(255.03287811,56.6111841)
  \curveto(229.44509858,25.45612268)(188.65669795,4.73497622)(151.29897071,1.4335589)
  \curveto(97.6206652,0.66208174)(49.36511244,27.44230221)(21.81552756,69.11809433)
  \curveto(-15.19498205,130.75954693)(4.53473008,178.71026567)(30.64394457,226.55821528)
  \curveto(31.47129827,272.50697496)(39.60579402,316.15887796)(77.72883024,338.38482819)
  \curveto(115.85187024,360.61077843)(151.91151118,367.26575166)(193.96965921,347.94894536)
  \curveto(242.3634822,326.81267827)(253.5873222,273.83813969)(257.23997858,231.70812772)
  \closepath
 }
}
\end{pspicture}
\end{document}

这将创建如下所示的图片。

在此处输入图片描述

我该如何给它着色以创建类似于上面显示的内容?有没有其他方法可以使用其他包(如 TikZ)来实现这一点?

答案1

这或许能给你一个开始。

\documentclass[tikz,border=3.14mm]{standalone}
% from https://tex.stackexchange.com/a/76216/121799
\tikzset{clip even odd rule/.code={\pgfseteorule}} 
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\myangleA}{140}
 \path (\myangleA:3) + (0,1) coordinate (aux);
 \clip[clip even odd rule,draw]
 ([xshift=8mm,yshift=-2mm]aux) 
   arc(180:0:{3*cos(180-\myangleA)-0.3*(1+cos(\myangleA))-0.8})
   -- ++ (0,-0.3) arc(0:-90:0.3) -- 
   ++({-3*cos(180-\myangleA)+0.6*(1+cos(\myangleA))},0) 
   arc(-90:-180:0.4) -- cycle 
  (aux)
  arc(180:0:{3*cos(180-\myangleA)-0.3*(1+cos(\myangleA))}) 
  -- ++ (0,-1) arc(180:360-140:0.3)
  arc(180-\myangleA:\myangleA-360:3)
  arc(\myangleA-180:0:0.3)
  --  cycle  ;
  \shade[ball color=gray] (2,-2) circle [radius=8cm];
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容