% Entire source released into the public domain -- Keith Wolcott, 2012.
\documentclass[final]{ltugboat}
\usepackage{microtype}
\def\code{\nolinkurl}
\divide\floatsep by 2
\renewcommand{\topfraction}{.9}  % don't go to a float page so soon:
\renewcommand{\dbltopfraction}{.9}
\renewcommand{\bottomfraction}{.7}
\renewcommand{\textfraction}{.1}
\renewcommand{\floatpagefraction}{.8}
\renewcommand{\dblfloatpagefraction}{.8}

\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{ifthen}
\usepackage{framed}

\usepackage{ifpdf}
\ifpdf
  \usepackage[breaklinks,colorlinks,linkcolor=black,citecolor=black,
              urlcolor=black]{hyperref}
\else
  \usepackage{url}
\fi
 

\usepackage[all]{hypcap}
 \usepackage{float}

\newboolean{fastdraft}
\setboolean{fastdraft}{false}  % Set to false for a final version (which takes about 30 seconds).
\ifthenelse{\boolean{fastdraft}}
{\def\speed{.10}}
{\def\speed{10}}



\newboolean{color}
\setboolean{color}{false}  % Set to true for color version.





%% helper macros

\newcommand\pgfmathsinandcos[3]{%
  \pgfmathsetmacro#1{sin(#3)}%
  \pgfmathsetmacro#2{cos(#3)}%
}
\newcommand\LongitudePlane[3][current plane]{%
  \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
  \pgfmathsinandcos\sint\cost{#3} % azimuth
  \tikzset{#1/.estyle={cm={\cost,\sint*\sinEl,0,\cosEl,(0,0)}}}
}
\newcommand\LatitudePlane[3][current plane]{%
  \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
  \pgfmathsinandcos\sint\cost{#3} % latitude
  \pgfmathsetmacro\yshift{\cosEl*\sint}
  \tikzset{#1/.estyle={cm={\cost,0,0,\cost*\sinEl,(0,\yshift)}}} %
}
\newcommand\DrawLongitudeCircle[4][1]{
  \LongitudePlane{\angEl}{#2}
  \tikzset{current plane/.prefix style={scale=#1}}
   % angle of "visibility"
  \pgfmathsetmacro\angVis{atan(sin(#2)*cos(\angEl)/sin(\angEl))} %
  \draw[shift={(#3, #4)}][current plane] (\angVis:1) arc (\angVis:\angVis+180:1);
  \draw[shift={(#3, #4)}][current plane,dashed] (\angVis-180:1) arc (\angVis-180:\angVis:1);
}
\newcommand\DrawLatitudeCircle[4][1]{
  \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[shift={(#3, #4)}][current plane] (\angVis:1) arc (\angVis:-\angVis-180:1);
  \draw[shift={(#3, #4)}][current plane,dashed] (180-\angVis:1) arc (180-\angVis:\angVis:1);
}

\newcommand\calculateCenterSpan[4]{
\pgfmathsetmacro#1{((#2)^2 + (#3)^2 + (#4)^2)^(1/2)}
}

\newcommand\calculateAngtheta[3]{
\pgfmathsetmacro#1{acos(#2/#3)}
}

\newcommand\calculateShiftDistance[4]{
\pgfmathsetmacro#1{((((#2)^2 - (#3)^2)/(2*#4) + #4/2))}
}

\newcommand\calculateShiftDistancePercent[3]{
\pgfmathsetmacro#1{#2/#3}
}

\newcommand\calculateCircleRadius[3]{
\pgfmathsetmacro#1{((#2)^2 - (#3)^2)^(1/2)}
}

% The function below does not use the rotation matrices about coordinate axis, but instead computes the vector that we want to rotate around and then the corresponding rotation matrix and then as before projects to the xy-plane.  It also shifts to the correct location.
\newcommand\drawIntersectionOfSpheres[9]{ 
% Parameters are,  CenterSphere1x, CenterSphere1y, CenterSphere1z, CenterSphere2x,
% CenterSphere2y, CenterSphere2z, RadiusSphere1, RadiusSphere2, DrawColor.
\pgfmathsetmacro\xchange{#4 - #1}
\pgfmathsetmacro\ychange{#5 - #2}
\pgfmathsetmacro\zchange{#6 - #3}
\pgfmathsetmacro\firstSphereCenterx{#1}
\pgfmathsetmacro\firstSphereCentery{#2}

\calculateCenterSpan\centerSpan{\xchange}{\ychange}{\zchange}
\calculateAngtheta\angtheta{\zchange}{\centerSpan}
\calculateShiftDistance\shiftDistance{#7}{#8}{\centerSpan}
\calculateShiftDistancePercent\shiftDistancePercent{\shiftDistance}{\centerSpan}
\calculateCircleRadius\circleRadius{#7}{\shiftDistance}

\pgfmathsetmacro\ux{\ychange}
\pgfmathsetmacro\uy{-\xchange}
\pgfmathsetmacro\C{1-cos(\angtheta)}
 \pgfmathsetmacro\L{((\ux)^2 + (\uy)^2)^(1/2)}
 
\pgfmathsetmacro\first{cos(\angtheta) + (\ux)^2*\C/(\L^2)}
\pgfmathsetmacro\second{\ux*\uy*\C/(\L^2)}
\pgfmathsetmacro\third{\ux*\uy*\C/(\L^2)}
\pgfmathsetmacro\fourth{cos(\angtheta)+(\uy)^2*\C/(\L^2)}

\tikzset{xyplane/.estyle={cm={\first,  \second, \third,  \fourth,  (\firstSphereCenterx + \shiftDistancePercent*\xchange, \firstSphereCentery + \shiftDistancePercent*\ychange)}}}

%\draw[xyplane] (-2*\R,-2*\R) rectangle (2*\R,2*\R);
%\draw[xyplane, color=#9, fill=#9, opacity=.5] (0, 0) circle (\circleRadius);

%If you don't want a dashed line on the back, use the following line instead of the next three lines.
%\draw[xyplane, color=#9, ultra thick] (0, 0) circle (\circleRadius);

\pgfmathsetmacro\dottedAngle{atan(\xchange/\ychange)}
\draw[xyplane, color=#9, ultra thick] (-\dottedAngle:\circleRadius) arc (-\dottedAngle:-\dottedAngle-180:\circleRadius);
\draw[xyplane, color=#9, ultra thick, dashed] (-\dottedAngle+180:\circleRadius) arc (-\dottedAngle+180:-\dottedAngle:\circleRadius);
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\getFrontArcStartPosition[5]{% Start of \getFrontArcStartPosition. 
% Theta must be between -180 and 180. 
\pgfmathtruncatemacro\psiInt{#3} 
\pgfmathtruncatemacro\thetaInt{#4} 
\pgfmathtruncatemacro\phiInt{#5} 
\pgfmathtruncatemacro\psiTemp{\psiInt} 
\ifthenelse{\thetaInt < 0}% When theta < 0, use symmetry. Negate theta and negate the results at the end. 
{\pgfmathtruncatemacro\thetaTemp{-\thetaInt}} 
{\pgfmathtruncatemacro\thetaTemp{\thetaInt}} 
\pgfmathtruncatemacro\phiTemp{\phiInt} 
\pgfmathsetmacro\anglexTemp{atan(sin(\thetaTemp)/(cos(\thetaTemp)*sin(\psiTemp)))} 
\ifthenelse{\thetaTemp > 90} 
{\pgfmathsetmacro\anglexTemp{\anglexTemp + 180}} 
{} 
\pgfmathsetmacro\result{atan(sin(\thetaTemp)*cos(\phiTemp)*sin(\anglexTemp)/ 
(sin(\anglexTemp)*cos(\psiTemp)*sin(\phiTemp)+cos(\anglexTemp)*cos(\phiTemp)*sin(\thetaTemp)))} 
\pgfmathsetmacro\specialAngle{(cos(\phiTemp)*cos(\phiTemp) - cos(\psiTemp)*cos(\psiTemp))/ 
(cos(\phiTemp)*cos(\phiTemp)*sin(\psiTemp)*sin(\psiTemp))} 
\ifthenelse{\phiInt < \psiInt} 
{ 
\pgfmathparse{sqrt(\specialAngle)} 
\pgfmathsetmacro\specialAngle{asin(-\pgfmathresult)+180} 
\ifthenelse{\thetaTemp > \specialAngle} 
{ 
\pgfmathsetmacro\result{\result + 180} 
\pgfmathsetmacro#1{\result}} 
{ 
\pgfmathsetmacro#1{\result}}} 
{} 
\ifthenelse{\thetaInt < 0}% Negate the results since theta is negative. 
{ 
\pgfmathsetmacro#1{-\result} 
\pgfmathsetmacro#2{-\anglexTemp}} 
{ 
\pgfmathsetmacro#1{\result} 
\pgfmathsetmacro#2{\anglexTemp}} 
}% End of \getFrontArcStartPosition function. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the shaded outter frame
\newenvironment{outshaded}{%
  \def\FrameCommand{\colorbox{gray!20}}%
  \MakeFramed {\FrameRestore}}%
 {\endMakeFramed}



%%% Start of metadata %%%

\title{Three-dimensional graphics with \acro{PGF}/\TikZ}

% repeat info for each author.
\author{Keith Wolcott}
\address{Dept. of Mathematics and Computer Science\\
Eastern Illinois University\\
Charleston, IL 61920-3099 \quad USA}
\netaddress{kwolcott (at) eiu dot edu}
%\personalURL{http://example.org/~user/}

%%% End of metadata %%%


\begin{document}
\maketitle


\begin{abstract}
\acro{PGF} and \TikZ{} are languages for creating graphics.  These packages are predominantly two-dimensional graphics packages, so three-dimensional graphing is more challenging, but still possible. A demonstration of how to draw surfaces of revolution, satellite orbits, and intersections of spheres is given.  As is typical with three-dimensional graphics, the technique is to rotate in three-space and then project to the drawing surface.  The mathematics involved is discussed and sample code is provided.
\end{abstract}

\section{Introduction}

\acro{PGF} (Portable Graphics Format) is a lower-level language and \TikZ{} is a set of higher-level macros that use \acro{PGF}.  \TikZ{} is a recursive acronym for ``\TikZ{} ist kein Zeichenprogramm'' or ``\TikZ{} is not a drawing program''.  These languages were created by Till Tantau~\cite{TT}.  \acro{PGF} and \TikZ{} commands are invoked as \AllTeX\ macros.

\TikZ{} is packed with features for two-dimensional drawings of lines, circles, ellipses, paths, graphs, etc.  The \acro{PGF}/\TikZ{} manual~\cite{TT} has many examples to facilitate learning these languages. Andrew Mertz and William Slough~\cite{AMWS} have written a very nice sequence of examples which is an excellent way to get started using \TikZ{}.  

The graph in figure~\ref{figure2d} is an example of a function graphed using \acro{PGF} and \TikZ{}.

\begin{figure}[H]
\centering
% Set the x = a and x = b values of the domain here where a <= x <= b.
\def\aDomain{-1.4}
\def\bDomain{1.4}
% Set min and max values of the function (c <= f(x) <= d). Used for the y-axis.
\def\cRange{-2.5}
\def\dRange{2.5}
\pgfmathsetmacro\scaleAttempt{2/(\bDomain - \aDomain)}

\begin{tikzpicture}[scale= \scaleAttempt, domain= \aDomain : \bDomain]
\draw[very thin,color=gray] (1.1*\aDomain, 1.1*\cRange) grid (1.1*\bDomain, 1.1*\dRange);
\draw[very thick, ->] (1.2*\aDomain, 0) -- (1.2*\bDomain, 0) node[right] {$x$};
\draw[very thick, ->] (0, 1.2*\cRange) -- (0, 1.2*\dRange) node[above] {$y$};
\draw[smooth, very thick] plot (\x, 3*\x^5 - 5*\x^3);
\end{tikzpicture}
\caption{$f(x) = 3x^5 - 5x^3$.}
\label{figure2d}
\end{figure}

\begin{figure}[b]
\begin{center}
\includegraphics[width=.6\hsize]{fig2-x2rot-crop.pdf}
\end{center}
\caption{$f(x) = x^2$ rotated about the $y$-axis.}
\label{rot1}
\end{figure}

\noindent
The following code for this figure, and all of the examples in this paper, can be run by cutting-and-pasting into a \LaTeX\ document of this form (be sure to use at least a 2011 version since there are compatibility issues with earlier versions of \acro{PGF}/\TikZ{}):
\newpage

%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode
\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\ruled]}
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{ifthen}\newboolean{color}
\begin{document}
% insert code here
\end{document}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize

\smallskip
\noindent Here is the code to generate figure~1:

%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode
\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\ruled]}
\begin{figure}[H]
\centering
% Set the x = a and x = b values of the domain here
% where a <= x <= b.
\def\aDomain{-1.4}
\def\bDomain{1.4}
% Set min and max values of the function
% (c <= f(x) <= d). Used for the y-axis.
\def\cRange{-2.5}
\def\dRange{2.5}
\pgfmathsetmacro\scaleAttempt{2/(\bDomain-\aDomain)}

\begin{tikzpicture}[scale= \scaleAttempt,
   domain= \aDomain : \bDomain]
\draw[very thin,color=gray]
   (1.1*\aDomain,1.1*\cRange)
   grid (1.1*\bDomain, 1.1*\dRange);
\draw[very thick, ->] (1.2*\aDomain, 0) -- 
   (1.2*\bDomain, 0) node[right] {$x$};
\draw[very thick, ->] (0, 1.2*\cRange) -- 
   (0, 1.2*\dRange) node[above] {$y$};
\draw[smooth, very thick]
   plot (\x, 3*\x^5 - 5*\x^3);
\end{tikzpicture}
      
\caption{$f(x) = 3x^5 - 5x^3$.}
\end{figure}
\end{verbatim}
\vspace{.2cm}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize

\enlargethispage{-1pc}
\section{Three-dimensional graphing}

%\vskip-25pt \hrule \vskip25pt \hrule \vskip5pt
The graph in figure~\ref{rot1} is rotated about the $y$-axis.
The code for the figure is given in appendix~\ref{rotyCode}.






First we give additional examples, and then an explanation of the methods used.
To create additional examples, use the eleven parameters that are set at the beginning of the code.  They are the domain (min and max), function, range (min and max), back color, front color, shading steps, \code{xGridSteps}, \code{rotationGridSteps}, and the viewing angle.

Changing the following four parameters and the figure title results in figure~\ref{rot2}.
\smallskip

\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\ruled]}
\def\bDomain{6.3}		
\def\fcn{cos(\x r)} % The r means to use radians.
\def\cRange{-1}
\def\dRange{1}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize


\begin{figure}[H]
\begin{center}
%%%%%%%%%%%%%%%%%%%%%   Set Function Values  %%%%%%%%%%%%%%%%%%%%%
% Set the x = a and x = b values of the domain here where a <= x <= b.
\def\aDomain{0}
\def\bDomain{6.3}
% Set the function.  The variable must be \x.  e.g. \x^2.		
\def\fcn{cos(\x r)}
%\def\fcn{\x^2}
%\def\fcn{sqrt(\x)}
% Set min and max values of the function (c <= f(x) <= d). Used for the y-axis.
\def\cRange{-1}
\def\dRange{1}
% Set the color of the back half.  
% This can look good as a different color if it looks like the inside.	
\ifthenelse{\boolean{color}}
{\def\backColor{brown}}
{\def \backColor{lightgray!30}}
% Set the color of the front half.  lightgray looks good for both back and front.	 
\ifthenelse{\boolean{color}}	 
{\def\frontColor{red}}
{\def\frontColor{lightgray}}
% Set the number of shading circles to draw.  More gives a more even color.
% Enter 1 for no shading.	
\pgfmathsetmacro\xShadingSteps{30* \speed}
% Set the number of x radius grid circles.
\def\xGridSteps{8}
% Set the number of radial grid lines.	
\def\rotationGridSteps{12}	
% Set the viewing elevation angle, which is the angle up from horizontal.
\def\phi{10}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfmathsetmacro\scaleAttempt{2.7/\bDomain}
\begin{tikzpicture}[scale= \scaleAttempt, domain= \aDomain: \bDomain]
\pgfmathsetmacro\intervalLength{\bDomain - \aDomain}
\pgfmathsetmacro\xGridStepsize{\intervalLength/\xGridSteps}
\pgfmathsetmacro\xShadingStepsize{\intervalLength/\xShadingSteps}
\pgfmathsetmacro\rotationGridStepsize{360/\rotationGridSteps}
% Draw the shading of the back half.
% Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
 \pgfmathsetmacro\nextShadingStep{\aDomain + \xShadingStepsize}
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, \backColor, ultra thick, opacity=1] (\x, 0) arc (0:180:\x);
}
% Back Longitude lines.
% Rotates graph around y-axis, then projects to xy-plane.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={cos(-\theta),  sin(-\theta)*sin(-\phi), 0,  cos(-\phi),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}
 % Back Latitude lines.
 % Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
 % shifted up or down to the correct height.
\pgfmathsetmacro\nextStep{\aDomain + \xGridStepsize}
\foreach \x in {\aDomain, \nextStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, color=black] (\x, 0) arc (0: 180:\x);  
}
% Draw the axis.
\pgfmathsetmacro\yHeight{\dRange + \bDomain*sin(\phi) + .5}
\draw[->] (0, \cRange - .5) -- (0, \yHeight) node[above] {$y$};
% Comment out the next four lines if you don't want an x-axis, and labels.
\draw[<->] (-\bDomain -.5, 0) -- (\bDomain + .5, 0)
   node[right] {$x$};
\draw (\bDomain, .1) -- (\bDomain, -.1)
   node[below] {\bDomain};
\pgfmathsetmacro\yLabel {cos(\phi)* \dRange}
\draw (-.1, \yLabel) -- (.1, \yLabel)
   node[right] {\dRange};
   
% Draw the shading of the front half.
% Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, \frontColor, ultra thick, opacity=.6] (-\x, 0) arc (-180:0:\x); 
}
% Front Longitude lines.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={cos(\theta),  sin(\theta)*sin(-\phi), 0,  cos(-\phi),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}
% Front Latitude lines.
% Bottom half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
\foreach \x in {\aDomain, \nextStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0,  \ysh)}}}
    \draw[xyplane, color=black] (-\x, 0) arc (-180: 0:\x); 
}
\end{tikzpicture}
\caption{$f(x) = \cos{x}$ rotated about the $y$-axis.}
\label{rot2}
\end{center}
\end{figure}

\medskip

We may not like the back and front colors so we change them both to \code{lightgray}.  We also change the viewing angle from 10 to 15 degrees.  To give some idea of how other changes can be made, we enlarge it and also comment out the $x$-axis and the axis number labels.  The altered lines of code for figure~\ref{rot3} are:

\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\ruled]}
\def\backColor{lightgray}
\def\frontColor{lightgray}

\def\phi{15} % Viewing angle of 15 degrees.

\pgfmathsetmacro\scaleAttempt{6/\bDomain}% 6 was 4.

%\draw[<->] (-\bDomain -.5, 0) -- (\bDomain + .5, 0)
%   node[right] {$x$};
%\draw (\bDomain, .1) -- (\bDomain, -.1)
%   node[below] {\bDomain};
%\pgfmathsetmacro\yLabel {cos(\phi)* \dRange}
%\draw (-.1, \yLabel) -- (.1, \yLabel)
%   node[right] {\dRange};
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize

\smallskip

\begin{figure}[H]
\begin{center}
%%%%%%%%%%%%%%%%%%%%%   Set Function Values  %%%%%%%%%%%%%%%%%%%%%
% Set the x = a and x = b values of the domain here where a <= x <= b.
\def\aDomain{0}
\def\bDomain{6.3}
% Set the function.  The variable must be \x.  e.g. \x^2.		
\def\fcn{cos(\x r)}
%\def\fcn{\x^2}
%\def\fcn{sqrt(\x)}
% Set min and max values of the function (c <= f(x) <= d). Used for the y-axis.
\def\cRange{-1}
\def\dRange{1}
% Set the color of the back half.  
% This can look good as a different color if it looks like the inside.	
\def\backColor{lightgray}
% Set the color of the front half.  lightgray looks good for both back and front.	 
\def\frontColor{lightgray}
% Set the number of shading circles to draw.  More gives a more even color.
% Enter 1 for no shading.	
\pgfmathsetmacro\xShadingSteps{30* \speed}
% Set the number of x radius grid circles.
\def\xGridSteps{8}
% Set the number of radial grid lines.	
\def\rotationGridSteps{12}	
% Set the viewing elevation angle, which is the angle up from horizontal.
\def\phi{15}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfmathsetmacro\scaleAttempt{3.5/\bDomain}
\begin{tikzpicture}[scale= \scaleAttempt, domain= \aDomain: \bDomain]
\pgfmathsetmacro\intervalLength{\bDomain - \aDomain}
\pgfmathsetmacro\xGridStepsize{\intervalLength/\xGridSteps}
\pgfmathsetmacro\xShadingStepsize{\intervalLength/\xShadingSteps}
\pgfmathsetmacro\rotationGridStepsize{360/\rotationGridSteps}
% Draw the shading of the back half.
% Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
 \pgfmathsetmacro\nextShadingStep{\aDomain + \xShadingStepsize}
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, \backColor, ultra thick, opacity=1] (\x, 0) arc (0:180:\x);
}
% Back Longitude lines.
% Rotates graph around y-axis, then projects to xy-plane.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={cos(-\theta),  sin(-\theta)*sin(-\phi), 0,  cos(-\phi),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}
 % Back Latitude lines.
 % Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
 % shifted up or down to the correct height.
\pgfmathsetmacro\nextStep{\aDomain + \xGridStepsize}
\foreach \x in {\aDomain, \nextStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, color=black] (\x, 0) arc (0: 180:\x);  
}
% Draw the axis.
\pgfmathsetmacro\yHeight{\dRange + \bDomain*sin(\phi) + .5}
\draw[->] (0, \cRange - .5) -- (0, \yHeight) node[above] {$y$};
% Comment out the next four lines if you don't want an x-axis, and labels.
%\draw[<->] (-\bDomain -.5, 0) -- (\bDomain + .5, 0) node[right] {$x$};
%\draw (\bDomain, .1) -- (\bDomain, -.1) node[below] {\bDomain};
%\pgfmathsetmacro\yLabel {cos(\phi)* \dRange}
%\draw (-.1, \yLabel) -- (.1, \yLabel) node[right] {\dRange};
   
% Draw the shading of the front half.
% Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, \frontColor, ultra thick, opacity=.6] (-\x, 0) arc (-180:0:\x); 
}
% Front Longitude lines.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={cos(\theta),  sin(\theta)*sin(-\phi), 0,  cos(-\phi),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}
% Front Latitude lines.
% Bottom half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
\foreach \x in {\aDomain, \nextStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0,  \ysh)}}}
    \draw[xyplane, color=black] (-\x, 0) arc (-180: 0:\x); 
}
\end{tikzpicture}
\caption{$f(x) = \cos{x}$ rotated about the $y$-axis.}
\label{rot3}
\end{center}
\end{figure}

\newpage
Figure~\ref{rot4} is another example.
\begin{figure}[H]
\begin{center}
%%%%%%%%%%%%%%%%%%%%%   Set Function Values  %%%%%%%%%%%%%%%%%%%%%
% Set the x = a and x = b values of the domain here where a <= x <= b.
\def\aDomain{1}
\def\bDomain{4}
% Set the function.  The variable must be \x.  e.g. \x^2.		
%\def\fcn{cos(\x r)}
%\def\fcn{\x^2}
\def\fcn{2*sqrt(\x)}
% Set min and max values of the function (c <= f(x) <= d). Used for the y-axis.
\def\cRange{2}
\def\dRange{4}
% Set the color of the back half.  
% This can look good as a different color if it looks like the inside.	
\def\backColor{lightgray}
% Set the color of the front half.  lightgray looks good for both back and front.	 
\def\frontColor{lightgray}
% Set the number of shading circles to draw.  More gives a more even color.
% Enter 1 for no shading.	
\pgfmathsetmacro\xShadingSteps{30* \speed}
% Set the number of x radius grid circles.
\def\xGridSteps{8}
% Set the number of radial grid lines.	
\def\rotationGridSteps{12}	
% Set the viewing elevation angle, which is the angle up from horizontal.
\def\phi{7}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfmathsetmacro\scaleAttempt{3.5/\bDomain}
\begin{tikzpicture}[scale= \scaleAttempt, domain= \aDomain: \bDomain]
\pgfmathsetmacro\intervalLength{\bDomain - \aDomain}
\pgfmathsetmacro\xGridStepsize{\intervalLength/\xGridSteps}
\pgfmathsetmacro\xShadingStepsize{\intervalLength/\xShadingSteps}
\pgfmathsetmacro\rotationGridStepsize{360/\rotationGridSteps}
% Draw the shading of the back half.
% Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
 \pgfmathsetmacro\nextShadingStep{\aDomain + \xShadingStepsize}
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, \backColor, ultra thick, opacity=1] (\x, 0) arc (0:180:\x);
}
% Back Longitude lines.
% Rotates graph around y-axis, then projects to xy-plane.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={cos(-\theta),  sin(-\theta)*sin(-\phi), 0,  cos(-\phi),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}
 % Back Latitude lines.
 % Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
 % shifted up or down to the correct height.
\pgfmathsetmacro\nextStep{\aDomain + \xGridStepsize}
\foreach \x in {\aDomain, \nextStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, color=black] (\x, 0) arc (0: 180:\x);  
}
% Draw the axis.
\pgfmathsetmacro\yHeight{\dRange + \bDomain*sin(\phi) + .5}
\draw[->] (0, \cRange - .5) -- (0, \yHeight) node[above] {$y$};
% Comment out the next four lines if you don't want an x-axis, and labels.
%\draw[<->] (-\bDomain -.5, 0) -- (\bDomain + .5, 0) node[right] {$x$};
%\draw (\bDomain, .1) -- (\bDomain, -.1) node[below] {\bDomain};
%\pgfmathsetmacro\yLabel {cos(\phi)* \dRange}
%\draw (-.1, \yLabel) -- (.1, \yLabel) node[right] {\dRange};
   
% Draw the shading of the front half.
% Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, \frontColor, ultra thick, opacity=.6] (-\x, 0) arc (-180:0:\x); 
}
% Front Longitude lines.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={cos(\theta),  sin(\theta)*sin(-\phi), 0,  cos(-\phi),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}
% Front Latitude lines.
% Bottom half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
\foreach \x in {\aDomain, \nextStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0,  \ysh)}}}
    \draw[xyplane, color=black] (-\x, 0) arc (-180: 0:\x); 
}
\end{tikzpicture}
\caption{$f(x) = 2\sqrt{x}$, from 1 to 4, rotated about the $y$-axis.}
\label{rot4}
\end{center}
\end{figure}



Figure~\ref{rot5} is figure \ref{rot4} rotated toward the viewer by changing the view angle from 7  to 30 degrees.

\begin{figure}[H]
\begin{center}
%%%%%%%%%%%%%%%%%%%%%   Set Function Values  %%%%%%%%%%%%%%%%%%%%%
% Set the x = a and x = b values of the domain here where a <= x <= b.
\def\aDomain{1}
\def\bDomain{4}
% Set the function.  The variable must be \x.  e.g. \x^2.		
%\def\fcn{cos(\x r)}
%\def\fcn{\x^2}
\def\fcn{2*sqrt(\x)}
% Set min and max values of the function (c <= f(x) <= d). Used for the y-axis.
\def\cRange{2}
\def\dRange{4}
% Set the color of the back half.  
% This can look good as a different color if it looks like the inside.	
\def\backColor{lightgray}
% Set the color of the front half.  lightgray looks good for both back and front.	 
\def\frontColor{lightgray}
% Set the number of shading circles to draw.  More gives a more even color.
% Enter 1 for no shading.	
\pgfmathsetmacro\xShadingSteps{30* \speed}
% Set the number of x radius grid circles.
\def\xGridSteps{8}
% Set the number of radial grid lines.	
\def\rotationGridSteps{12}	
% Set the viewing elevation angle, which is the angle up from horizontal.
\def\phi{30}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfmathsetmacro\scaleAttempt{3.5/\bDomain}
\begin{tikzpicture}[scale= \scaleAttempt, domain= \aDomain: \bDomain]
\pgfmathsetmacro\intervalLength{\bDomain - \aDomain}
\pgfmathsetmacro\xGridStepsize{\intervalLength/\xGridSteps}
\pgfmathsetmacro\xShadingStepsize{\intervalLength/\xShadingSteps}
\pgfmathsetmacro\rotationGridStepsize{360/\rotationGridSteps}
% Draw the shading of the back half.
% Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
 \pgfmathsetmacro\nextShadingStep{\aDomain + \xShadingStepsize}
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, \backColor, ultra thick, opacity=1] (\x, 0) arc (0:180:\x);
}
% Back Longitude lines.
% Rotates graph around y-axis, then projects to xy-plane.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={cos(-\theta),  sin(-\theta)*sin(-\phi), 0,  cos(-\phi),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}
 % Back Latitude lines.
 % Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
 % shifted up or down to the correct height.
\pgfmathsetmacro\nextStep{\aDomain + \xGridStepsize}
\foreach \x in {\aDomain, \nextStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, color=black] (\x, 0) arc (0: 180:\x);  
}
% Draw the axis.
\pgfmathsetmacro\yHeight{\dRange + \bDomain*sin(\phi) + .5}
\draw[->] (0, \cRange - .5) -- (0, \yHeight) node[above] {$y$};
% Comment out the next four lines if you don't want an x-axis, and labels.
%\draw[<->] (-\bDomain -.5, 0) -- (\bDomain + .5, 0) node[right] {$x$};
%\draw (\bDomain, .1) -- (\bDomain, -.1) node[below] {\bDomain};
%\pgfmathsetmacro\yLabel {cos(\phi)* \dRange}
%\draw (-.1, \yLabel) -- (.1, \yLabel) node[right] {\dRange};
   
% Draw the shading of the front half.
% Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0, \ysh)}}}
    \draw[xyplane, \frontColor, ultra thick, opacity=.6] (-\x, 0) arc (-180:0:\x); 
}
% Front Longitude lines.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={cos(\theta),  sin(\theta)*sin(-\phi), 0,  cos(-\phi),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}
% Front Latitude lines.
% Bottom half of a circle, rotated back (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
\foreach \x in {\aDomain, \nextStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 - \phi),  (0,  \ysh)}}}
    \draw[xyplane, color=black] (-\x, 0) arc (-180: 0:\x); 
}
\end{tikzpicture}
\caption{$f(x) = 2\sqrt{x}$, from 1 to 4, rotated about the $y$-axis,
with a view angle of 30 degrees.}
\label{rot5}
\end{center}
\end{figure}

