使用 TIKZ 制作 3D 球体

使用 TIKZ 制作 3D 球体

我正在尝试重现这张图片,但不幸的是我不知道如何制作透明球体。有人有想法吗?谢谢 David

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x={(-10:1cm)},y={(90:1cm)},z={(210:1cm)}, scale=.75]
\foreach \i in {-5,...,-1}{
    \filldraw [fill=red, nearly transparent] (\i,2,3) -- (\i,-2,3) -- (\i,-2,-3) -- (\i,2,-3) -- cycle;
    \draw [] (\i,2,3) -- (\i,-2,3) -- (\i,-2,-3) -- (\i,2,-3) -- cycle;
}             
\filldraw [fill=white] (0,3,-4) -- (0,3,4) -- (0,-3,4);
\filldraw [fill=white] (.5,3,4) -- (.5,-3,4) -- (.5,-3,-4) -- (.5,3,-4) -- cycle;
\draw (0,3,4) -- (.5,3,4);
\draw (0,-3,4) -- (.5,-3,4);
\draw (0,3,-4) -- (.5,3,-4);
\draw [fill=black] (.5,0,0) [x={(0,0,1)}] circle (.1);
\foreach \i in {0.5,2,3.5}{
    \draw (.5,0,0) [x={(0,0,1)}] circle (\i);
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

作为初学者,请研究以下代码;它应该能让你很好地了解如何在正确伪造的 3D 中绘制球体,摘自这里

\documentclass[tikz,border=10pt]{standalone}
\begin{document}

%spherical coordinate system around y axis
\makeatletter
\define@key{y sphericalkeys}{radius}{\def\myradius{#1}}
\define@key{y sphericalkeys}{theta}{\def\mytheta{#1}}
\define@key{y sphericalkeys}{phi}{\def\myphi{#1}}
\tikzdeclarecoordinatesystem{y spherical}{% %%%rotation around y
    \setkeys{y sphericalkeys}{#1}%
    \pgfpointxyz{\myradius*sin(\mytheta)*cos(\myphi)}{\myradius*cos(\mytheta)}{\myradius*sin(\mytheta)*sin(\myphi)}}

%spherical coordinate system around z axis
\makeatletter
\define@key{z sphericalkeys}{radius}{\def\myradius{#1}}
\define@key{z sphericalkeys}{theta}{\def\mytheta{#1}}
\define@key{z sphericalkeys}{phi}{\def\myphi{#1}}
\tikzdeclarecoordinatesystem{z spherical}{% %%%rotation around z
    \setkeys{z sphericalkeys}{#1}%
    \pgfpointxyz{\myradius*sin(\mytheta)*cos(\myphi)}{\myradius*sin(\mytheta)*sin(\myphi)}{\myradius*cos(\mytheta)}}

\begin{tikzpicture}%[z={(0.5cm,-0.3cm)}]
%Pinhole
\draw (0,0,0) circle (1pt);
%Innermost sphere
\foreach \aangle in {-90,-80,...,80}{
    \foreach \bangle in {-90,-80,...,80}{
    \fill[blue!20, fill opacity = 0.5] (z spherical cs: radius = 1, phi=\aangle, theta=\bangle) -- (z spherical cs: radius = 1, phi=\aangle+10, theta=\bangle) -- (z spherical cs: radius = 1, phi=\aangle+10, theta=\bangle+10) -- (z spherical cs: radius = 1, phi=\aangle, theta=\bangle+10)--cycle;
    }}
%second sphere
\foreach \aangle in {-90,-80,...,80}{
    \foreach \bangle in {-90,-80,...,80}{
    \fill[blue!20, fill opacity = 0.5] (z spherical cs: radius = 2, phi=\aangle, theta=\bangle) -- (z spherical cs: radius = 2, phi=\aangle+10, theta=\bangle) -- (z spherical cs: radius = 2, phi=\aangle+10, theta=\bangle+10) -- (z spherical cs: radius = 2, phi=\aangle, theta=\bangle+10)--cycle;
    }}
%sphere with ends
\foreach \aangle in {0,10,...,170}{
    \foreach \bangle in {30,40,...,140}{
    \fill[blue!20, fill opacity = 0.5] (y spherical cs: radius = 3, phi=\aangle, theta=\bangle) -- (y spherical cs: radius = 3, phi=\aangle+10, theta=\bangle) -- (y spherical cs: radius = 3, phi=\aangle+10, theta=\bangle+10) -- (y spherical cs: radius = 3, phi=\aangle, theta=\bangle+10)--cycle;
    }}
\end{tikzpicture}
\end{document}

这应该会把你推向正确的方向。请注意,如果使用更精细的迭代绘制,代码会非常慢,并且会很快超出 TeX 的容量。每个球体由 10°x10° 大小的平面组成,用两个foreach循环进行迭代。下图;请注意不透明度如何巧妙地处理自重叠。您可以通过 tikzpicture 环境的键稍微“旋转”球体[z={(coordinate, coordinate)}],这与画布上的坐标(coordinate, coordinate)相关。x,y

重叠领域

相关内容