Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to draw my Moon on its orbit around the Earth. I can easily draw the moon on the perpendicular axes centring on the Earth but to move it around would probably require some trigonometry... Is there an easy way to tweak my Moon's position, keeping it in orbit? Maybe with shift?

\documentclass{article}

\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{3d,calc}

\begin{document}

\begin{tikzpicture}
    % Earth
    \shade[ball color=blue!10!white,opacity=0.20] (0,0) circle (1.5);
    \node[align=center](earth) at (0,0) {Earth};

    % Orbitz
    \draw[circle] (0,0) circle (3);

    %Moon
    \shade[ball color=blue!10!white,opacity=0.20] (0,3) circle (0.5);
    \node[align=center](moon) at (0,3) {Moon};


\end{tikzpicture}



\end{document}

enter image description here

share|improve this question
1  
You could try using shift in polar coordinates: [shift=(angle:length)] instead of [shift=(Xlength:Ylength)] in cartesian coordinates (I'm not a specialist and I have not tried). –  anderstood Aug 26 at 2:28
    
@anderstood That's the answer –  Francesco Aug 26 at 2:33
1  
Assuming the orbit is not elliptic ;) –  anderstood Aug 26 at 2:34
    
Have you seen tex.stackexchange.com/questions/116978/…? –  Jubobs Aug 26 at 8:03

4 Answers 4

up vote 20 down vote accepted

Run with --shell-escape and needs imagemagick installed.

\documentclass[preview,border={10pt 0pt 10pt 10pt}]{standalone}

\usepackage{filecontents}
\begin{filecontents*}{moon.tex}
\documentclass[tikz,preview,border=15mm]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{3d,calc}

\begin{document}
 \foreach \x in {5,10,...,355}{
\begin{tikzpicture}
    % Earth
    \path[use as bounding box] (-3,-3) rectangle (3,3);
    \shade[ball color=blue!10!white,opacity=0.20] (0,0) circle (1.5);
    \node[align=center](earth) at (0,0) {Earth};

    % Orbitz
    \draw[circle] (0,0) circle (3);

    %Moon
    \shade[ball color=blue!10!white,opacity=0.20] (\x:3) circle (0.5);
    \node[align=center](moon) at (\x:3) {Moon};
\end{tikzpicture}
}
\end{document}

\end{filecontents*}
%
\immediate\write18{pdflatex moon}

% convert to GIF animation
%\immediate\write18{convert -delay 10 -loop 0 -density 400 -alpha remove moon.pdf moon.gif}
%
% convert to PNG
\makeatletter
\immediate\write18{convert -density 200 -alpha on moon.pdf moon-\@percentchar02d.png}
\makeatother

\usepackage{animate}
\begin{document}
\begin{preview}
%\animategraphics[controls,autoplay,loop,scale=<integer>]{<frame rate>}{<PDF filename without extension>}{<left blank>}{<left blank>}
\animategraphics[controls,autoplay,loop,scale=1]{10}{moon}{}{}
\end{preview}
\end{document}

enter image description here

Elliptical orbits:

\documentclass[preview,border={10pt 0pt 10pt 10pt}]{standalone}

\usepackage{filecontents}
\begin{filecontents*}{moon.tex}
\documentclass[tikz,preview,border=-2mm]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{3d,calc}

\begin{document}
 \foreach \x in {5,10,...,355}{
\begin{tikzpicture}[rotate=-52]
    % Earth
    \clip[use as bounding box,yslant=0.5,xslant=0.5,circle] (0,0) circle (5);
    %\draw[use as bounding box] (-4,-4) rectangle (4,4);
    \shade[ball color=blue!10!white,opacity=0.20] (0,0) circle (1.5);
    \node[align=center](earth) at (0,0) {Earth};

    % Orbitz
    \begin{scope}[yslant=0.5,xslant=0.5]
    \draw (0,0) circle (4);

    %Moon
    \node (\x) at (\x:4) {};
    \node[align=center](moon) at (\x:4) {Moon};
    \end{scope}
    \shade[ball color=blue!10!white,opacity=0.20] (\x) circle (0.5);
\end{tikzpicture}
}
\end{document}


\end{filecontents*}
%
\immediate\write18{pdflatex moon}

% convert to GIF animation
%\immediate\write18{convert -delay 10 -loop 0 -density 400 -alpha remove moon.pdf moon.gif}
%
% convert to PNG
\makeatletter
\immediate\write18{convert -density 200 -alpha on moon.pdf moon-\@percentchar02d.png}
\makeatother

\usepackage{animate}
\begin{document}
\begin{preview}
%\animategraphics[controls,autoplay,loop,scale=<integer>]{<frame rate>}{<PDF filename without extension>}{<left blank>}{<left blank>}
\animategraphics[controls,autoplay,loop,scale=1]{10}{moon}{}{}
\end{preview}
\end{document}

enter image description here

share|improve this answer
1  
It always baffles me when users create animations with TeX (+1) –  moose Aug 27 at 18:30

The following approach uses the polar coordinates to place the moon in the orbit. The foreach-loop contains the code to place the moon on an offset (orbit) at a given angle. This way you can easily iterate over your desired angles and render your image.

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

\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
 \newcommand{\orbit}{3}

  % Earth
 \coordinate (center) at (0,0);
 \shade [ball color=blue!10!white, opacity=.2] (center) circle (1.5) node [opacity=1] {Earth};

 % Orbit
 \draw [dashed, black!50] (center) circle (\orbit);

 % Moon
 \foreach \angle in {0,45,...,325} {
  \shade [ball color=blue!10!white, opacity=.2] (center) ++ (\angle:\orbit) circle (.5) node [opacity=1] {Moon};
 };
\end{tikzpicture}
\end{document}

Rendered image

If you want to offset your labels (e.g moon) from the nodes (because you want to draw on them) you can use this approach (in the \foreach-loop):

  \shade [ball color=blue!10!white, opacity=.2] (center) ++ (\angle:\orbit) circle (.5) ++(\angle:1) node  [opacity=1] {Moon};

This code sets every moon label with an offset of the given angle and 1cm:

Rendered image of offset labels

share|improve this answer

It is intentionally made as simple as possible with PSTricks.

\documentclass[pstricks,border=12pt,dvipsnames]{standalone}
\usepackage{pst-map3d}  
\psset
{
    RotX=-45,
    RotZ=-45,
    PHI=45,
    THETA=30,
    Decran=10,
    path=C:/texlive/2014/texmf-dist/tex/generic/pst-geo/data,
}

\begin{document}
\foreach \i in {0,30,...,330}{
\begin{pspicture}(-5,-5)(5,5)
    \WorldMapThreeD[circles=false,blueEarth=true]
    \pscircle{4}
    \pscircle*[linecolor=lightgray](4;\i){1}
\end{pspicture}}
\end{document}

enter image description here

share|improve this answer

Something like this, maybe?

many moons

\documentclass[tikz, border=10pt]{standalone}

\begin{document}

  \begin{tikzpicture}
    [
      heavenly body/.style={shading=ball, ball color=blue!10!white, opacity=0.2},
      moon/.pic={%
          \path [heavenly body] circle (.5);
          \node (moon #1) {Moon #1};
      },
      align=center,
    ]
    % Earth
    \shade[heavenly body] (0,0) circle (1.5);
    \node (earth) at (0,0) {Earth};
    % Orbitz
    \draw[circle] (0,0) circle (3);
    %Moon
    \path (3,0) foreach \i in {0,...,11}
      {
        pic {moon=\i}
        arc (\i*30:\i*30+30:3)
      };
  \end{tikzpicture}

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.