%\newpage
With similar code we rotate about the $x$-axis.
%%%%%%%%%%%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
\begin{figure}[H]
\begin{center}
%%%%%%%%%%%%%%%%%%%%%   Set Function Values  %%%%%%%%%%%%%%%%%%%%%
% Set the x = a and x = b values of the domain here where a <= x <= b.
\def\aDomain{0}
\def\bDomain{3.14159}
% Set the function.  The variable must be \x.  e.g. \x^2.		
%\def\fcn{cos(\x r)}
\def\fcn{sin(\x r)}
%\def\fcn{sqrt(\x)}
% Set min and max values of the function (c <= f(x) <= d). Used for the y-axis.
\def\cRange{0}
\def\dRange{1}
% Set the color of the back half.  
% This can look good as a different color if it looks like the inside.	
\ifthenelse{\boolean{color}}
{\def \backColor{red!70!black}}
{\def\backColor{lightgray}}
% Set the color of the front half.  lightgray looks good for both back and front.
\ifthenelse{\boolean{color}}
{\def\frontColor{red!70!black}}
{\def\frontColor{lightgray}}
% Set the number of shading circles to draw.  More gives a more even color.
% Enter 1 for no shading.	
\pgfmathsetmacro\xShadingSteps{10* \speed}
%\pgfmathsetmacro\xShadingSteps{300}
% Set the number of x radius grid circles.
\def\xGridSteps{8}
% Set the number of radial grid lines.	
\def\rotationGridSteps{18}	
% Set the viewing elevation angle, which is the angle up from horizontal.
\def\phi{15}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfmathsetmacro\scaleAttempt{1.8/\dRange}
\begin{tikzpicture}[scale= \scaleAttempt, domain= \aDomain: \bDomain]
\pgfmathsetmacro\intervalLength{\bDomain - \aDomain}
\pgfmathsetmacro\xGridStepsize{\intervalLength/\xGridSteps}
\pgfmathsetmacro\xShadingStepsize{\intervalLength/\xShadingSteps}
\pgfmathsetmacro\rotationGridStepsize{360/\rotationGridSteps}
% Draw the shading of the back half.
% Left half of a circle, rotated right (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
 \pgfmathsetmacro\nextShadingStep{\aDomain + \xShadingStepsize}
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\xsh{(cos(\phi))*(\x)}
    \pgfmathsetmacro\rad{(\fcn)}
    \tikzset{xyplane/.estyle={cm={cos(\phi - 90), 0, 0, 1,  (\xsh, 0)}}}
    \draw[xyplane, \backColor, ultra thick, opacity= .6] (0, \rad) arc (90 : 270 : \rad);
}
% Back Longitude lines.
% Rotates graph around y-axis, then projects to xy-plane.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={cos(\phi),  0, sin(\theta)*sin(\phi),  cos(\theta),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}
 % Back Latitude lines.
 % Left half of a circle, rotated right (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
\pgfmathsetmacro\nextStep{\aDomain + \xGridStepsize}
\foreach \x in {\aDomain, \nextStep, ..., \bDomain} {
    \pgfmathsetmacro\xsh{(cos(\phi))*(\x)}
    \pgfmathsetmacro\rad{(\fcn)}
    \tikzset{xyplane/.estyle={cm={cos(\phi - 90), 0, 0, 1,  (\xsh, 0)}}}
    \draw[xyplane, black, thin, opacity=1] (0, \rad) arc (90 : 270 : \rad); 
}
% Draw the axis.
\pgfmathsetmacro\xdim{\bDomain + \dRange*sin(\phi)}
\draw[->] (0, -\dRange - .5) -- (0, \dRange + .5) node[above] {$y$};
% Comment out the next four lines if you don't want an x-axis, and labels.
\draw[<->] (\aDomain -.2, 0) -- (\bDomain+.1, 0) node[right] {$x$};
\pgfmathsetmacro\xLabel{cos(\phi)*\bDomain}
\draw (\xLabel, .1) -- (\xLabel, -.1) node[below right] {\bDomain};
\draw (-.1, \dRange) -- (.1, \dRange) node[right] {\dRange};
   
% Draw the shading of the front half.
% Right half of a circle, rotated right (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\xsh{(cos(\phi))*(\x)}
    \pgfmathsetmacro\rad{(\fcn)}
    \tikzset{xyplane/.estyle={cm={cos(\phi - 90), 0, 0, 1,  (\xsh, 0)}}}
    \draw[xyplane, \frontColor, ultra thick, opacity= .6] (0, -\rad) arc (-90 : 90 : \rad); 
}
% Front Longitude lines.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={cos(\phi),  0, sin(\theta)*sin(\phi),  cos(\theta),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}

% Front Latitude lines.
% Right half of a circle, rotated right (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
\foreach \x in {\aDomain, \nextStep, ..., \bDomain} {
    \pgfmathsetmacro\xsh{(cos(\phi))*(\x)}
     \pgfmathsetmacro\rad{(\fcn)}
    \tikzset{xyplane/.estyle={cm={cos(\phi - 90), 0, 0, 1,  (\xsh, 0)}}}
    \draw[xyplane, black, thin, opacity=1] (0, -\rad) arc (-90 : 90 : \rad); 
}
\end{tikzpicture}
\caption{$f(x) = \sin{x}$ rotated about the $x$-axis.}
\label{rot1x}
\end{center}
\end{figure}

The code for figure \ref{rot1x} is included in appendix \ref{rotxCode}.








%\vspace{1.5cm}
\section{The mathematics behind the scenes}
%\vskip-25pt \hrule \vskip25pt \hrule \vskip5pt



%{\bf The Mathematics Behind the Scenes}

This project began with the goal of drawing two spheres and their circle of intersection.  A Google search turned up Tomasz M. Trzeciak's~\cite{TMT} beautiful spheres (see figure~\ref{sphere1}).    He very effectively draws spheres, drawing the latitude and longitude curves by creating a circle, rotating it in three-space, and then projecting to the $xy$-plane.
\begin{figure}[H]
\begin{center}
\begin{tikzpicture}

\def\Rc{2.5} % sphere radius
\def\angEl{30} % elevation angle
\def\xc{0} % x coordinate of center
\def\yc{0} % y coordinate of center
\def\zc{0} % z coordinate of center
\filldraw[shift={(\xc, \yc)}][ball color=lightgray, opacity=1] (0,0,0) circle (\Rc);
\foreach \t in {-80,-60,...,80} { \DrawLatitudeCircle[\Rc]{\t}{\xc}{\yc}}
\foreach \t in {-5,-35,...,-175} { \DrawLongitudeCircle[\Rc]{\t}{\xc}{\yc}}

\end{tikzpicture}
\caption{Beautiful sphere created by Tomasz M. Trzeciak.}
\label{sphere1}
\end{center}
\end{figure}
\noindent
We will show how to use these methods to draw the surface of revolution in figure \ref{rot1}.

 The drawing surface is the $xy$-plane with the $x$-axis pointing to the right and the $y$-axis pointing up.  The $z$-axis points toward the viewer.
The rotation matrices about the $x$, $y$, and $z$-axes at an angle $\theta$ in the counterclockwise direction are:
$$R_x(\theta) = \left( \begin{array}{ccc}
1 & 0 & 0 \\
0 & \cos{\theta} & -\sin{\theta} \\
0 & \sin{\theta} & \cos{\theta} \end{array} \right),$$

$$R_y(\theta) = \left( \begin{array}{ccc}
\cos{\theta} & 0 & \sin{\theta}  \\
0 & 1 & 0 \\
-\sin{\theta} & 0 & \cos{\theta} \end{array} \right),$$

and $$R_z(\theta) = \left( \begin{array}{ccc}
\cos{\theta} & -\sin{\theta} & 0  \\
\sin{\theta} & \cos{\theta} & 0\\
0 & 0 & 1 \end{array} \right).$$
\noindent
See \cite{RotationMatrices} to learn about rotation matrices.

First we draw the latitude lines, which are circles with radius $x$ drawn at height $f(x)$.  To do this, draw a circle centered at the origin with radius $x$, but give Ti{\it k}Z the transformation of the plane that rotates it to give the correct viewing angle and shifts it to height $f(x)$.  To find the affine transformation of the plane that does this, the following matrix rotates three-space counter-clockwise around the $x$-axis.  Since we start with a circle in the $xy$-plane we need to rotate it $90 + \phi$ degrees.

$$R_x(\phi) =
\left( \begin{array}{ccc}
1 & 0 & 0 \\
0 & \cos{\phi} & -\sin{\phi} \\
0 & \sin{\phi} & \cos{\phi} \end{array} \right)$$


Next, project to the $xy$-plane by removing the third row and third column.  This gives the two-by-two matrix 
$$\left( \begin{array}{cc}
1 & 0\\
0 & \cos{\phi}  \end{array} \right)\\$$
that is the transformation of the $xy$-plane that we tell \TikZ{} to apply to our circle.  

\begin{framed}
\noindent {\it Remark:}  On page 253 of the \acro{PGF} manual~\cite{TT} version 2.10, it says that
\begin{verbatim}[\small]
\tikzset{xyplane/.estyle={cm={
   a,b,c,d,(e,f)}}}
\end{verbatim}
\noindent
uses the transformation 
$$\left( \begin{array}{cc}
a & b  \\
c & d \end{array} \right)
\left(\begin{array}{c}
x  \\
 y \end{array}\right) + 
\left( \begin{array}{c}
e  \\
f \end{array} \right).$$

For example:
\begin{center}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (2,2);
\draw (0,0) -- (1,1);
\draw[cm={
   1,1,
   0,1,(0,0)}] (0,0) -- (1,1);
\end{tikzpicture}
\end{center}

\smallskip
\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\ruled]}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (2,2);
\draw (0,0) -- (1,1);
\draw[cm={1,1,0,1,(0,0)}] (0,0) -- (1,1);
\end{tikzpicture}\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize

