答案1
这篇文章的目的是定义一种绘制球体一部分的样式。更具体地说,它将绘制一个纬度和经度在一定范围内的线段。绘制这个线段很简单,只需说
\draw[thin,fill=white,fill opacity=0.6,
sphere segment={phi from 60 to 120 and theta from -10 to 50 and radius 4}];
其中 phi 和 theta 是经度和纬度参数。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d}
\begin{document}
\tdplotsetmaincoords{70}{30}
\begin{tikzpicture}[tdplot_main_coords,bullet/.style={fill,circle,inner
sep=1pt},sphere segment/.style args={%
phi from #1 to #2 and theta from #3 to #4 and radius #5}{insert path={%
plot[variable=\x,smooth,domain=#2:#1]
(xyz spherical cs:radius=#5,longitude=\x,latitude=#3)
-- plot[variable=\x,smooth,domain=#3:#4]
(xyz spherical cs:radius=#5,longitude=#1,latitude=\x)
--plot[variable=\x,smooth,domain=#1:#2]
(xyz spherical cs:radius=#5,longitude=\x,latitude=#4)
-- plot[variable=\x,smooth,domain=#4:#3]
(xyz spherical cs:radius=#5,longitude=#2,latitude=\x)}},
>=stealth,declare function={f(\x)=exp(-2+0.5*\x);}]
\draw[thick] (0,0,0) -- (4,0,0);
\draw[thick,orange] plot[variable=\x,smooth,domain=0:4] ({\x},0,{f(\x)});
% note: this domain is an approximation, it should really be
% from 0 to xmax where xmax is the solution of x^2+f(x)^2=4^2
\draw[thin,fill=white,fill opacity=0.6,
sphere segment={phi from 60 to 120 and theta from -10 to 50 and radius 4}] ;
\draw[thick,->] (4,0,0) -- (6,0,0);
\draw[thick,orange] plot[variable=\x,smooth,domain=4:6] ({\x},0,{f(\x)});
% should be xmax:6
\end{tikzpicture}
\end{document}
或者 Henri Menke 的版本,他建议使用以下语法
\clip[sphere segment={r=4, phi=60:120, theta=-10:50}];
剪辑用于剪切球体的一部分。(球体只是一个圆圈,tdplot_screen_coordinate
因为任何球体的正交投影在屏幕坐标中都是一个圆圈。很可能我重新发明了一些东西……)
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d}
\tikzset{3d stuff/.is family,
3d stuff/.cd,
parse domain/.code args={#1:#2}{\def\xmin{#1}\def\xmax{#2}},
sphere segment/.is family,
sphere segment/.cd,
r/.initial=1,
phi/.initial=0:30,
theta/.initial=0:30
}
\begin{document}
\tdplotsetmaincoords{70}{30}
\begin{tikzpicture}[tdplot_main_coords,bullet/.style={fill,circle,inner
sep=1pt},sphere segment/.style={
/utils/exec=\tikzset{3d stuff/sphere segment/.cd,#1}%
\pgfkeys{/tikz/3d stuff/parse domain/.expanded=\pgfkeysvalueof{/tikz/3d stuff/sphere segment/phi}}
\edef\phimin{\xmin}
\edef\phimax{\xmax}
\pgfkeys{/tikz/3d stuff/parse domain/.expanded=\pgfkeysvalueof{/tikz/3d stuff/sphere segment/theta}}
\edef\thetamin{\xmin}
\edef\thetamax{\xmax},
insert path={%
plot[variable=\x,smooth,domain=\phimax:\phimin]
(xyz spherical cs:radius=\pgfkeysvalueof{/tikz/3d stuff/sphere segment/r},
longitude=\x,latitude=\thetamin)
-- plot[variable=\x,smooth,domain=\thetamin:\thetamax]
(xyz spherical cs:radius=\pgfkeysvalueof{/tikz/3d stuff/sphere segment/r},
longitude=\phimin,latitude=\x)
--plot[variable=\x,smooth,domain=\phimin:\phimax]
(xyz spherical cs:radius=\pgfkeysvalueof{/tikz/3d stuff/sphere segment/r},
longitude=\x,latitude=\thetamax)
-- plot[variable=\x,smooth,domain=\thetamax:\thetamin]
(xyz spherical cs:radius=\pgfkeysvalueof{/tikz/3d stuff/sphere segment/r},
longitude=\phimax,latitude=\x) --cycle}},
>=stealth,declare function={f(\x)=exp(-2+0.5*\x);}]
\draw[thick] (0,0,0) -- (4,0,0);
\draw[thick,orange] plot[variable=\x,smooth,domain=0:4] ({\x},0,{f(\x)});
% note: this domain is an approximation, it should really be
% from 0 to xmax where xmax is the solution of x^2+f(x)^2=4^2
\begin{scope}
\clip[sphere segment={r=4, phi=60:120, theta=-10:50}];
\shade[tdplot_screen_coords,ball color=gray!30,opacity=0.5]
(0,0) circle[radius=4];
\end{scope}
\draw[thick,->] (4,0,0) -- (6,0,0);
\draw[thick,orange] plot[variable=\x,smooth,domain=4:6] ({\x},0,{f(\x)});
% should be xmax:6
\end{tikzpicture}
\end{document}