Example: The Earth’s orbit around the Sun

Published 2013-03-09 | Author: Julien Cretel

The Earth’s orbit is an elliptical motion of the Earth around the Sun. This example calculates the distance between Earth and Sun, and the direction of the light hitting the Earth, and draws one complete step-by-step movement around the sun in a series of frames. This could be made to an animation, for example to an animated GIF or PNG or a PDF animation.

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% The Earth's orbit around the Sun
% Author: Julien Cretel, 25/02/2013
\documentclass{beamer}
\usepackage{tikz}

\begin{document}
\begin{frame}[fragile]
\frametitle{}
\begin{center}
\begin{tikzpicture}[scale=3.5]
\setbeamercovered{invisible}
\pgfmathsetmacro{\e}{0.25}          % Excentricity of the elliptical orbit

% Draw the Sun at the right-hand-side focus
top color=yellow!70,
bottom color=red!70,
\visible<1>{
}

% Draw the elliptical path of the Earth.
\draw[thin] (0,0) ellipse (1 and {\b});

% This function computes the direction in which light hits the Earth.
\pgfmathdeclarefunction{f}{1}{%
\pgfmathparse{
((-\e+cos(#1))<0) * ( 180 + atan( \b*sin(#1)/(-\e+cos(#1)) ) )
+
((-\e+cos(#1))>=0) * ( atan( \b*sin(#1)/(-\e+cos(#1)) ) )
}
}

% This function computes the distance between Earth and the Sun,
% which is used to calculate the varying radiation intensity on Earth.
\pgfmathdeclarefunction{d}{1}{%
\pgfmathparse{ sqrt((-\e+cos(#1))*(-\e+cos(#1))+\b*sin(#1)*\b*sin(#1)) }
}

% Produces a series of frames showing one revolution
% (the total number of frames is controlled by macro \N)
\pgfmathtruncatemacro{\N}{12}
\foreach \k in {0,1,...,\N}{
\pgfmathsetmacro{\theta}{360*\k/\N}
\pgfmathparse{int(\k+1)}
\onslide<\pgfmathresult>{
% \onslide is used instead of \visible<.-(x)> and \pause,
% in order not to break header and footer.
top color=Earthlight,
bottom color=blue,
\visible<1>{
}
}
}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document} 