\vspace{.2cm}
The transformation should map the point (1, 1) to (2, 1) rather than (1, 2) as shown in the figure.  Thus, \TikZ{} (or the underlying \acro{PGF}) is in fact using
$$ \begin{array}{c}
(x\hspace{.3cm} y)  \\
 \hspace{.3cm} \end{array}
\left( \begin{array}{cc}
a & b  \\
c & d \end{array} \right) + 
\left( \begin{array}{c}
e  \\
f \end{array} \right).$$

Since our rotation matrices operate on vectors on the left, this is the same as operating on the right with the transpose of the matrix.  For a rotation matrix, the transpose of the matrix is the inverse of the matrix, which means that the direction of rotation is reversed.  Thus, earlier we said that the rotation matrix $R_x(\psi)$ rotates counterclockwise, but for this application, it rotates clockwise.  

In conclusion, the rotations are clockwise, and if we wish to do a sequence of rotations, the first rotation matrix is on the left.
\end{framed}

Each circle with radius $x$ needs to be shifted up to the correct height $f(x)$, but because of the viewing angle rotation $\phi$, this gets foreshortened to $f(x)\cos{\phi}$.  The following code does this for 9 circles where $x$ is each quarter unit from 0 to 2.
(The ninth is an invisible point at~0.)

\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\ruled]}
\def\fcn{\x^2}
\def\phi{-10}
\foreach \x in {0, .25, ..., 2} {
    \pgfmathsetmacro\yshift{(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={
       1, 0, 0, cos(90 + \phi),(0, \yshift)}}}
    \draw[xyplane, color=black] (0, 0) circle (\x);  
}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize





\begin{figure}[H]
\begin{center}
%%%%%%%%%%%%%%%%%%%%%   Set Function Values  %%%%%%%%%%%%%%%%%%%%%
% Set the x = a and x = b values of the domain here where a <= x <= b.
\def\aDomain{0}
\def\bDomain{2}
% Set the function.  The variable must be \x.  e.g. \x^2.		
%\def\fcn{cos(\x r)}
\def\fcn{\x^2}
%\def\fcn{sqrt(\x)}
% Set min and max values of the function (c <= f(x) <= d). Used for the y-axis.
\def\cRange{0}
\def\dRange{4}

% Set the viewing elevation angle, which is the angle up from horizontal.
\def\phi{-10}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tikzpicture}[scale= 1.0, domain= \aDomain: \bDomain]

 % Back Latitude lines.
 % Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
 % shifted up or down to the correct height.
\foreach \x in {0, .25, ..., 2} {
    \pgfmathsetmacro \yshift{(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0, cos(90 + \phi), (0, \yshift)}}}
   % \draw[xyplane, color=black] (\x, 0) arc (0: 180:\x);  
     \draw[xyplane, color=black] (0, 0) circle (\x);  
}
\end{tikzpicture}
\caption{$f(x) = x^2$ rotated about the $y$-axis.}
\label{rot1a}
\end{center}
\end{figure}


To draw the longitude lines we draw the given function $f(x)$ from 0 to 2, rotate every 30 degrees, and then change the viewing angle by $\phi$ degrees.  Thus we use 
$R_y(\theta)R_x(\phi)  = $
$$\left( \begin{array}{ccc}
\cos{\theta} & 0 & \sin{\theta}  \\
0 & 1 & 0 \\
-\sin{\theta} & 0 & \cos{\theta} \end{array} \right)
\left( \begin{array}{ccc}
1 & 0 & 0 \\
0 & \cos{\phi} & -\sin{\phi} \\
0 & \sin{\phi} & \cos{\phi} \end{array} \right)=$$

$$\left( \begin{array}{ccc}
\cos{\theta} & \sin{\theta}\sin{\phi} & \sin{\theta}\cos{\phi}  \\
0 & \cos{\phi} & -\sin{\phi} \\
-\sin{\theta} & \cos{\theta}\sin{\phi} & \cos{\theta}\cos{\phi} \end{array} \right).$$

\noindent
Next, project to the $xy$-plane by removing the third row and third column, resulting in the $2\times2$ matrix 
$$\left( \begin{array}{cc}
\cos{\theta} & \sin{\theta}\sin{\phi}\\
0 & \cos{\phi}  \end{array} \right)\\.$$

If $\theta$ and $\phi$ are both negated in the above matrix, it does not change.  Thus, we will think of the rotations as being in the counterclockwise direction for positive values of $\theta$ and $\phi$.

This is the transformation of the $xy$-plane that we tell \TikZ{} to apply to the graph of $f(x)$.

\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\ruled]}
\def\fcn{\x^2}
\def\phi{10}
\foreach \theta in {0, 30, ..., 360} {
 \tikzset{xyplane/.estyle={cm={
    cos(\theta),sin(\theta)*sin(\phi),
    0,cos(\phi),(0, 0)}}}
 \draw[xyplane, color=black, thin, smooth]
    plot (\x, \fcn) ;
}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize


%Longitude
\begin{figure}[H]
\begin{center}
%%%%%%%%%%%%%%%%%%%%%   Set Function Values  %%%%%%%%%%%%%%%%%%%%%
% Set the x = a and x = b values of the domain here where a <= x <= b.
\def\aDomain{0}
\def\bDomain{2}
% Set the function.  The variable must be \x.  e.g. \x^2.		
%\def\fcn{cos(\x r)}
\def\fcn{\x^2}
%\def\fcn{sqrt(\x)}
% Set min and max values of the function (c <= f(x) <= d). Used for the y-axis.
\def\cRange{0}
\def\dRange{4}

% Set the viewing elevation angle, which is the angle up from horizontal.
\def\phi{10}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tikzpicture}[scale= 1.1, domain= \aDomain: \bDomain]

% Back Longitude lines.
% Rotates graph around y-axis, then projects to xy-plane.
\foreach \theta in {0, 30, ..., 360} {
 \tikzset{xyplane/.estyle={cm={cos(\theta),  sin(\theta)*sin(\phi), 0,  cos(\phi),  (0, 0)}}}
 \draw[xyplane, color=black, thin, smooth] plot (\x, \fcn) ;
}
 % Back Latitude lines.
 % Top half of a circle, rotated back (around x-axis) 90 - \phi degrees and
 % shifted up or down to the correct height.
