# Saturn rings with TikZ

The planet and a ring are easily drawn. The challenge is to layer them in such a way that the far side of the ring is hidden from view behind the planet, and the near side of the ring eclipses the planet in the foreground. My instinct tells me that this can be done using clipping, scopes, or the backgrounds package, but I apparently lack the cleverness to turn the trick.

Here's the code for Saturn and one ring:

\documentclass{article}

\usepackage{tikz}

\begin{document}

%Saturn on top
\begin{tikzpicture}
\draw[fill=blue] (0,-.2) circle [x radius=4, y radius=1];
\draw[fill=white] (0,0) circle [x radius=3, y radius=.5];
\draw[fill=red] (0,0) circle [radius=2];
\end{tikzpicture}

\bigskip

%ring on top
\begin{tikzpicture}
\draw[fill=red] (0,0) circle [radius=2];
\draw[fill=blue] (0,-.2) circle [x radius=4, y radius=1];
\draw[fill=white] (0,0) circle [x radius=3, y radius=.5];
\end{tikzpicture}

\end{document}

-

Example with some clipping:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
% red Saturn as background
\draw[fill=red] (0,0) circle [radius=2];
% blue ring with white in the middle
\draw[fill=blue, even odd rule]
% redraw red Saturn in the foreground with clipping
\begin{scope}
\clip[overlay] (-2.1,0) rectangle (2.1,2.1);
\draw[fill=red] (0,0) circle [radius=2];
\end{scope}
\end{tikzpicture}

\end{document}


Remarks:

• Instead of filling the interior of the ring with white, the code uses option even odd rule for filling. The "white" interior is specified twice for filling. According to that rule, it is not filled with the fill color.

-
Bravo! That's exactly what I thought could be done, but couldn't do it. Thanks, Heiko. –  steven_nevets Jan 25 '14 at 20:41
That looks strangely familiar. –  episanty Jan 26 '14 at 2:45

Another example:

\documentclass[x11names]{beamer}
\usepackage{lmodern,tikz}
\usetikzlibrary{decorations.markings,overlay-beamer-styles}

color(0cm)=(AntiqueWhite4);
color(0.1cm)=(LightGoldenrod2);
color(0.15cm)=(Bisque1);
color(0.25cm)=(LemonChiffon3);
color(0.3cm)=(NavajoWhite3);
color(0.6cm)=(LemonChiffon3);
color(0.7cm)=(Wheat2);
color(0.85cm)=(Wheat1);
color(0.95cm)=(Bisque3);
color(1.25cm)=(Cornsilk2);
color(1.5cm)=(Bisque3);
color(1.7cm)=(AntiqueWhite3);
color(1.9cm)=(AntiqueWhite4)
}

\tikzset{satellite orbit/.style args={at pos #1 with #2}{
postaction=decorate,
decoration={
markings,
mark=
at position #1
with
{
#2
}
}
},
}

\begin{document}
\begin{frame}
\begin{center}
\begin{tikzpicture}[rotate=21]

\path[inner color=LemonChiffon3,outer color=Bisque3,even odd rule]

\path[inner color=Bisque2,outer color=NavajoWhite3,even odd rule]
;

\path[left color=AntiqueWhite3,right color=Wheat3!98!black,even odd rule,

\path[inner color=NavajoWhite2,outer color=Bisque3,even odd rule]

\foreach \x[count=\xi] in {0.35,0.4,...,1,0,0.05,...,0.25}{
\tikzset{visibility/.style={
visible on=<\xi>,
}
}
\ifnum\xi<3
\tikzset{visibility/.append style={
background shade={right color=gray,left color=gray!80},
}
}
\fi
\ifnum\xi>16
\tikzset{visibility/.append style={
background shade={left color=gray,right color=gray!80},
}
}
\fi
\path[satellite orbit=%
at pos {\x} with {%
\node[circle,visibility]{};%
}

\begin{scope}
\clip[overlay] (-2.1,0) rectangle (2.1,2.1);
\end{scope}

\end{tikzpicture}
\end{center}
\end{frame}
\end{document}


The result:

-
Really cool drawing! –  Svend Tveskæg Jan 29 '14 at 16:17
Amazing code, Claudio! In other news, Houston, we have a problem... :) –  Paulo Cereda Jan 29 '14 at 17:03
@PauloCereda: the birth of alien ducks :) What else will you invent? ;) –  Claudio Fiandrino Jan 29 '14 at 17:50
@Claudio: my original plan was to replace the moon with the duck, but apparently I injected the duck code in the wrong place. :) –  Paulo Cereda Jan 29 '14 at 18:06
@PauloCereda: oh, I may have some clues for that; let me have a look tomorrow. –  Claudio Fiandrino Jan 29 '14 at 19:02

With PSTricks just for fun as usual. No clipping used here.

\documentclass[pstricks,border=12pt]{standalone}
\begin{document}
\begin{pspicture}[linecolor=blue](-4,-2)(4,2)
\pscustom*{\psellipticarcn(4,1){180}{0}\psellipticarc(0,.2)(3,.5){0}{180}}
\pscircle*[linecolor=red]{2}
\pscustom*{\psellipticarc(4,1){180}{0}\psellipticarcn(0,.2)(3,.5){0}{180}}
\end{pspicture}
\end{document}


-
Can you make it animated with an actual asteroid belt rotating :-) just saying.... –  azetina Jan 25 '14 at 23:24
@azetina: I am thinking of it. –  please stop stalking Jan 25 '14 at 23:40

Not sure I like the colour scheme, so here's a more restrained Metapost version, featuring the useful buildcycle macro.

% prologues :=3; outputtemplate := saturn.eps; % uncomment if you want these
beginfig(1);
path globe, gap, ring[], limb[];
globe = fullcircle scaled 2cm;
gap   = fullcircle xscaled 3cm yscaled .8cm;
ring1  = fullcircle xscaled 4cm yscaled 1.2cm;
ring2  = ring1 scaled 0.93;
ring3  = ring1 scaled 0.89;
limb1 = buildcycle(subpath (5,7) of ring1, subpath (8,4) of globe);
limb2 = buildcycle(subpath (5,7) of gap, subpath (-2,6) of globe);
picture saturn; saturn = image(
fill ring1 withcolor .1red+.1green+.4white;
fill ring2 withcolor .2white;
fill ring3 withcolor .1red+.1green+.6white;
unfill gap;
fill limb1 withcolor .2red+.1green+.7white;
fill limb2 withcolor .2red+.1green+.7white;
);
draw saturn rotated 30;
endfig;
end.  % don't include this if you are making this part of another file


-
Really cool drawing! –  Svend Tveskæg Jan 29 '14 at 16:42
Impressively economical code. –  Faheem Mitha Jan 29 '14 at 22:58
I tried to learn MetaPost a bit, did not see buildcycle yet. You made a lot of impressive examples here. Would you like to write a MetaPost article on LaTeX-Community.org on MetaPost which explains some clever features, motivating potential learners with such nice images? I gladly return the favor with a 500 bounty, but more important is that readers there will like it for sure and we will bring MetaPost again in a focus. You could contact me via stefan@texblog.net if you like. –  Stefan Kottwitz Jan 28 at 15:37