如何使用 TikZ 在球体表面绘制带有褪色的挤压椭圆?

如何使用 TikZ 在球体表面绘制带有褪色的挤压椭圆?

理想情况下,我想绘制类似下图 (c) 部分的内容: 椭圆球
(来源:nbi.ku.dk)
。在这里,我将问题缩小到如何在球体表面上用渐变颜色绘制椭圆,如图 (b) 部分所示。(注:也许我可以跟进这个例子在球体表面画箭头,根据(b)完成(c)。

  1. 第一次尝试基本上是遵循一个简单的例子绘制球体,发现它不适用于当前版本的 TikZ。现在我卡在这一步。这是我第一次尝试完成这项工作的一部分的最少代码:

    
    \documentclass[border=2pt]{standalone} %\documentclass[tikz]{standalone}
    \usepackage{pgfplots}
    \usepackage{tikz}
    \usetikzlibrary{calc,decorations.pathmorphing,shapes,arrows,math}
    \begin{document}
    \usetikzlibrary{calc,fadings,decorations.pathreplacing}
    \newcommand\pgfmathsinandcos3{%
      \pgfmathsetmacro#1{sin(#3)}%
      \pgfmathsetmacro#2{cos(#3)}%
    }
    \newcommand\LongitudePlane3[current plane]{%
      \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
      \pgfmathsinandcos\sint\cost{#3} % azimuth
      \tikzset{#1/.style={cm={\cost,\sint*\sinEl,0,\cosEl,(0,0)}}}
    }
    \newcommand\LatitudePlane3[current plane]{%
      \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
      \pgfmathsinandcos\sint\cost{#3} % latitude
      \pgfmathsetmacro\yshift{\cosEl*\sint}
      \tikzset{#1/.style={cm={\cost,0,0,\cost*\sinEl,(0,\yshift)}}} %
    }
    \newcommand\DrawLongitudeCircle2{
      \LongitudePlane{\angEl}{#2}
      \tikzset{current plane/.prefix style={scale=#1}}
       % angle of "visibility"
      \pgfmathsetmacro\angVis{atan(sin(#2)*cos(\angEl)/sin(\angEl))} %
      \draw[current plane] (\angVis:1) arc (\angVis:\angVis+180:1);
      \draw[current plane,dashed] (\angVis-180:1) arc (\angVis-180:\angVis:1);
    }
    \newcommand\DrawLatitudeCircle2{
      \LatitudePlane{\angEl}{#2}
      \tikzset{current plane/.prefix style={scale=#1}}
      \pgfmathsetmacro\sinVis{sin(#2)/cos(#2)*sin(\angEl)/cos(\angEl)}
      % angle of "visibility"
      \pgfmathsetmacro\angVis{asin(min(1,max(\sinVis,-1)))}
      \draw[current plane] (\angVis:1) arc (\angVis:-\angVis-180:1);
      \draw[current plane,dashed] (180-\angVis:1) arc (180-\angVis:\angVis:1);
    }

    %% document-wide tikz options and styles

    \tikzset{%

    =latex, % option for nice arrows inner sep=0pt,% outer sep=2pt,% mark coordinate/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt, fill=black,circle}% }

    \begin{figure}[ht] \begin{center} \begin{tikzpicture} % MERC

    % Modified from Stereographic and cylindrical map projections % Author: Tomasz M. Trzeciak % Source: LaTeX-Community.org % http://www.latex-community.org/viewtopic.php?f=4&t=2111

    %% some definitions

    \def\R{3} % sphere radius \def\angEl{25} % elevation angle \def\angAz{-100} % azimuth angle \def\angPhiOne{-50} % longitude of point P \def\angPhiTwo{-35} % longitude of point Q \def\angBeta{33} % latitude of point P and Q

    %% working planes

    \pgfmathsetmacro\H{\R*cos(\angEl)} % distance to north pole \LongitudePlane[xzplane]{\angEl}{\angAz} \LongitudePlane[pzplane]{\angEl}{\angPhiOne} \LongitudePlane[qzplane]{\angEl}{\angPhiTwo} \LatitudePlane[equator]{\angEl}{0}

    %% draw background sphere

    \fill[ball color=white] (0,0) circle (\R); % 3D lighting effect %\fill[white] (0,0) circle (\R); % just a white circle \draw (0,0) circle (\R);

    %% characteristic points

    \coordinate (O) at (0,0); \coordinate[mark coordinate] (N) at (0,\H); \coordinate[mark coordinate] (S) at (0,-\H); \path[xzplane] (\R,0) coordinate (XE); \path[pzplane] (\angBeta:\R) coordinate (P); \path[pzplane] (\R,0) coordinate (PE); \path[qzplane] (\angBeta:\R) coordinate (Q); \path[qzplane] (\R,0) coordinate (QE);

    %% meridians and latitude circles

    % \DrawLongitudeCircle[\R]{\angAz} % xzplane % \DrawLongitudeCircle[\R]{\angAz+90} % yzplane \DrawLongitudeCircle[\R]{\angPhiOne} % pzplane %\DrawLongitudeCircle[\R]{\angPhiTwo} % qzplane \DrawLatitudeCircle[\R]{\angBeta} \DrawLatitudeCircle[\R]{0} % equator

    % shifted equator in node with nested call to tikz % (I didn't know it's possible) %\node at (0,1.6*\R) { \tikz{\DrawLatitudeCircle[\R]{0}} };

    %% draw lines and put labels

    %\draw (-\R,-\H) -- (-\R,2*\R) (\R,-\H) -- (\R,2*\R); %\draw[->] (O) -- +(0,1.5*\R) node[above] {$|0\rangle$}; \node[above=8pt] at (N) {$|0\rangle$};%{$\mathbf{N}$}; \node[below=8pt] at (S) {$|1\rangle$};%{$\mathbf{S}$}; \node[right=8pt] at (P) {$|\Phi \rangle$}; \draw[->] (O) -- (P); \draw[dashed] (O) -- (N); \draw[dashed] (XE) -- (O) -- (PE); %\draw[dashed] (O) -- (QE); %\draw[pzplane,->,thin] (0:0.5*\R) to[bend right=15] % node[midway,right] {$\beta$} (\angBeta:0.5*\R); \path[pzplane] (0.5*\angBeta:\R) ;%node[right] {$\hat{1}$}; \path[qzplane] (0.5*\angBeta:\R) ;%node[right] {$\hat{2}$}; \draw[equator,->,thin] (\angAz:0.5*\R) to[bend right=30] node[pos=0.4,above] {$\phi$} (\angPhiOne:0.5*\R); \draw[pzplane,->,thin] (90:0.5*\R) to[bend left=30] node[midway,right] {$\theta$} (\angBeta:0.5*\R); \draw[equator,->] (-90:\R) arc (-90:-70:\R) ;%node[below=0.3ex] {$x = a\phi$}; \path[xzplane] (0:\R) node[below] {$\theta=0$}; \path[xzplane] (\angBeta:\R);% node[below left] {$\beta=\beta_0$};

    \end{tikzpicture} \end{center} \caption{Ellipse on a Sphere} \label{fig:sphere} \end{figure} \end{document}

预览:在此处输入图片描述 如你所见,与原始示例(左下图),赤道上的两条虚线无法在我的修改版本中显示。即使我编译原始示例的代码,也会出现此错误。注意到我已经替换了风格风格。最重要的是,我接下来该怎么做才能在任意位置制作球体上的椭圆和渐变颜色?再次感谢。

  1. 第二次尝试是使用3dplot来完成球体,代码非常简单(有缺陷)。以下是代码:
    
    \documentclass[11pt]{standalone}
    %\usepackage[utf8]{inputenc}
    \usepackage{tikz,tikz-3dplot}
    \usetikzlibrary{arrows}
    \begin{document}
    \tdplotsetmaincoords{70}{135}
    \begin{tikzpicture}
    \begin{scope}[tdplot_main_coords, fill opacity=.7,>=latex,shape=circle]
    \pgfsetlinewidth{.1pt}]
    %draw sphere
    \tdplotsphericalsurfaceplot{36}{12}{1}{black!85!white}{blue!40!white}
    {\draw[color=black,thick,->]  (-1.5,0,0) --  (1.5,0,0)  node[anchor=north  east]{$x$};}
    {\draw[color=black,thick,->]  (0,-1.5,0) --  (0,1.5,0)  node[anchor=north  west]{$y$};}
    {\draw[color=black,thick,->]  (0,0,-1.5) --  (0,0,1.5)  node[anchor=south]{$z$};}
    \end{scope}
    \end{tikzpicture}
    \end{document}
    
    预览:球体三维绘图

我不知道接下来该如何画椭圆。您有什么想法吗?

答案1

这是获取所需图像的替代方法。我只是对地球上的路径进行了近似(没有数学正确的绘制)。将其视为简单的操作方法。

\documentclass[tikz, border=6mm]{standalone}

\usetikzlibrary{backgrounds}

\begin{document}
  \begin{tikzpicture}[x={(215:.75cm)},y={(-10:1cm)},z={(90:1cm)}]

    % Axes
    \begin{scope}[black!50, >=latex, ->, font=\scriptsize]
        \draw (0,0,0) -- ++(1.5,0,0) coordinate [label={0:$x$}];
        \draw (0,0,0) -- ++(0,1.5,0) coordinate [label={35:$y$}];
        \draw (0,0,0) -- ++(0,0,1.5) coordinate [label={90:$z$}];
    \end{scope}

    % Globe
    \begin{scope}[blue!50!white, samples=250, domain=0:2*pi]
        \draw plot ({cos(\x r)}, {sin(\x r)}, 0); % xy
        \draw plot ({cos(\x r)}, 0, {sin(\x r)}); % xz
        \draw plot (0, {cos(\x r)}, {sin(\x r)}); % yz
        \fill [opacity=.5] (0,0) circle (.85cm);
    \end{scope}

    % Plot around globe - just approximation!
    \begin{scope}[smooth, red, >=latex, ->, scale=.85]
        \draw [domain=0:.875] plot ({cos(\x r)},{sin(\x r)},0);
        \draw [domain=.19:pi/3.5] plot ({cos(\x r)},{cos(\x r)},{sin(\x r)}) coordinate (c-x);
    \end{scope}

    \begin{scope}[on background layer, smooth]
        % projection area with plot
        \fill [blue!50!white] (-4,-1.5,-1.5) -- (-4,1.5,-1.5) -- (-4,1.5,1.5) -- (-4,-1.5,1.5);
        \draw [black!25, very thin] (-4,0,-1.5) -- (-4,0,1.5);
        \draw [black!25, very thin] (-4,-1.5,0) -- (-4,1.5,0);
        \draw [black!75, domain=-1.5:1.5] plot (-4,{\x},{sin((2*\x) r)});
        \draw [black!25, domain=-1.5:1.5] plot (-4,{\x},{0.8*sin((2*\x) r)});

        % grey filled circle
        \fill [black!15, opacity=.5] (0,0,0) circle (1.155cm);

        % projection of globe-coordinate
        \draw [dashed] (c-x) -- +(-4,0,0);
    \end{scope}     
  \end{tikzpicture}
\end{document}

渲染图像

相关内容