\foreach \x in {0, .25, ..., 2} {
    \pgfmathsetmacro\ysh{(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={1, 0, 0,  cos(90 + \phi),  (0, \ysh)}}}
   % \draw[xyplane, color=black] (\x, 0) arc (0: 180:\x);  
     \draw[xyplane, color=black] (0, 0) circle (\x);  
}
\end{tikzpicture}
\caption{$f(x) = x^2$ rotated about the $y$-axis.}
\label{rot1b}
\end{center}
\end{figure}
\noindent
Figure \ref{rot1b} gives the wire frame of the surface of revolution.  The remaining code for creating the final version in figure \ref{rot1} is more of the same.  The coloring of the front consists of the front half of many latitude curves (this slows the code down) that typically look good with opacity set to be less than~1 so that the wire frame on the back shows through a little.


\medskip
\section{Global Positioning System (\texorpdfstring{\acro{GPS}}{GPS}) orbits}
%\vskip-25pt \hrule \vskip25pt \hrule \vskip5pt

As another example, let us draw the orbit of a \acro{GPS} satellite around the earth.  The orbit is inclined 55 degrees from the equator, which is the same as tilted 35 degrees from the north pole.  Thus, we can start with a circle in the $xy$-plane and tilt it 35 degrees toward the viewer.  Thus, the 35 degree tilt is a $-35$ degree clockwise rotation about the $x$-axis.  The rotation matrix about the $x$-axis at an angle $\psi $ in the clockwise direction is 
$$R_x(\psi) = \left( \begin{array}{ccc}
1 & 0 & 0 \\
0 & \cos{\psi} & -\sin{\psi} \\
0 & \sin{\psi} & \cos{\psi} \end{array} \right).$$

Projecting to the $xy$-plane (removing the third row and third column), we have 
$\left( \begin{array}{cc}
1 & 0  \\
0 & \cos{\psi}\end{array} \right)$.  This is the transformation of the $xy$-plane that we tell \TikZ\ to apply to our circle as follows.  The angle $\psi $ should be $-35$ degrees since we are tilting the top of the circle toward the viewer.

\vspace{.9cm}
\begin{center}
\begin{tikzpicture}
 \tikzset{xyplane/.estyle={cm={1,0,0,cos(-35), (0, 0)}}}
 \draw[xyplane, color=black, thin] (0, 0) circle (2) ;
\end{tikzpicture}
\end{center}

%\newpage
\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\ruled]}
\begin{tikzpicture}
 \tikzset{xyplane/.estyle={cm={
    1,0,0,cos(-35), (0, 0)}}}
 \draw[xyplane, color=black, thin]
    (0, 0) circle (2);
\end{tikzpicture}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize
\vspace{.2cm}

\smallskip
\noindent
Now we wish to rotate our tilted orbit $\theta$ degrees around the $y$-axis.  
%The rotation matrix (all rotations are in the clockwise direction) is $$R_y(\theta) = \left( \begin{array}{ccc}
%\cos{\theta} & 0 & \sin{\theta}  \\
%0 & 1 & 0 \\
%-\sin{\theta} & 0 & \cos{\theta} \end{array} \right).$$  
To rotate first $\psi$ degrees about the $x$-axis and then $\theta$ degrees about the $y$-axis, we compute 

\vspace{4pt}%
\noindent $\displaystyle R_x(\psi)R_y(\theta) =$ 
\vspace{-6pt}%
%$$\left( \begin{array}{ccc}
%1 & 0 & 0 \\
%0 & \cos{\psi} & -\sin{\psi} \\
%0 & \sin{\psi} & \cos{\psi} \end{array} \right)
%\left( \begin{array}{ccc}
%\cos{\theta} & 0 & \sin{\theta}  \\
%0 & 1 & 0 \\
%-\sin{\theta} & 0 & \cos{\theta} \end{array} \right) = $$
$$\hphantom{34pt}\left( \begin{array}{ccc}
\cos{\theta} & 0 & \sin{\theta}  \\
\sin{\psi}\sin{\theta} & \cos{\psi} & -\sin{\psi}\cos{\theta} \\
-\cos{\psi}\sin{\theta} & \sin{\psi} & \cos{\psi}\cos{\theta} \end{array} \right).$$ 
\noindent
Next, project to the $xy$-plane, which is the transformation 
$$\left( \begin{array}{cc}
\cos{\theta} & 0  \\
\sin{\psi}\sin{\theta} & \cos{\psi} \end{array} \right).$$

If $\theta$, $\psi$, and $\phi$ are all negated in the above matrix, it does not change.  Thus, we will think of the rotations as being in the counterclockwise direction for positive values of $\theta$, $\psi$, and $\phi$.

\vspace{.2cm}
\begin{center}
\begin{tikzpicture}
  \tikzset{xyplane/.estyle={cm={cos(90),  0,  sin(35)*sin(90), cos(35) ,  (0, 0)}}}
  \draw[xyplane] (0, 0) circle (2) ;
\end{tikzpicture}
\end{center}

\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\ruled]}
\begin{tikzpicture}
 \tikzset{xyplane/.estyle={cm={
    cos(90),0,sin(35)*sin(90),cos(35),(0, 0)}}}
 \draw[xyplane] (0, 0) circle (2);
\end{tikzpicture}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize


\vspace{.2cm}\noindent
Now suppose that we wish to view this from above and thus rotate some angle $\phi$ around the $x$-axis to tilt the top of the orbit toward the viewer.  This is the product 
$R_x(\psi)R_y(\theta)R_x(\phi)$
%\left( \begin{array}{ccc}
%\cos{\theta} & 0 & \sin{\theta}  \\
%\sin{\psi}\sin{\theta} & \cos{\psi} & -\sin{\psi}\cos{\theta} \\
%-\cos{\psi}\sin{\theta} & \sin{\psi} & \cos{\psi}\cos{\theta} \end{array} \right)
%\left( \begin{array}{ccc}
%1 & 0 & 0 \\
%0 & \cos{\phi} & -\sin{\phi} \\
%0 & \sin{\phi} & \cos{\phi} \end{array} \right)=
%$$\left( \begin{array}{ccc}
%\cos{\theta} &  \sin{\theta}\sin{\phi} & \sin{\theta}\cos{\phi}  \\
%\sin{\psi}\sin{\theta} & \cos{\psi}\cos{\phi} - \sin{\psi}\cos{\theta}\sin{\phi}& -\cos{\psi}\cos{\phi} - \sin{\psi}\cos{\theta}\sin{\phi} \\
%-\cos{\psi}\sin{\theta} & \sin{\psi}\cos{\phi} + \cos{\psi}\cos{\theta}\sin{\phi} & -\sin{\psi}\sin{\phi} + \cos{\psi}\cos{\theta}\cos{\phi} \end{array} \right).$$   
that has the projected matrix
$$\left( \begin{array}{cc}
\cos{\theta} &  \sin{\theta}\sin{\phi} \\
\sin{\psi}\sin{\theta} & \cos{\psi}\cos{\phi} - \sin{\psi}\cos{\theta}\sin{\phi} \end{array} \right).$$

Again, if $\theta$, $\psi$, and $\phi$ are all negated in the above matrix, it does not change.  Thus, we will think of the rotations as being in the counterclockwise direction for positive values of $\theta$, $\psi$, and $\phi$.

With $\psi=35$, $\theta =90$, and $\phi =20$, we rotate the top of the above circle (that looks like a line) toward the viewer 20 degrees so it looks like an ellipse again.

\vspace{.2cm}
\begin{center}
\begin{tikzpicture}
 \tikzset{xyplane/.estyle={cm={cos(90),  sin(90)*sin(20), sin(35)*sin(90),  cos(35)*cos(20) - sin(35)*cos(90)*sin(20),  (0, 0)}}}
 \draw[xyplane] (0, 0) circle (2) ;
\end{tikzpicture}
\end{center}


\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\ruled]}
\begin{tikzpicture}
  \tikzset{xyplane/.estyle={cm={
    cos(90),sin(90)*sin(20),sin(35)*sin(90),
    cos(35)*cos(20)-sin(35)*cos(90)*sin(20),(0,0)}}}
  \draw[xyplane] (0, 0) circle (2);
\end{tikzpicture}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize
\vspace{.2cm}
As an example of the above rotation and projection matrix, we draw the \acro{U.S.} \acro{GPS} (Global Positioning System) satellite orbits.  There are six orbits, each tilted $\psi$ = 35 degrees from the north pole and then rotated to be positioned every 60 degrees ($\theta$) around the earth.  These orbits can then be viewed from different angles $\phi$.  Figure~\ref{gps1} shows a view from the equator.  The code is included in appendix \ref{gps1Code}.
\enlargethispage{-7pt}

%%%%%%%%%%%%%%%%%%%11111111111111111111111111111111111111111111111
\begin{figure}[H]
\begin{center}
\begin{tikzpicture}[scale=.77] % GPS satellite orbits.
\def\R{1.4} % sphere radius
\def\orbitRadius{3.172*\R}
\def\angEl{1} % elevation angle
\def\x{0} % x coordinate of center
\def\y{0} % y coordinate of center
\def\z{0} % z coordinate of center
\pgfmathsetmacro\psi{35}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\firstTheta{-135}
\pgfmathsetmacro\phi{\angEl} % Third, rotate about the x-axis.
\ifthenelse{\boolean{color}}
{\draw[color=red, fill=blue, opacity=.15] (0, 0) circle (\orbitRadius);}
{}
%{\draw[color=black] (0, 0) circle (\orbitRadius);}
% Set the variables, theta, c = color, angVis, and \thetaSatShift for each of 6 orbits.
\ifthenelse{\boolean{color}}
{\foreach \theta/\c in {\firstTheta/red, \firstTheta+60/blue, \firstTheta+2*60/green, \firstTheta+3*60/black, \firstTheta+4*60/cyan, \firstTheta+5*60/brown}}
{\foreach \theta/\c in {\firstTheta/black, \firstTheta+60/black, \firstTheta+2*60/black, \firstTheta+3*60/black, \firstTheta+4*60/black, \firstTheta+5*60/black}}
{
     % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
    % Draw the back half of the orbit.
    \getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
   \pgfmathtruncatemacro\angleInt{\angle}
    \ifthenelse{\angleInt < -180}
    {\pgfmathsetmacro\angleInt{\angleInt + 360}}
    {}
    \draw[xyplane, dashed, color=\c] (\angleInt -180: \orbitRadius) arc (\angleInt -180: \angle: \orbitRadius);% node[right] {theta= \theta , R=\angleInt};
}
% Draw the earth.
\tikzset{current plane/.estyle={cm={1,  0,   0,  1,  (0, 0)}}}
\ifthenelse{\boolean{color}}
{\filldraw[current plane][shift={(\x, \y)}][ball color=blue, opacity=.7] (0,0,0) circle (\R);}
{\filldraw[current plane][shift={(\x, \y)}][ball color=lightgray] (0,0,0) circle (\R);}
\foreach \t in {-80,-60,...,80} { \DrawLatitudeCircle[\R]{\t}{\x}{\y}}
\foreach \t in {-5,-35,...,-175} { \DrawLongitudeCircle[\R]{\t}{\x}{\y}}
\pgfmathsetmacro\orbitBaseAngle{30}

% Draw the front half of the orbit.
\ifthenelse{\boolean{color}}
{\foreach \theta/\c in {\firstTheta/red, \firstTheta+60/blue, \firstTheta+2*60/green, \firstTheta+3*60/black, \firstTheta+4*60/cyan, \firstTheta+5*60/brown}}
{\foreach \theta/\c in {\firstTheta/black, \firstTheta+60/black, \firstTheta+2*60/black, \firstTheta+3*60/black, \firstTheta+4*60/black, \firstTheta+5*60/black}}
{
	% Set the drawing plane affine transformation again.
	 \tikzset{xyplane/.estyle={cm={cos(\theta),  sin(\theta)*sin(\phi), sin(\theta)*sin(\psi),  cos(\psi)*cos(\phi) - sin(\psi)*cos(\theta)*sin(\phi),  (0, 0)}}}
	% Draw the front half of the orbit.
	\getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
	\pgfmathtruncatemacro\angleInt{\angle}
	\ifthenelse{\angleInt > 180}
    		{\pgfmathsetmacro\angleInt{\angleInt - 360}}
    		{}
	\draw[xyplane, very thick, color=\c] (\angleInt :  \orbitRadius) arc (\angleInt: \angleInt +180 : \orbitRadius);% node[left] {\angleInt};
    	% Draw the satellites.
    	\foreach \thetaSat in {\orbitBaseAngle, \orbitBaseAngle + 30, \orbitBaseAngle + 135, \orbitBaseAngle + 255} {
        	\pgfmathsetmacro\xsh{(7/1)* \orbitRadius*cos(\thetaSat)}
       	 	\pgfmathsetmacro\ysh{(7/1)* \orbitRadius*sin(\thetaSat)}
        	\draw[xyplane, color=\c, scale=1/7][shift={(\xsh, \ysh)}] (-2,-1) grid (2,1);
   	}
}
\end{tikzpicture}
\ifthenelse{\boolean{color}}
{\caption{The U.S. \acro{GPS} system.  This view looking directly at the equator shows that the orbits never pass over the north or south poles.  In order, they are colored, red, blue, green, black, cyan, and brown.   Each orbit has four satellites spaced 30 degrees, 105 degrees, 120 degrees, and 105 degrees apart.}}
{\caption{The \acro{U.S.} \acro{GPS} system.  This view, looking directly at the equator, shows that the orbits never pass over the north or south poles.  Each orbit has four satellites spaced 30 degrees, 105 degrees, 120 degrees, and 105 degrees apart.}}
\label{gps1}
\end{center}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



 By changing the viewing angle \cs{angEl} = $\psi$, we can get additional views.  See figures~\ref{gps2} 
 %~\ref{gps3}, 
 and ~\ref{gps4} for two such views.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{figure}[H]
