
% This file provides LaTeX pst-plot package commands to draw a sphere,
% complete with appropriate markings for spherical and cylindrical
% coordinates.  An example of how to use it:
%
%     \include{sphere}
%     [...]
%     \begin{figure}
%       \spherecaption
%       \label{fig-sphere}
%       \sphere
%     \end{figure}
%
% In the derivations source, all the needed elements are predeclared
% in tex/main.tex; so if writing derivations, you needn't worry about
% declaring anything.  If you have some other caller include this
% file, however, then you must ensure that the following are declared
% before the \begin{document}:
%
%     \usepackage{pst-plot}
%     \include{def}
%
% And that the following are declared after the \begin{document}:
%
%     \newlength{\tla}
%     \newlength{\tlb}
%     \newlength{\tlc}
%     \newlength{\tld}
%     \newlength{\tle}
%     \newlength{\tlf}
%     \newlength{\tlg}
%     \newlength{\tlh}
%     \newlength{\tli}
%     \newlength{\tlj}
%
% The required file tex/def.tex is found in the derivations source.
%
% (One could make a complete LaTeX package of this file, but this writer
% does not know how to make such and, at the moment, judges learning to
% do so an unwise diversion of scarce development time.  Maybe someday.)

\nc\spherecaption{%
  \caption[A point on a sphere.]{%
  A point on a sphere, in spherical $(r;\theta;\phi)$ and cylindrical
  $(\rho;\phi,z)$ coordinates.  (The axis labels bear circumflexes in
  this figure only to disambiguate the $\hat z$ axis from the
  cylindrical coordinate $z$.)%
  }%
}

\nc\sphere{%
  \bc
    % Figure bounds:
      \nc\xa{-5.0}\nc\xb{ 5.0}
      \nc\ya{-3.5}\nc\yb{ 3.5}
    \nc\xxr{2.5}    % The sphere's radius.
    \nc\xxrb{2.8}   % Axis extent.
    \nc\xxq{0.30}   % Right-angle symbol size.
    \nc\xxd{0.85}\nc\xxda{0.1}\nc\xxdb{0.3} % rho dimension offsets
    \nc\xxs{0.20}   % Perspective ratio of the x-y circle.
    \nc\xxphir{0.8} % Angle dimension radius.
    % Angles.  (The xg is a perspective angle for the x axis.)
      \nc\xxxg    {5} \nc\xxcosxg   {0.99619} \nc\xxsinxg   {0.08716}
      \nc\xxphi  {50} \nc\xxcosphi  {0.64279} \nc\xxsinphi  {0.76604}
      \nc\xxtheta{40} \nc\xxcostheta{0.76604} \nc\xxsintheta{0.64279}
    \setlength\tla{\xxr cm}         % The sphere's radius.
    \setlength\tlb{\xxcostheta\tla} % The                      y coordinate of the point.
    \setlength\tlc{\xxsintheta\tla} % The in-ellipse           x coordinate of the point.
    \setlength\tld{\xxsinphi\tlc}   % The on-paper             x coordinate of the point.
    \setlength\tle{\xxcosphi\tlc}   % The non-x-y-perspectived y coordinate of the z-rho intersection.
    \setlength\tlf{\xxs\tle}        % The x-y-perspectived     y coordinate of the z-rho intersection.
    \setlength\tlg{\xxq cm}         % The right-angle symbol size.
    \setlength\tlh{\xxsintheta\tlg} % The in-ellipse           x-length of the right-angle symbol.
    \setlength\tli{\xxcosphi\tlh}   % The non-x-y-perspectived y-offset of the right-angle symbol.
    \setlength\tlj{\xxs\tlh}        % The x-y-perspectived     y-offset of the right-angle symbol.
    \nc\xxaxisx{ % An axis without the upper extension.
      {
        \psset{linewidth=0.5pt}
        \psline[linestyle=dashed](0,-\xxr)(0,\xxr)
        \psline(0,-\xxr)(0,-\xxrb)
      }%
    }
    \nc\xxaxis{ % An axis (vertical by default).
      {
        \xxaxisx
        \psline(0,\xxr)(0,\xxrb)
      }%
    }
    \begin{pspicture}(\xa,\ya)(\xb,\yb)
      \small
      \psset{dimen=middle}
      %\psframe[dimen=outer,linewidth=0.5pt](\xa,\ya)(\xb,\yb) % A figure bounding box.
      {
        \psset{linewidth=2.0pt}
        \localscalebox{1.0}{\xxs}{
          % The horizontal ellipse.
            \pscircle[linewidth=0pt,linecolor=white,fillstyle=solid,fillcolor=lightgray](0,0){\xxr}
          %\psdot(0,0) % A dot at the sphere's center.
          % The x-y circle.
            \psarc(0,0){\xxr}{-180}{0}
            \psarc[linestyle=dashed](0,0){\xxr}{0}{180}
          \rput{-\xxxg}(0,0){\xxaxisx} % The x axis.
          {
            \psset{linewidth=1.0pt}
            \rput{\xxphi}{ % The rho arm.
              \psline[linestyle=dashed](0,0)(0,-\xxr)
            }
            \rput{-90}(0,0){ % The phi angle arc.
              \psarc{->}(0,0){\xxphir}{-\xxxg}{\xxphi}
            }
          }%
        }
        \pscircle(0,0){\xxr} % The outer silhouette of the sphere.
      }
      {
        \psset{linewidth=0.5pt}
        \rput{0} (0,0){\xxaxis} % The z axis.
        \rput{90}(0,0){\xxaxis} % The y axis.
        \rput{90}(0,0){ % (Rotation and rerotation are needed to work around a LaTeX syntax issue.)
          \localscalebox{1.0}{\xxsinphi}{
            % The vertical ellipse.
            \rput{*0}(0,0){
              \psarc(0,0){\xxr}{-90}{90} % The constant-phi arc.
              \rput{90}{ % The theta angle arc.
                \psarc{<-}(0,0){\xxphir}{-\xxtheta}{0}
              }
              \psline[linestyle=dashed](\tlc,\tlb)(\tlc,-\tlf) % The z altitude.
              \rput(0,-\xxd){ % The rho dimension double arrow.
                \psline[linestyle=solid]{<->}(0,\tlf)(\tlc,0)
                \psline(\tlc,-\xxda)(\tlc,\xxdb)
              }
              \rput(\tlc,-\tlf){
                \rput(0,\tlg){\psline{-C}(0,0)(-\tlg,\tlj)}
                \rput(-\tlg,\tlj){\psline{-C}(0,0)(0,\tlg)}
              }
              \rput{-\xxtheta}{\psline[linestyle=dashed](0,0)(0,\xxr)}
            }%
          }
        }
      }
      \psdot(\tld,\tlb)
      \psframe[linewidth=0.0pt,linecolor=white,fillstyle=solid,fillcolor=white]
        (0.48,-0.55)(0.77,-1.00)
      \rput(0.62,1.20){$r$}
      \rput(0.27,0.93){$\theta$}
      \rput(0.25,-0.26){$\phi$}
      \rput(0.60,-0.79){$\rho$}
      \rput(1.38,0.75){$z$}
      \rput(0,3.05){$\hat z$}
      \rput(3.00,0){$\hat y$}
      \rput(-0.38,-0.75){$\hat x$}
    \end{pspicture}
  \ec%
}