\begin{center}
\begin{tikzpicture}[scale=.77] % GPS satellite orbits.
\def\R{1.4} % sphere radius
\def\orbitRadius{3.172*\R}
\def\angEl{30} % elevation angle
\def\x{0} % x coordinate of center
\def\y{0} % y coordinate of center
\def\z{0} % z coordinate of center
\pgfmathsetmacro\psi{35}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\firstTheta{-135}
\pgfmathsetmacro\phi{\angEl} % Third, rotate about the x-axis.
\ifthenelse{\boolean{color}}
{\draw[color=red, fill=blue, opacity=.15] (0, 0) circle (\orbitRadius);}
{}
%{\draw[color=black] (0, 0) circle (\orbitRadius);}
% Set the variables, theta, c = color, angVis, and \thetaSatShift for each of 6 orbits.
\ifthenelse{\boolean{color}}
{\foreach \theta/\c in {\firstTheta/red, \firstTheta+60/blue, \firstTheta+2*60/green, \firstTheta+3*60/black, \firstTheta+4*60/cyan, \firstTheta+5*60/brown}}
{\foreach \theta/\c in {\firstTheta/black, \firstTheta+60/black, \firstTheta+2*60/black, \firstTheta+3*60/black, \firstTheta+4*60/black, \firstTheta+5*60/black}}
{
     % Set the drawing plane affine transformation.
\tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
    % Draw the back half of the orbit.
    \getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
   \pgfmathtruncatemacro\angleInt{\angle}
    \ifthenelse{\angleInt < -180}
    {\pgfmathsetmacro\angleInt{\angleInt + 360}}
    {}
    \draw[xyplane, dashed, color=\c] (\angleInt -180: \orbitRadius) arc (\angleInt -180: \angle: \orbitRadius);% node[right] {theta= \theta , R=\angleInt};
}
% Draw the earth.
\tikzset{current plane/.estyle={cm={1,  0,   0,  1,  (0, 0)}}}
\ifthenelse{\boolean{color}}
{\filldraw[current plane][shift={(\x, \y)}][ball color=blue, opacity=.7] (0,0,0) circle (\R);}
{\filldraw[current plane][shift={(\x, \y)}][ball color=lightgray] (0,0,0) circle (\R);}
\foreach \t in {-80,-60,...,80} { \DrawLatitudeCircle[\R]{\t}{\x}{\y}}
\foreach \t in {-5,-35,...,-175} { \DrawLongitudeCircle[\R]{\t}{\x}{\y}}
\pgfmathsetmacro\orbitBaseAngle{30}
% Draw the front half of the orbit.
\ifthenelse{\boolean{color}}
{\foreach \theta/\c in {\firstTheta/red, \firstTheta+60/blue, \firstTheta+2*60/green, \firstTheta+3*60/black, \firstTheta+4*60/cyan, \firstTheta+5*60/brown}}
{\foreach \theta/\c in {\firstTheta/black, \firstTheta+60/black, \firstTheta+2*60/black, \firstTheta+3*60/black, \firstTheta+4*60/black, \firstTheta+5*60/black}}
{
	% Set the drawing plane affine transformation again.
	 \tikzset{xyplane/.estyle={cm={cos(\theta),  sin(\theta)*sin(\phi), sin(\theta)*sin(\psi),  cos(\psi)*cos(\phi) - sin(\psi)*cos(\theta)*sin(\phi),  (0, 0)}}}
	% Draw the front half of the orbit.
	\getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
	\pgfmathtruncatemacro\angleInt{\angle}
	\ifthenelse{\angleInt > 180}
    		{\pgfmathsetmacro\angleInt{\angleInt - 360}}
    		{}
	\draw[xyplane, very thick, color=\c] (\angleInt :  \orbitRadius) arc (\angleInt: \angleInt +180 : \orbitRadius);% node[left] {\angleInt};
    	% Draw the satellites.
    	\foreach \thetaSat in {\orbitBaseAngle, \orbitBaseAngle + 30, \orbitBaseAngle + 135, \orbitBaseAngle + 255} {
        	\pgfmathsetmacro\xsh{(7/1)* \orbitRadius*cos(\thetaSat)}
       	 	\pgfmathsetmacro\ysh{(7/1)* \orbitRadius*sin(\thetaSat)}
        	\draw[xyplane, color=\c, scale=1/7][shift={(\xsh, \ysh)}] (-2,-1) grid (2,1);
   	}
}
\end{tikzpicture}
\caption{Looking down on the earth, 30 degrees above the equator.  Dotted lines are on the back side of the orbit.}
\label{gps2}
\end{center}
\end{figure}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[H]
\begin{center}
\begin{tikzpicture}[scale=.77] % GPS satellite orbits.
\def\R{1.4} % sphere radius
\def\orbitRadius{3.172*\R}
\def\angEl{89.9} % elevation angle
\def\x{0} % x coordinate of center
\def\y{0} % y coordinate of center
\def\z{0} % z coordinate of center
\pgfmathsetmacro\psi{35}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\firstTheta{-135}
\pgfmathsetmacro\phi{\angEl} % Third, rotate about the x-axis.
\ifthenelse{\boolean{color}}
{\draw[color=red, fill=blue, opacity=.15] (0, 0) circle (\orbitRadius);}
{}
%{\draw[color=black] (0, 0) circle (\orbitRadius);}
% Set the variables, theta, c = color, angVis, and \thetaSatShift for each of 6 orbits.
\ifthenelse{\boolean{color}}
{\foreach \theta/\c in {\firstTheta/red, \firstTheta+60/blue, \firstTheta+2*60/green, \firstTheta+3*60/black, \firstTheta+4*60/cyan, \firstTheta+5*60/brown}}
{\foreach \theta/\c in {\firstTheta/black, \firstTheta+60/black, \firstTheta+2*60/black, \firstTheta+3*60/black, \firstTheta+4*60/black, \firstTheta+5*60/black}}
{
     % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
    % Draw the back half of the orbit.
    \getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
   \pgfmathtruncatemacro\angleInt{\angle}
    \ifthenelse{\angleInt < -180}
    {\pgfmathsetmacro\angleInt{\angleInt + 360}}
    {}
    \draw[xyplane, dashed, color=\c] (\angleInt -180: \orbitRadius) arc (\angleInt -180: \angle: \orbitRadius);% node[right] {theta= \theta , R=\angleInt};
}
% Draw the earth.
\tikzset{current plane/.estyle={cm={1,  0,   0,  1,  (0, 0)}}}
\ifthenelse{\boolean{color}}
{\filldraw[current plane][shift={(\x, \y)}][ball color=blue, opacity=.7] (0,0,0) circle (\R);}
{\filldraw[current plane][shift={(\x, \y)}][ball color=lightgray] (0,0,0) circle (\R);}
\foreach \t in {-80,-60,...,80} { \DrawLatitudeCircle[\R]{\t}{\x}{\y}}
\foreach \t in {-5,-35,...,-175} { \DrawLongitudeCircle[\R]{\t}{\x}{\y}}
\pgfmathsetmacro\orbitBaseAngle{30}

% Draw the front half of the orbit.
\ifthenelse{\boolean{color}}
{\foreach \theta/\c in {\firstTheta/red, \firstTheta+60/blue, \firstTheta+2*60/green, \firstTheta+3*60/black, \firstTheta+4*60/cyan, \firstTheta+5*60/brown}}
{\foreach \theta/\c in {\firstTheta/black, \firstTheta+60/black, \firstTheta+2*60/black, \firstTheta+3*60/black, \firstTheta+4*60/black, \firstTheta+5*60/black}}
{
	% Set the drawing plane affine transformation again.
	 \tikzset{xyplane/.estyle={cm={cos(\theta),  sin(\theta)*sin(\phi), sin(\theta)*sin(\psi),  cos(\psi)*cos(\phi) - sin(\psi)*cos(\theta)*sin(\phi),  (0, 0)}}}
	% Draw the front half of the orbit.
	\getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
	\pgfmathtruncatemacro\angleInt{\angle}
	\ifthenelse{\angleInt > 180}
    		{\pgfmathsetmacro\angleInt{\angleInt - 360}}
    		{}
	\draw[xyplane, very thick, color=\c] (\angleInt :  \orbitRadius) arc (\angleInt: \angleInt +180 : \orbitRadius);% node[left] {\angleInt};
    	% Draw the satellites.
    	\foreach \thetaSat in {\orbitBaseAngle, \orbitBaseAngle + 30, \orbitBaseAngle + 135, \orbitBaseAngle + 255} {
        	\pgfmathsetmacro\xsh{(7/1)* \orbitRadius*cos(\thetaSat)}
       	 	\pgfmathsetmacro\ysh{(7/1)* \orbitRadius*sin(\thetaSat)}
        	\draw[xyplane, color=\c, scale=1/7][shift={(\xsh, \ysh)}] (-2,-1) grid (2,1);
   	}
}
\end{tikzpicture}
\caption{Looking directly down on the north pole.  Pairs of orbits overlap in this view, but the front and back parts of these orbits are on opposite sides of the earth.}
\label{gps4}
\end{center}
\end{figure}

For comparison, figure~\ref{gps5} is the configuration of satellites that the European Union is using for the \acro{GPS} system that they are currently implementing that has just three orbits with nine satellites in each.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[t]
\begin{center}
\begin{tikzpicture}[scale=.77] % GPS satellite orbits.
\def\R{1.4} % sphere radius
\def\orbitRadius{3.172*\R}
\def\angEl{89.9} % elevation angle
\def\x{0} % x coordinate of center
\def\y{0} % y coordinate of center
\def\z{0} % z coordinate of center
\pgfmathsetmacro\psi{34}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\firstTheta{-121}
\pgfmathsetmacro\phi{\angEl} % Third, rotate about the x-axis.
\ifthenelse{\boolean{color}}
{\draw[color=red, fill=blue, opacity=.15] (0, 0) circle (\orbitRadius);}
{}
%{\draw[color=black] (0, 0) circle (\orbitRadius);}
% Set the variables, theta, c = color, angVis, and \thetaSatShift for each of 6 orbits.
\ifthenelse{\boolean{color}}
{\foreach \theta/\c in {\firstTheta/red, \firstTheta+120/blue, \firstTheta+2*120/black}}
{\foreach \theta/\c in {\firstTheta/black, \firstTheta+120/black, \firstTheta+2*120/black}}
{
     % Set the drawing plane affine transformation.
\tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
    % Draw the back half of the orbit.
    \getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
   \pgfmathtruncatemacro\angleInt{\angle}
    \ifthenelse{\angleInt < -180}
    {\pgfmathsetmacro\angleInt{\angleInt + 360}}
    {}
    \draw[xyplane, dashed, color=\c] (\angleInt -180: \orbitRadius) arc (\angleInt -180: \angle: \orbitRadius);% node[right] {theta= \theta , R=\angleInt};
}
% Draw the earth.
\tikzset{current plane/.estyle={cm={1,  0,   0,  1,  (0, 0)}}}
\ifthenelse{\boolean{color}}
{\filldraw[current plane][shift={(\x, \y)}][ball color=blue, opacity=.7] (0,0,0) circle (\R);}
{\filldraw[current plane][shift={(\x, \y)}][ball color=lightgray] (0,0,0) circle (\R);}
\foreach \t in {-80,-60,...,80} { \DrawLatitudeCircle[\R]{\t}{\x}{\y}}
\foreach \t in {-5,-35,...,-175} { \DrawLongitudeCircle[\R]{\t}{\x}{\y}}
\pgfmathsetmacro\orbitBaseAngle{30}

% Draw the front half of the orbit.
\ifthenelse{\boolean{color}}
{\foreach \theta/\c in {\firstTheta/red, \firstTheta+120/blue, \firstTheta+2*120/black}}
{\foreach \theta/\c in {\firstTheta/black, \firstTheta+120/black, \firstTheta+2*120/black}}
{
	% Set the drawing plane affine transformation again.
	 \tikzset{xyplane/.estyle={cm={cos(\theta),  sin(\theta)*sin(\phi), sin(\theta)*sin(\psi),  cos(\psi)*cos(\phi) - sin(\psi)*cos(\theta)*sin(\phi),  (0, 0)}}}
	% Draw the front half of the orbit.
	\getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
	\pgfmathtruncatemacro\angleInt{\angle}
	\ifthenelse{\angleInt > 180}
    		{\pgfmathsetmacro\angleInt{\angleInt - 360}}
    		{}
	\draw[xyplane, very thick, color=\c] (\angleInt :  \orbitRadius) arc (\angleInt: \angleInt +180 : \orbitRadius);% node[left] {\angleInt};
    	% Draw the satellites.
    	\foreach \thetaSat in {\orbitBaseAngle, \orbitBaseAngle + 40, \orbitBaseAngle + 80, \orbitBaseAngle + 120, \orbitBaseAngle + 160, \orbitBaseAngle + 200, \orbitBaseAngle + 240, \orbitBaseAngle + 280, \orbitBaseAngle + 320} {
        	\pgfmathsetmacro\xsh{(7/1)* \orbitRadius*cos(\thetaSat)}
       	 	\pgfmathsetmacro\ysh{(7/1)* \orbitRadius*sin(\thetaSat)}
        	\draw[xyplane, color=\c, scale=1/7][shift={(\xsh, \ysh)}] (-2,-1) grid (2,1);
   	}
}
\end{tikzpicture}
\caption{The European Union's planned \acro{GPS} has three orbital planes, 120 degrees apart, inclined at 56 degrees, that divide the earth's surface into eight congruent spherical triangles.  Each of the three orbits has nine satellites, equally spaced, 40 degrees apart.}
\label{gps5}
\end{center}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Intersections of spheres}

Rotations around an axis other than the coordinate axes can also be useful.  Consider figure~\ref{intersectingSpheres}.
%%%%%%%%%%%%%%%%%%%%%%%%%6666666666666666666666666666666666666
\begin{figure}[t]
\centering
\begin{tikzpicture}[scale=.8] % The intersection of two spheres.
%%Draw the first sphere.
\def\Rb{3.1} % sphere radius
\def\angEl{30} % elevation angle
\def\xb{3} % x coordinate of center
\def\yb{3} % y coordinate of center
\def\zb{-1} % z coordinate of center
\ifthenelse{\boolean{color}}
{\filldraw[shift={(\xb, \yb)}][ball color= blue,opacity=1] (0, 0, 0) circle (\Rb);}
{\filldraw[shift={(\xb, \yb)}][ball color= lightgray!10,opacity=1] (0, 0, 0) circle (\Rb);}
\foreach \t in {-60,-20,...,80} { \DrawLatitudeCircle[\Rb]{\t}{\xb}{\yb}}
\foreach \t in {-5,-45,...,-175} { \DrawLongitudeCircle[\Rb]{\t}{\xb}{\yb}}
%Draw the second sphere.
\def\Rc{2.4} % sphere radius
\def\angEl{30} % elevation angle
\def\xc{0} % x coordinate of center
\def\yc{0} % y coordinate of center
\def\zc{0} % z coordinate of center
\ifthenelse{\boolean{color}}
{\filldraw[shift={(\xc, \yc)}][ball color= red, opacity=1] (0,0,0) circle (\Rc);}
{\filldraw[shift={(\xc, \yc)}][ball color= lightgray!20, opacity=1] (0,0,0) circle (\Rc);}
\foreach \t in {-60,-20,...,80} { \DrawLatitudeCircle[\Rc]{\t}{\xc}{\yc}}
\foreach \t in {-5,-45,...,-175} { \DrawLongitudeCircle[\Rc]{\t}{\xc}{\yc}}

\ifthenelse{\boolean{color}}
{\drawIntersectionOfSpheres{\xc}{\yc}{\zc}{\xb}{\yb}{\zb}{\Rc}{\Rb}{yellow}}
{\drawIntersectionOfSpheres{\xc}{\yc}{\zc}{\xb}{\yb}{\zb}{\Rc}{\Rb}{black}}

\end{tikzpicture}
\ifthenelse{\boolean{color}}
{\caption{These two spheres intersect in the yellow circle.}}
{\caption{These two spheres intersect in the bold circle.}}
\label{intersectingSpheres}
\end{figure}



  The goal was to draw the intersection of the two spheres.  The plane of the circle of intersection is perpendicular to the vector between the two sphere centers.  Thus to find the circle of intersection we rotate three-space so that the $z$-axis (which is perpendicular to the $xy$-plane) rotates to be parallel to the vector between the sphere centers.  Thus the rotation axis is perpendicular to both of these vectors and we compute it by taking the cross product of them.  Then we use the rotation matrix about this vector (see \cite{RotationMatrices}), project to the $xy$-plane, and then shift to the correct position.  This figure needs more work, since it does not show the overlapping parts of the spheres correctly, but it is still an example of how to find and draw the intersection using these techniques.  The code for figure \ref{intersectingSpheres} is included in appendix \ref{sphereInt}.

\section{Conclusion}

The \acro{PGF} and \TikZ{} languages are predominantly for two-dimensional graphics, but with an understanding of a few rotation matrices, some three-dimensional graphics can be drawn fairly easily.

\begin{appendix}
\section{Rotation about the \texorpdfstring{$y$}{y}-axis}
\label{rotyCode}
Code for figure \ref{rot1}.  The function $f(x) = x^2$ is rotated about the $y$-axis.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode

\let\appruled=\relax
\smallskip

\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\appruled]}
\begin{figure}[h]
\begin{center}
%%%%%%%%   Set function values  %%%%%%%
% Set the x = a and x = b values of the 
% domain here where a <= x <= b.
\def\aDomain{0}
\def\bDomain{2}
% Set the function.  
% The variable must be \x, e.g. \x^2.		
\def\fcn{\x^2}
% Set min and max values of the function
% (c <= f(x) <= d). Used for the y-axis.
\def\cRange{0}
\def\dRange{4}
% Set the color of the back half.  
% This can look good as a different color
% if it looks like the inside.	
\def\backColor{brown}
% Set the color of the front half.  lightgray looks
% good for both back and front.	 
\def\frontColor{red}
% Set the number of shading circles to draw. 
% More gives a more even color.
% Enter 1 for no shading.	
\def\xShadingSteps{300}
% Set the number of x radius grid circles.
\def\xGridSteps{8}
% Set the number of radial grid lines.	
\def\rotationGridSteps{12}	
% Set the viewing elevation angle,
% which is the angle up from horizontal.
\def\phi{10}
%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfmathsetmacro\scaleAttempt{4/\bDomain}
\begin{tikzpicture}[scale= \scaleAttempt, 
   domain= \aDomain: \bDomain]
\pgfmathsetmacro\intervalLength{\bDomain - \aDomain}
\pgfmathsetmacro\xGridStepsize{
   \intervalLength/\xGridSteps}
\pgfmathsetmacro\xShadingStepsize{
   \intervalLength/\xShadingSteps}
\pgfmathsetmacro\rotationGridStepsize{
   360/\rotationGridSteps}
% Draw the shading of the back half.
% Top half of a circle, rotated back (around x-axis)
% 90 - \phi degrees and shifted up or down
% to the correct height.
\pgfmathsetmacro\nextShadingStep{
   \aDomain + \xShadingStepsize}
\foreach \x in 
   {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={
       1,0,0,cos(90-\phi), (0, \ysh)}}}
    \draw[xyplane,\backColor,ultra thick,opacity=1] 
       (\x, 0) arc (0:180:\x);
}
% Back longitude lines.
% Rotates graph around y-axis, then
% projects to xy-plane.
\foreach \theta in 
   {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={
    cos(-\theta), sin(-\theta)*sin(-\phi), 
    0, cos(-\phi),  (0, 0)}}}
 \draw[xyplane, smooth] plot (\x, \fcn);
}
% Back latitude lines.
% Top half of a circle, rotated back
% (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
\pgfmathsetmacro\nextStep{\aDomain + \xGridStepsize}
\foreach \x in {\aDomain,\nextStep, ...,\bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={
       1,0,0,cos(90-\phi), (0, \ysh)}}}
    \draw[xyplane] (\x, 0) arc (0: 180:\x);  
}
% Draw the axis.
\pgfmathsetmacro\yHeight{
   \dRange + \bDomain*sin(\phi) + .5}
\draw[->] (0, \cRange - .5) -- (0, \yHeight)
   node[above] {$y$};
% Comment out the next four commands
% if you don't want an x-axis, and labels.
\draw[<->] (-\bDomain -.5, 0) -- (\bDomain + .5, 0)
   node[right] {$x$};
\draw (\bDomain, .1) -- (\bDomain, -.1)
   node[below] {\bDomain};
\pgfmathsetmacro\yLabel {cos(\phi)* \dRange}
\draw (-.1, \yLabel) -- (.1, \yLabel)
   node[right] {\dRange};
   
% Draw the shading of the front half.
% Top half of a circle, rotated back (around x-axis) 
% 90 - \phi degrees and shifted up or down
% to the correct height.
\foreach \x in 
   {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={
       1,0,0,cos(90-\phi), (0, \ysh)}}}
    \draw[xyplane,\frontColor,ultra thick,
       opacity=.6]
       (-\x, 0) arc (-180:0:\x); 
}
% Front longitude lines.
\foreach \theta in 
   {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={
    cos(\theta), sin(\theta)*sin(-\phi), 
    0, cos(-\phi),  (0, 0)}}}
 \draw[xyplane,smooth] plot (\x, \fcn);
}
% Front latitude lines.
% Bottom half of a circle, rotated back 
% (around x-axis) 90 - \phi degrees and
% shifted up or down to the correct height.
\foreach \x in {\aDomain, \nextStep, ..., \bDomain}{
    \pgfmathsetmacro\ysh {(cos(\phi))*(\fcn)}
    \tikzset{xyplane/.estyle={cm={
       1,0,0,cos(90-\phi),(0, \ysh)}}}
    \draw[xyplane] (-\x, 0) arc (-180: 0:\x); 
}
\end{tikzpicture}
\caption{Rotate $f(x) = x^2$ about the $y$-axis.}
\end{center}
\end{figure}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize
%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode
%\begin{framed}
\smallskip
\noindent
{\it Remark:} Since the back and front shading is drawn by drawing circles that are rotated in three-space, and the above code uses 300 such circles, it is slow (about 3 seconds).
Thus, when working with the document, it is useful to set \cs{xShadingSteps} to 1 and then change it to 300 for the final version.
%\end{framed}


\section{Rotation about the \texorpdfstring{$x$}{x}-axis}
\label{rotxCode}
Code for figure \ref{rot1x}.  The function $\sin{x}$ is rotated about the $x$-axis.

%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode
\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\appruled]}
\begin{figure}[h]
\begin{center}
%%%%%%%%   Set function values  %%%%%%%
% Set the x = a and x = b values of the
% domain here where a <= x <= b.
\def\aDomain{0}
\def\bDomain{3.14159}
% Set the function.  
% The variable must be \x, e.g. \x^2.		
\def\fcn{sin(\x r)}
%\def\fcn{sqrt(\x)}
% Set min and max values of the function
% (c <= f(x) <= d). Used for the y-axis.
\def\cRange{0}
\def\dRange{1}
% Set the color of the back half.  
% This can look good as a different color
% if it looks like the inside.	
\def\backColor{red!70!black}
% Set the color of the front half.  lightgray looks
% good for both back and front.	 
\def\frontColor{red!70!black}
% Set the number of shading circles to draw. 
% More gives a more even color.  Enter 1 for
% no shading; a large number makes it slow. 
% Use the following two lines while editing and then
% change the speed to 100 for the final version.
%\def\speed{1}
%\pgfmathsetmacro\xShadingSteps{3* \speed}
\pgfmathsetmacro\xShadingSteps{300}
% Set the number of x radius grid circles.
\def\xGridSteps{8}
% Set the number of radial grid lines.	
\def\rotationGridSteps{18}	
% Set the viewing elevation angle,
% which is the angle up from horizontal.
\def\phi{15}
%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfmathsetmacro\scaleAttempt{3.4/\dRange}
\begin{tikzpicture}[scale= \scaleAttempt, 
   domain= \aDomain: \bDomain]
\pgfmathsetmacro\intervalLength{\bDomain - \aDomain}
\pgfmathsetmacro\xGridStepsize{
   \intervalLength/\xGridSteps}
\pgfmathsetmacro\xShadingStepsize{
   \intervalLength/\xShadingSteps}
\pgfmathsetmacro\rotationGridStepsize{
   360/\rotationGridSteps}
% Draw the shading of the back half.
% Left half of a circle, rotated right
% (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
\pgfmathsetmacro\nextShadingStep{
   \aDomain + \xShadingStepsize}
\foreach \x in 
  {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\xsh{(cos(\phi))*({\x})}
    \pgfmathsetmacro\rad{(\fcn)}
    \tikzset{xyplane/.estyle={cm={
       cos(\phi - 90), 0,0,1, (\xsh, 0)}}}
    \draw[xyplane,\backColor,ultra thick,opacity=.6]
       (0, \rad) arc (90 : 270 : \rad);
}
% Back longitude lines.
% Rotates graph around y-axis,
% then projects to xy-plane.
\foreach \theta in 
   {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={
    cos(\phi), 0, sin(\theta)*sin(\phi), 
    cos(\theta),  (0, 0)}}}
 \draw[xyplane,smooth] plot (\x, \fcn) ;
}
% Back latitude lines.
% Left half of a circle, rotated right
% (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
\pgfmathsetmacro\nextStep{\aDomain + \xGridStepsize}
\foreach \x in {\aDomain,\nextStep, ...,\bDomain} {
    \pgfmathsetmacro\xsh{(cos(\phi))*({\x})}
    \pgfmathsetmacro\rad{(\fcn)}
    \tikzset{xyplane/.estyle={cm={
       cos(\phi - 90), 0,0,1,(\xsh, 0)}}}
    \draw[xyplane,black,thin,opacity=1]
       (0, \rad) arc (90 : 270 : \rad); 
}
% Draw the axis.
\pgfmathsetmacro\xdim{
   \bDomain + \dRange*sin(\phi) + .5}
\draw[->] (0, -\dRange - .5) -- (0, \dRange + .5)
   node[above] {$y$};
% Comment out the next four commands
% if you don't want an x-axis, and labels.
\draw[<->] (\aDomain -.5, 0) -- (\xdim, 0)
   node[right] {$x$};
\pgfmathsetmacro\xLabel{cos(\phi)*\bDomain}
\draw (\xLabel, .1) -- (\xLabel, -.1)
   node[below right] {\bDomain};
\draw (-.1, \dRange) -- (.1, \dRange)
   node[right] {\dRange};
   
% Draw the shading of the front half.
% Right half of a circle, rotated right
% (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
\foreach \x in
  {\aDomain, \nextShadingStep, ..., \bDomain} {
    \pgfmathsetmacro\xsh{(cos(\phi))*({\x})}
    \pgfmathsetmacro\rad{(\fcn)}
    \tikzset{xyplane/.estyle={cm={
       cos(\phi - 90),0,0,1,(\xsh, 0)}}}
   \draw[xyplane,\frontColor,ultra thick,opacity=.6]
       (0, -\rad) arc (-90 : 90 : \rad); 
}
% Front longitude lines.
\foreach \theta in 
   {0, \rotationGridStepsize, ..., 180} {
 \tikzset{xyplane/.estyle={cm={
    cos(\phi), 0, 
    sin(\theta)*sin(\phi),cos(\theta),(0, 0)}}}
 \draw[xyplane,smooth] plot (\x, \fcn) ;
}
% Front latitude lines.
% Right half of a circle, rotated right
% (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
\foreach \x in {\aDomain, \nextStep, ..., \bDomain}{
    \pgfmathsetmacro\xsh{(cos(\phi))*({\x})}
    \pgfmathsetmacro\rad{(\fcn)}
    \tikzset{xyplane/.estyle={cm={
       cos(\phi-90),0,0,1, (\xsh, 0)}}}
    \draw[xyplane] (0, -\rad) arc (-90 : 90 : \rad); 
}
\end{tikzpicture}
\caption{$f(x)=\sin{x}$ rotated about the $x$-axis.}
\label{rot1x}
\end{center}
\end{figure}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize
%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode

\section{GPS satellites}
\label{gps1Code}
Code for figure \ref{gps1}, the \acro{U.S.} \acro{GPS} satellite orbits.
%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode
\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\appruled]}
% GPS satellite orbits.
\begin{tikzpicture}[scale=.77]
\def\R{1.4} % sphere radius
\def\orbitRadius{3.172*\R}
\def\angEl{1} % elevation angle
\def\x{0} % x coordinate of center
\def\y{0} % y coordinate of center
\def\z{0} % z coordinate of center
% First tilt the orbit from the north
% pole (rotate about the x-axis).
\pgfmathsetmacro\psi{35}
% Second, rotate around the y-axis.  
\pgfmathsetmacro\firstTheta{-135}
% Third, rotate about the x-axis.
\pgfmathsetmacro\phi{\angEl}
\draw[color=red, fill=blue, opacity=.15]
   (0, 0) circle (\orbitRadius);
% Set the variables, theta, c = color, angVis,
% and \thetaSatShift for each of 6 orbits.
\foreach \theta/\c in{\firstTheta/red,
   \firstTheta+60/blue,
   \firstTheta+2*60/green, 
   \firstTheta+3*60/black, 
   \firstTheta+4*60/cyan, 
   \firstTheta+5*60/brown}{
% Set the drawing plane affine transformation.
\tikzset{xyplane/.estyle={cm={
   cos(\theta),sin(\theta)*sin(\phi),
   sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-
   sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
    % Draw the back half of the orbit.
    \getFrontArcStartPosition\angle\anglex{
       \psi}{\theta}{\phi}
   \pgfmathtruncatemacro\angleInt{\angle}
    \ifthenelse{\angleInt < -180}
    {\pgfmathsetmacro\angleInt{\angleInt + 360}}
    {}
    \draw[xyplane, dashed, color=\c] 
       (\angleInt -180: \orbitRadius) 
       arc (\angleInt -180: \angle: \orbitRadius);
}
% Draw the earth.
\tikzset{current plane/.estyle={cm={1,0,0,1,(0,0)}}}
\filldraw[current plane][shift={(\x, \y)}]
   [ball color=blue,opacity=.7] (0,0,0) circle (\R);
\foreach \t in {-80,-60,...,80} {
   \DrawLatitudeCircle[\R]{\t}{\x}{\y}}
\foreach \t in {-5,-35,...,-175} {
   \DrawLongitudeCircle[\R]{\t}{\x}{\y}}
\pgfmathsetmacro\orbitBaseAngle{30}
% Draw the front half of the orbit.
\foreach \theta/\c in {\firstTheta/red, 
   \firstTheta+60/blue, 
   \firstTheta+2*60/green, 
   \firstTheta+3*60/black, 
   \firstTheta+4*60/cyan, 
   \firstTheta+5*60/brown}{
% Set the drawing plane affine transformation again.
   \tikzset{xyplane/.estyle={cm={cos(\theta),
      sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),
      cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*
      sin(\phi),(0,0)}}}
	% Draw the front half of the orbit.
	\getFrontArcStartPosition\angle\anglex{
	   \psi}{\theta}{\phi}
	\pgfmathtruncatemacro\angleInt{\angle}
	\ifthenelse{\angleInt > 180}
    		{\pgfmathsetmacro\angleInt{\angleInt - 360}}
    		{}
	\draw[xyplane,very thick,color=\c] 
	   (\angleInt:\orbitRadius) arc 
	   (\angleInt:\angleInt+180:\orbitRadius);
    	% Draw the satellites.
    	\foreach \thetaSat in {\orbitBaseAngle, 
	   \orbitBaseAngle + 30, \orbitBaseAngle + 135,
	   \orbitBaseAngle + 255} {
          \pgfmathsetmacro\xsh{
           (7/1)*\orbitRadius*cos(\thetaSat)}
       	   \pgfmathsetmacro\ysh{
	    (7/1)*\orbitRadius*sin(\thetaSat)}
          \draw[xyplane,color=\c,scale=1/7][shift=
             {(\xsh,\ysh)}](-2,-1) grid (2,1);
   	}
}
\end{tikzpicture}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize
%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode
\vspace{.1cm}
\noindent
This code requires some helper functions written by Tomasz~M. Trzeciak~\cite{TMT} for drawing spheres.  For completeness, these functions are listed below.  Place them just before \cs{begin}\verb|{document}|.

%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode
\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\appruled]}
\newcommand\pgfmathsinandcos[3]{%
  \pgfmathsetmacro#1{sin(#3)}%
  \pgfmathsetmacro#2{cos(#3)}%
}
\newcommand\LongitudePlane[3][current plane]{%
  \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
  \pgfmathsinandcos\sint\cost{#3} % azimuth
  \tikzset{#1/.estyle={cm={
     \cost,\sint*\sinEl,0,\cosEl,(0,0)}}}
}
\newcommand\LatitudePlane[3][current plane]{%
  \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
  \pgfmathsinandcos\sint\cost{#3} % latitude
  \pgfmathsetmacro\yshift{\cosEl*\sint}
  \tikzset{#1/.estyle={cm={
     \cost,0,0,\cost*\sinEl,(0,\yshift)}}} %
}
\newcommand\DrawLongitudeCircle[4][1]{
 \LongitudePlane{\angEl}{#2}
 \tikzset{current plane/.prefix style={scale=#1}}
  % angle of "visibility"
 \pgfmathsetmacro\angVis{
    atan(sin(#2)*cos(\angEl)/sin(\angEl))} %
 \draw[shift={(#3, #4)}][current plane]
    (\angVis:1) arc (\angVis:\angVis+180:1);
 \draw[shift={(#3, #4)}][current plane,dashed]
    (\angVis-180:1)arc(\angVis-180:\angVis:1);
}
\newcommand\DrawLatitudeCircle[4][1]{
 \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[shift={(#3, #4)}][current plane]
    (\angVis:1) arc (\angVis:-\angVis-180:1);
 \draw[shift={(#3, #4)}][current plane,dashed]
    (180-\angVis:1)arc(180-\angVis:\angVis:1);
}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize
\vspace{.2cm}
\noindent
This uses a macro \cs{getFrontArcStartPosition} to compute which parts of the orbit arcs are on the front side of the orbit so they can be drawn last and the back side can be drawn first with dotted lines.  There is likely an easier way to do this, but this solution involves using the spherical law of sines on various triangles on the sphere.  For completeness, an explanation of the formulas follows the code.
\smallskip

\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\appruled]}
\newcommand\getFrontArcStartPosition[5]{
% Theta must be between -180 and 180.
\pgfmathtruncatemacro\psiInt{#3}
\pgfmathtruncatemacro\thetaInt{#4}
\pgfmathtruncatemacro\phiInt{#5}
\pgfmathtruncatemacro\psiTemp{\psiInt}
\ifthenelse{\thetaInt < 0} 
% Negate theta and negate the results at the end.
    {\pgfmathtruncatemacro\thetaTemp{-\thetaInt}}
    {\pgfmathtruncatemacro\thetaTemp{\thetaInt}}
\pgfmathtruncatemacro\phiTemp{\phiInt}
\pgfmathsetmacro\anglexTemp{atan(sin(\thetaTemp)/
   (cos(\thetaTemp)*sin(\psiTemp)))}

\ifthenelse{\thetaTemp > 90}
    {\pgfmathsetmacro\anglexTemp{\anglexTemp + 180}}
    {}
\pgfmathsetmacro\result{atan(sin(\thetaTemp)*
   cos(\phiTemp)*sin(\anglexTemp)/
(sin(\anglexTemp)*cos(\psiTemp)*sin(\phiTemp)
+cos(\anglexTemp)*cos(\phiTemp)*sin(\thetaTemp)))}
\pgfmathsetmacro\specialAngle{(cos(\phiTemp)*
   cos(\phiTemp)-cos(\psiTemp)*cos(\psiTemp))/
   (cos(\phiTemp)*cos(\phiTemp)*
   sin(\psiTemp)*sin(\psiTemp))}
\ifthenelse{\phiInt < \psiInt}{
    \pgfmathparse{sqrt(\specialAngle)}
    \pgfmathsetmacro\specialAngle{
       asin(-\pgfmathresult)+180}
    \ifthenelse{\thetaTemp > \specialAngle}{
        \pgfmathsetmacro\result{\result + 180}
        \pgfmathsetmacro#1{\result}}
    {
        \pgfmathsetmacro#1{\result}}}
{}
% Negate the results if theta is negative.
\ifthenelse{\thetaInt < 0}{
    \pgfmathsetmacro#1{-\result}
    \pgfmathsetmacro#2{-\anglexTemp}}
{
    \pgfmathsetmacro#1{\result}
    \pgfmathsetmacro#2{\anglexTemp}}
}% End of \getFrontArcStartPosition function.
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize

%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode

\section{Explanation of formulas in~\texorpdfstring{\cs{getFrontArcStartPosition}}{\string\\getFrontArcStartPosition}}
\label{Explanation}
The function \cs{getFrontArcStartPosition} in appendix~C is used to find which parts of an orbit are on the front and which on the back, so that we can draw the back as a dotted line.

Figure~\ref{proof1} shows a red orbit that is tilted $\psi$ degrees from the north pole and then rotated $\theta$ degrees with the viewing angle tilted $\phi$ degrees as the orbits are for figure \ref{gps1}.  Our goal is to find the length of arc $DC$ = $x'$ since that is where the orbit comes around to the front of the sphere.

\begin{figure}[bH]
\begin{center}
\begin{tikzpicture}[scale=.66]
\def\R{1.4} % sphere radius
\def\orbitRadius{3.172*\R}
\def\angEl{30} % elevation angle
\def\x{0} % x coordinate of center
\def\y{0} % y coordinate of center
\def\z{0} % z coordinate of center
\pgfmathsetmacro\psiMaster{15}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\thetaMaster{60}
\pgfmathsetmacro\phiMaster{\angEl} % Third, rotate about the x-axis.

\ifthenelse{\boolean{color}}
{\def\c{red}}
{\def\c{black}}
% Set the variables, theta, c = color, angVis, and \thetaSatShift for each of 6 orbits.
\ifthenelse{\boolean{color}}
{\draw[color=black, fill=blue, opacity=.15] (0, 0) circle (3.172*\R);}
{\draw[color=black] (0, 0) circle (3.172*\R);}

\pgfmathsetmacro\psi{0}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{0}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
    % \draw[xyplane, color=black] (0, 0) circle (\orbitRadius);
    \draw[xyplane, dashed, color=black] (-\orbitRadius , 0) arc (-180: 0: \orbitRadius);
       \draw[xyplane, color=black] (\orbitRadius , 0) arc (0: 180: \orbitRadius);
     % Draw axis.
     \draw[xyplane, color=black] (0, -\orbitRadius) -- (0, \orbitRadius);
     
%Equator
\pgfmathsetmacro\psi{90}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{0}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
     %\draw[xyplane, color=black] (0, 0) circle (\orbitRadius);
      \draw[xyplane, dashed, color=black] (-\orbitRadius , 0) arc (-180: 0: \orbitRadius);
       \draw[xyplane, color=black] (\orbitRadius , 0) arc (0: 180: \orbitRadius);
     \draw[xyplane, color=black] (-\orbitRadius, 0) -- (\orbitRadius, 0);
    
% Without psi.
\pgfmathsetmacro\psi{1}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{\thetaMaster}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}    
\getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
   \pgfmathtruncatemacro\angleInt{\angle}
    \ifthenelse{\angleInt < -180}
    {\pgfmathsetmacro\angleInt{\angleInt + 360}}
    {}
    \draw[xyplane, dashed, color= \c] (\angleInt -180: \orbitRadius) arc (\angleInt -180: \angle: \orbitRadius);  
    
% With psi.
\pgfmathsetmacro\psi{\psiMaster}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{\thetaMaster}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
 \getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
   \pgfmathtruncatemacro\angleInt{\angle}
%    \ifthenelse{\angleInt < -180}
%    {\pgfmathsetmacro\angleInt{\angleInt + 360}}
%    {}
    \draw[xyplane, dashed, color= \c] (\angleInt -180: \orbitRadius) arc (\angleInt -180: \angle: \orbitRadius);    
    
% Draw the earth.
\tikzset{current plane/.estyle={cm={1,  0,   0,  1,  (0, 0)}}}
\ifthenelse{\boolean{color}}
{\filldraw[current plane][shift={(\x, \y)}][ball color=blue, opacity=.7] (0,0,0) circle (\R);}
{\filldraw[current plane][shift={(\x, \y)}][ball color=lightgray, opacity=1] (0,0,0) circle (\R);}
\foreach \t in {-60,-30,...,80} { \DrawLatitudeCircle[\R]{\t}{\x}{\y}}
\foreach \t in {-5,-45,...,-175} { \DrawLongitudeCircle[\R]{\t}{\x}{\y}}

% Without psi.
\pgfmathsetmacro\psi{1}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{\thetaMaster}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
     % Draw the front half of the orbit.
	\getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
	\pgfmathtruncatemacro\angleInt{\angle}
	\draw[xyplane, color= \c] (\angleInt :  \orbitRadius) arc (\angleInt: \angleInt +180 : \orbitRadius);
     % Draw axis.
     \draw[xyplane, color=black] (-\orbitRadius, 0) -- (\orbitRadius, 0);
% With psi.
\pgfmathsetmacro\psi{\psiMaster}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{\thetaMaster}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
\tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
    % Draw the front half of the orbit.
	\getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
	\pgfmathtruncatemacro\angleInt{\angle}
	\ifthenelse{\angleInt > 180}
    		{\pgfmathsetmacro\angleInt{\angleInt - 360}}
    		{}
	\draw[xyplane, color= \c] (\angleInt :  \orbitRadius) arc (\angleInt: \angleInt +180 : \orbitRadius);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\opac{1}
\ifthenelse{\boolean{color}}
{\def\color{blue}}
{\def\color{black}}
\pgfmathsetmacro\psi{-90}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{0}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
\draw[xyplane, dashed, ultra thick, color= \color, opacity= \opac] (\orbitRadius, 0) arc (0 : \thetaMaster : \orbitRadius);
     
     
% With psi.
\pgfmathsetmacro\psi{\psiMaster}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{\thetaMaster}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
\tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
\draw[xyplane, dashed, ultra thick, color= \color, opacity= \opac] (\orbitRadius, 0) arc (0: \angle : \orbitRadius);
\draw[xyplane, ultra thick, color= \color, opacity= \opac] (\angle : \orbitRadius) arc (\angle: \anglex : \orbitRadius);

\draw[ultra thick, color= \color, opacity= \opac] (\orbitRadius, 0) arc (0 : 61 : \orbitRadius);

\pgfmathsetmacro\psi{0}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{0}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
\draw[xyplane, ultra thick, color= \color, opacity= \opac] (\orbitRadius, 0) arc (0 : 73 : \orbitRadius);

% Now draw the labels.
\draw  (\orbitRadius + .3, 0) node {$A$};
\draw  (1.3, 3.35) node {$B$};
\draw  (2.4, 4.1) node {$C$};
\draw  (2.5, 2.1) node {$D$};

\end{tikzpicture}

\caption{Given the orbit containing points $B$, $C$, and $D$, find the angular distance from $D$ to $C$.  If this distance is found, then the orbit is drawn with a solid arc from that point at $C$ for 180 degrees and then for another 180 degrees as a dashed line.}
\label{proof1}
\end{center}
\end{figure}


\begin{figure}[tH]
\begin{center}
\begin{tikzpicture}[scale=2.0]

\draw (0, 0) -- (3, 0) -- (3, 2) -- cycle;
\draw (3, 0) -- (1, 2/3);
% Label the vertices.
\draw  (3.13, 0) node {$A$};
\draw  (-.15, 0) node {$B$};
\draw  (.85, .75) node {$C$};
\draw  (3.13, 2.08) node {$D$};
% Label the angles.
\draw  (2.3, .12) node {$\phi$};
\draw  (2.72, .25) node {$90 - \phi$};
\draw  (1, .5) node {$\beta$};
\draw  (1.43, .69) node {$180 - \beta$};
\draw  (2.72, 1.5) node {$90 - \psi$};
% Label the sides.
\draw  (3.13, 1) node {$\theta$};
\draw  (1.58, -.15) node {$y$};
\draw  (1.85, 1.4) node {$x'$};

\draw  (1.3, 1.3) node {$x$};
\pgfmathsetmacro\xsh{-.2}
\pgfmathsetmacro\ysh{(-3/2)*\xsh}
\draw[<-, shift={(\xsh, \ysh)}] (0, 0) -- (1.3, 1.3*2/3);
\draw[<-, shift={(\xsh+3, \ysh+2)}] (0, 0) -- (-1.3, -1.3*2/3);
\draw[shift={(-.04, .04*3/2)}] (0, 0) -- (-.25, .25*3/2);
\draw[shift={(-.04+3, .04*3/2+2)}] (0, 0) -- (-.25, .25*3/2);
\draw (.35, .43) node {$x - x'$};


\end{tikzpicture}
\caption{Triangles of figure \ref{proof1} drawn in the plane.}
\label{symmetry}
\end{center}
\end{figure}



The spherical law of sines says that for a triangle on a sphere $$\frac{\sin{A}}{\sin{a}} = \frac{\sin{B}}{\sin{b}} = \frac{\sin{C}}{\sin{c}}$$
where $a$, $b$, and $c$ are the three angles of the triangle and $A$, $B$, and $C$ are the three corresponding opposite side lengths (which are measured as an angle from the center of the sphere).


Using the law of sines on the large triangle $ABD$, we have that 
\begin{equation}\label{firstLOS}
\frac{\sin{y}}{\sin{(90 - \psi)}} = \frac{\sin{x}}{\sin{90}}.
\end{equation}

Using the law of sines on the lower small triangle $ABC$, we have that 
\begin{equation}\label{secondLOS}
\frac{\sin{\beta}}{\sin{y}} = \frac{\sin{\phi}}{\sin{(x-x')}}.  
\end{equation}

Simplifying and solving (1) for $\sin{y}$ and substituting into (2) and solving for $\sin{\beta}$, results in
\begin{equation}\label{firstSecond}
\sin{\beta} = \frac{\cos{\psi}\sin{x}\sin{\phi}}{\sin{(x-x')}}.
\end{equation}
Using the law of sines on the upper small triangle $ACD$, we have that 
\begin{equation}\label{thirdLOS}
\frac{\sin{(180 - \beta)}}{\sin{\theta}} = \frac{\sin{(90 - \phi)}}{\sin{x'}}. 
\end{equation}
Solving (4) for $\sin{\beta}$ and setting equal to the right side of (3) results in
$$\frac{\sin{\theta}\cos{\phi}}{\sin{x'}} = \frac{\cos{\psi}\sin{x}\sin{\phi}}{\sin{(x-x')}}$$
or
$$\frac{\sin{\theta}\cos{\phi}}{\sin{x'}} = \frac{\cos{\psi}\sin{x}\sin{\phi}}{\sin{x}\cos{x'} - \cos{x}\sin{x'}}.$$
Cross multiplying, dividing by $\cos{x'}$ and solving for $\tan{x'}$ gives
$$\tan{x'} = \frac{\sin{\theta}\cos{\phi}\sin{x}}{\sin{\theta}\cos{\phi}\cos{x} + \cos{\psi}\sin{\phi}\sin{x}}.$$
Thus we can compute $x'$ in terms of $x$.  In order to compute $x$, see figure~\ref{proofx} where we have added points $N$ and $E$.

\begin{figure}[t]
\begin{center}
\begin{tikzpicture}[scale=.66]
\def\R{1.4} % sphere radius
\def\orbitRadius{3.172*\R}
\def\angEl{30} % elevation angle
\def\x{0} % x coordinate of center
\def\y{0} % y coordinate of center
\def\z{0} % z coordinate of center
\pgfmathsetmacro\psiMaster{15}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\thetaMaster{60}
\pgfmathsetmacro\phiMaster{\angEl} % Third, rotate about the x-axis.
\ifthenelse{\boolean{color}}
{\def\c{red}}
{\def\c{black}}

\ifthenelse{\boolean{color}}
{\draw[color=black, fill=blue, opacity=.15] (0, 0) circle (3.172*\R);}
{\draw[color=black] (0, 0) circle (3.172*\R);}

\pgfmathsetmacro\psi{0}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{0}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
     %\draw[xyplane, color=black] (0, 0) circle (\orbitRadius);
     \draw[xyplane, dashed, color=black] (-\orbitRadius , 0) arc (-180: 0: \orbitRadius);
       \draw[xyplane, color=black] (\orbitRadius , 0) arc (0: 180: \orbitRadius);
     % Draw axis.
     \draw[xyplane, color=black] (0, -\orbitRadius) -- (0, \orbitRadius);
     
%Equator
\pgfmathsetmacro\psi{90}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{0}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
     \draw[xyplane, dashed, color=black] (-\orbitRadius , 0) arc (-180: 0: \orbitRadius);
       \draw[xyplane, color=black] (\orbitRadius , 0) arc (0: 180: \orbitRadius);
     \draw[xyplane, color=black] (-\orbitRadius, 0) -- (\orbitRadius, 0);
    
% Without psi.
\pgfmathsetmacro\psi{1}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{\thetaMaster}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}    
\getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
   \pgfmathtruncatemacro\angleInt{\angle}
    \ifthenelse{\angleInt < -180}
    {\pgfmathsetmacro\angleInt{\angleInt + 360}}
    {}
    \draw[xyplane, dashed, color= \c] (\angleInt -180: \orbitRadius) arc (\angleInt -180: \angle: \orbitRadius);
    
    
% With psi.
\pgfmathsetmacro\psi{\psiMaster}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{\thetaMaster}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
 \getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
   \pgfmathtruncatemacro\angleInt{\angle}
%    \ifthenelse{\angleInt < -180}
%    {\pgfmathsetmacro\angleInt{\angleInt + 360}}
%    {}
    \draw[xyplane, dashed, color= \c] (\angleInt -180: \orbitRadius) arc (\angleInt -180: \angle: \orbitRadius);    

    
% Draw the earth.
\tikzset{current plane/.estyle={cm={1,  0,   0,  1,  (0, 0)}}}
\ifthenelse{\boolean{color}}
{\filldraw[current plane][shift={(\x, \y)}][ball color=blue, opacity=.7] (0,0,0) circle (\R);}
{\filldraw[current plane][shift={(\x, \y)}][ball color=lightgray,] (0,0,0) circle (\R);}
\foreach \t in {-60,-20,...,80} { \DrawLatitudeCircle[\R]{\t}{\x}{\y}}
\foreach \t in {-5,-45,...,-175} { \DrawLongitudeCircle[\R]{\t}{\x}{\y}}

    

% Without psi.
\pgfmathsetmacro\psi{1}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{\thetaMaster}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
\tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
     % Draw the front half of the orbit.
	\getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
	\pgfmathtruncatemacro\angleInt{\angle}
	\draw[xyplane, color= \c] (\angleInt :  \orbitRadius) arc (\angleInt: \angleInt +180 : \orbitRadius);
     % Draw axis.
     \draw[xyplane, color=black] (-\orbitRadius, 0) -- (\orbitRadius, 0);
% With psi.
\pgfmathsetmacro\psi{\psiMaster}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{\thetaMaster}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
\tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
    % Draw the front half of the orbit.
	\getFrontArcStartPosition\angle\anglex{\psi}{\theta}{\phi}
	\pgfmathtruncatemacro\angleInt{\angle}
	\ifthenelse{\angleInt > 180}
    		{\pgfmathsetmacro\angleInt{\angleInt - 360}}
    		{}
	\draw[xyplane, color= \c] (\angleInt :  \orbitRadius) arc (\angleInt: \angleInt +180 : \orbitRadius);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\opac{1}
\ifthenelse{\boolean{color}}
{\def\color{blue}}
{\def\color{black}}
\pgfmathsetmacro\psi{-90}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{0}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
\draw[xyplane, dashed, ultra thick, color= \color, opacity= \opac] (\orbitRadius, 0) arc (0 : \thetaMaster : \orbitRadius);
     
     
% With psi.
\pgfmathsetmacro\psi{\psiMaster}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{\thetaMaster}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
\tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
\draw[xyplane, dashed, ultra thick, color= \color, opacity= \opac] (\orbitRadius, 0) arc (0: \angle : \orbitRadius);
\draw[xyplane, ultra thick, color= \color, opacity= \opac] (\angle : \orbitRadius) arc (\angle: \anglex : \orbitRadius);

\draw[ultra thick, color= \color, opacity= \opac] (\orbitRadius, 0) arc (0 : 61 : \orbitRadius);

\pgfmathsetmacro\psi{0}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{0}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
\draw[xyplane, ultra thick, color= \color, opacity= \opac] (\orbitRadius, 0) arc (0 : 73 : \orbitRadius);

% Now draw the labels.
\draw  (\orbitRadius + .3, 0) node {$A$};
\draw  (1.3, 3.4) node {$B$};
\draw  (2.4, 4.1) node {$C$};
\draw  (2.5, 2.1) node {$D$};
\draw  (0, 4.1) node {$N$};
\draw  (.54, 3.33) node {$E$};


\pgfmathsetmacro\psi{0}  % First tilt the orbit from the north pole (rotate about the x-axis).
\pgfmathsetmacro\theta{-30}
\pgfmathsetmacro\phi{\phiMaster} % Third, rotate about the x-axis.
 % Set the drawing plane affine transformation.
     \tikzset{xyplane/.estyle={cm={cos(\theta),sin(\theta)*sin(\phi),sin(\theta)*sin(\psi),cos(\psi)*cos(\phi)-sin(\psi)*cos(\theta)*sin(\phi),(0, 0)}}}
\draw[xyplane, ultra thick, color= \color, opacity= \opac] (0, \orbitRadius) arc (90 : 75 : \orbitRadius);

\end{tikzpicture}
\caption{This is the same as figure \ref{proof1} with added points $N$ and $E$ where segment $NE$ is perpendicular to both red orbits.  Triangle $NED$ is used to find the value of $x$ which is the arc $BD$.}
\label{proofx}
\end{center}
\end{figure}

Point $E$ is chosen such that angle $NEB$ is a right angle.  Triangle $NED$ is drawn in figure~\ref{proofxFlat}.

\begin{figure}[H]
\begin{center}
\begin{tikzpicture}[scale=2.1]

\draw (0, 0) -- (3, 0) -- (0, 1) -- cycle;
\draw (0, 1) -- (1, 0);
% Label the vertices.
\draw  (3.13, 0) node {$D$};
\draw  (-.15, 0) node {$E$};
\draw  (-.15, 1) node {$N$};
\draw  (1, -.1) node {$B$};
% Label the angles.
\draw  (2.3, .12) node {$\psi$};
\draw  (1.05, .1) node {$\alpha$};
\draw  (.58, .1) node {$180 - \alpha$};
\draw  (.3, .82) node {$\theta$};
\draw  (.27, .42) node {$90-\theta$};
\draw  (1.5, -.25) node {$90$};
\draw (-.1, .5) node {$\psi$};
\draw (1.5, .64) node {$90$};
\draw  (1.9, -.1) node {$x$};
\draw  (.5, -.1) node {$90-x$};

\draw (0, -.1) -- (0, -.35);
\draw (3, -.1) -- (3, -.35);

\draw[<-] (0, -.25) -- (1.3, -.25);
\draw[->] (1.7, -.25) -- (3, -.25);

\end{tikzpicture}
\caption{Triangle $NED$ of figure \ref{proofx} drawn in the plane.}
\label{proofxFlat}
\end{center}
\end{figure}

The law of sines applied to triangle $NBD$ on the right gives
\begin{equation}\label{figrightTriLOS}
\sin{\alpha} = \frac{\sin{\theta}}{\sin{x}}.
\end{equation}
The law of sines applied to triangle $NEB$ on the left gives
$\sin{(180-\alpha)} =$
\begin{equation}\label{figleftTriLOS}
\sin{\alpha} = \frac{\sin{\psi}\sin{(90 - \theta)}}{\sin{(90 - x)}} =  \frac{\sin{\psi}\cos{\theta}}{\cos{x}}.
\end{equation}
Setting the values of $\sin{\alpha}$ equal from (5) and (6) and solving for $\tan{x}$ gives
$$\tan{x} = \frac{\tan{\theta}}{\sin{\psi}}.$$

Thus we now have $x$ in terms of $\theta$ and $\psi$.  This explains the formulas used for $0 \le \theta \le 90$ in the macro \cs{getFrontArcStartPosition}.  In the case that $\psi \le \phi$, the same formulas work for when $90 \le \theta \le 180$.  There are some complications when $\psi > \phi$ and $90 \le \theta \le 180$.  In this case, when $x'$ exceeds 90 degrees there is a sign change in the computation.  Some more spherical trigonometry reveals that this happens when $$\sin^2{\theta} = \frac{\cos^2{\phi} - \cos^2{\psi}}{\cos^2{\phi}\sin^2{\psi}}$$ which we use in \cs{getFrontArcStartPosition} to compute the value of $\theta$ where this change occurs.  This allows us to compute $x'$ for all $0 \le \theta \le 180$.  For $-180 \le \theta \le 0$, we use symmetry and return the negative of the $x'$ computed for $|\theta|$.



\section{Intersection of two spheres}
\label{sphereInt}
Code for figure \ref{intersectingSpheres}.

%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode
\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\appruled]}
% The intersection of two spheres.
\begin{tikzpicture}[scale=.8]
% Draw the first sphere.
\def\Rb{3.1} % sphere radius
\def\angEl{30} % elevation angle
\def\xb{3} % x coordinate of center
\def\yb{3} % y coordinate of center
\def\zb{-1} % z coordinate of center
\filldraw[shift={(\xb, \yb)}][ball color= blue] 
   (0, 0, 0) circle (\Rb);
\foreach \t in {-60,-20,...,80} {
   \DrawLatitudeCircle[\Rb]{\t}{\xb}{\yb}}
\foreach \t in {-5,-45,...,-175} {
   \DrawLongitudeCircle[\Rb]{\t}{\xb}{\yb}}
% Draw the second sphere.
\def\Rc{2.4} % sphere radius
\def\angEl{30} % elevation angle
\def\xc{0} % x coordinate of center
\def\yc{0} % y coordinate of center
\def\zc{0} % z coordinate of center
\filldraw[shift={(\xc, \yc)}][ball color= red]
   (0,0,0) circle (\Rc);
\foreach \t in {-60,-20,...,80} {
   \DrawLatitudeCircle[\Rc]{\t}{\xc}{\yc}}
\foreach \t in {-5,-45,...,-175} {
   \DrawLongitudeCircle[\Rc]{\t}{\xc}{\yc}}
\drawIntersectionOfSpheres{\xc}{\yc}{\zc}{\xb}
   {\yb}{\zb}{\Rc}{\Rb}{yellow}
\end{tikzpicture}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize
\vspace{.2cm}
This code also requires the helper functions for drawing spheres, given in appendix \ref{gps1Code}.  Place them just before \cs{begin}\verb|{document}|.

%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode
\vspace{.2cm}
\noindent
The following are additional helper functions to the main
function
that draws the intersection of the two spheres,
\cs{drawIntersectionOfSpheres}.
%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode

\footnotesize
\ifthenelse{\boolean{color}}
{\begin{outshaded}
\begin{verbatim}[]}
{\begin{verbatim}[\appruled \hfuzz=.3pt]}
\newcommand\calculateCenterSpan[4]{
  \pgfmathsetmacro#1{((#2)^2+(#3)^2+(#4)^2)^(1/2)}}
\newcommand\calculateAngtheta[3]{
  \pgfmathsetmacro#1{acos(#2/#3)}}
\newcommand\calculateShiftDistance[4]{
 \pgfmathsetmacro#1{((((#2)^2-(#3)^2)/(2*#4)+#4/2))}}
\newcommand\calculateShiftDistancePercent[3]{
  \pgfmathsetmacro#1{#2/#3}}
\newcommand\calculateCircleRadius[3]{
  \pgfmathsetmacro#1{((#2)^2 - (#3)^2)^(1/2)}
}
% The function below does not use the 
% rotation matrices about coordinate axes,
% but instead computes the vector that we 
% want to rotate around, then the 
% corresponding rotation matrix, and then 
% (as before) projects to the xy-plane. 
% It also shifts to the correct location.
\newcommand\drawIntersectionOfSpheres[9]{ 
% Parameters are:  CenterSphere1x, CenterSphere1y,
% CenterSphere1z, CenterSphere2x,
% CenterSphere2y, CenterSphere2z, 
% RadiusSphere1, RadiusSphere2, DrawColor.
\pgfmathsetmacro\xchange{#4 - #1}
\pgfmathsetmacro\ychange{#5 - #2}
\pgfmathsetmacro\zchange{#6 - #3}
\pgfmathsetmacro\firstSphereCenterx{#1}
\pgfmathsetmacro\firstSphereCentery{#2}
\calculateCenterSpan\centerSpan{
   \xchange}{\ychange}{\zchange}
\calculateAngtheta\angtheta{\zchange}{\centerSpan}
\calculateShiftDistance\shiftDistance{
   #7}{#8}{\centerSpan}
\calculateShiftDistancePercent\shiftDistancePercent{
   \shiftDistance}{\centerSpan}
\calculateCircleRadius\circleRadius{
   #7}{\shiftDistance}
\pgfmathsetmacro\ux{\ychange}
\pgfmathsetmacro\uy{-\xchange}
\pgfmathsetmacro\C{1-cos(\angtheta)}
\pgfmathsetmacro\L{((\ux)^2 + (\uy)^2)^(1/2)}
\pgfmathsetmacro\first{
  cos(\angtheta) + (\ux)^2*\C/(\L^2)}
\pgfmathsetmacro\second{\ux*\uy*\C/(\L^2)}
\pgfmathsetmacro\third{\ux*\uy*\C/(\L^2)}
\pgfmathsetmacro\fourth{
  cos(\angtheta)+(\uy)^2*\C/(\L^2)}
\tikzset{xyplane/.estyle={cm={\first,\second,\third,
  \fourth,(\firstSphereCenterx+\shiftDistancePercent
  *\xchange,\firstSphereCentery
            +\shiftDistancePercent*\ychange)}}}
\pgfmathsetmacro\dAng{atan(\xchange/\ychange)}
\draw[xyplane, color=#9, ultra thick]
  (-\dAng: \circleRadius) arc
  (-\dAng:-\dAng-180:\circleRadius);
\draw[xyplane, color=#9, ultra thick, dashed]
  (-\dAng+180: \circleRadius) arc
  (-\dAng+180:-\dAng:\circleRadius);
}
\end{verbatim}
\ifthenelse{\boolean{color}}
{\end{outshaded}}
{}
\normalsize

%%%%%%CodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCodeCode
\end{appendix}


\small
\SetBibJustification{\raggedright \advance\itemsep by 0pt plus1pt minus1pt }
\begin{thebibliography}{8}


\bibitem{TT} Till Tantau. \acro{PGF}/\TikZ{} manual, version 2.10.
\url{http://mirror.ctan.org/graphics/pgf/base/doc/generic/pgf/pgfmanual.pdf}, 2010.


\bibitem{AMWS}
Andrew Mertz and William Slough.  Graphics with \PGF\ and \TikZ.  \TUB\ 28:1, Proceedings of the
Practical \TeX\ 2006 Conference, \url{http://tug.org/TUGboat/tb28-1/tb88mertz.pdf}, 2007.


\bibitem{TMT} Tomasz M. Trzeciak.  \url{http://www.texample.net/tikz/examples/map-projections/}, 2008.


\bibitem{RotationMatrices} Wikipedia, \url{http://en.wikipedia.org/wiki/Rotation_matrix}.


\end{thebibliography}

\advance\signaturewidth by 4pc
\makesignature
\end{document}
