# A More Precise Dance of Planets with Metapost and TikZ

This is a long question and I am not sure I read everything but the `dimension too large`

error is easily taken care of. You have angles that become large, but obviously only the angle modulo 360 is relevant.

```
\documentclass[tikz,border=20pt]{standalone}
\tikzset{planets/.cd,outer distance/.store in=\outerdistance,
inner distance/.store in=\innerdistance,
outer period/.store in=\outerperiod,
inner period/.store in=\innerperiod,
max times/.store in=\maxtimes,
increment/.store in=\increment,
outer distance=22.79cm,
inner distance=5.79cm,
inner period=88,
outer period=687,
max times=2700,
increment=2}
\newcommand{\danceofplanets}[1][]{%
%\setkeys{planet}{#1}
\begin{tikzpicture}[planets/.cd,#1]
\edef\iloop{0}
\edef\x{0}
\edef\y{0}
\loop
\pgfmathsetmacro{\x}{Mod(\x+\increment,360)}
\pgfmathsetmacro{\y}{Mod(\y+(\increment/\innerperiod)*\outerperiod,360)}
\draw[help lines]
(\x:\outerdistance) --
(\y:\innerdistance);%
\edef\iloop{\the\numexpr\iloop+\increment}
\ifnum\iloop<\maxtimes\repeat
\end{tikzpicture}%
}
\begin{document}
\danceofplanets[ %% Venus and Earth
outer period=365.2,
inner period=224.7,
inner distance=5.41cm,
outer distance=7.48cm,
increment=5]
\danceofplanets[ %% Mercury and Mars
outer period=687,
inner period=88,
outer distance=22.79cm,
inner distance=5.79cm,
increment=5]
\end{document}
```

This is the result of the second picture, which previously did not work.

It should be possible to speed this up a bit but this depends also on the boundary conditions such as whether coloring is required.

This turns out to still be subject to `dimension too large`

and memory errors for (excessively?) large values of `max times`

. There are various reasons for that, some of them have to do with Ti*k*Z. Also, the compilation becomes slow. So here is a rescue for (hilariously?) large values of `max time`

: `picture`

mode!

```
\documentclass[border=20pt]{standalone}
\usepackage{xfp}
\usepackage{pgf}
\usepackage{pict2e}
\standaloneenv{picture}
\pgfkeys{/planets/.cd,outer distance/.store in=\outerdistance,
inner distance/.store in=\innerdistance,
outer period/.store in=\outerperiod,
inner period/.store in=\innerperiod,
max times/.store in=\maxtimes,
increment/.store in=\increment,
line width/.store in=\planetlinewidth,
outer distance=22.79cm,
inner distance=5.79cm,
inner period=88,
outer period=687,
max times=270,
increment=2,
line width=0.2pt}
\newcommand\PlanetDance[1][]{%
\begingroup
\pgfkeys{/planets/.cd,#1}%
\pgfmathtruncatemacro{\dx}{2*\outerdistance+1pt}%
\pgfmathtruncatemacro{\dy}{\dx/2}%
\begin{picture}(\dx,\dx)(-\dy,-\dy)
\linethickness{\planetlinewidth}%
\edef\iloop{0}%
\edef\x{0}%
\edef\y{0}%
\edef\imax{\inteval{\maxtimes/\increment}}%
\loop
\pgfmathsetmacro{\x}{Mod(\x+\increment,360)}%
\pgfmathsetmacro{\y}{Mod(\y+(\increment/\innerperiod)*\outerperiod,360)}%
\pgfmathsetmacro{\myxa}{\outerdistance*cos(\x)}%
\pgfmathsetmacro{\myya}{\outerdistance*sin(\x)}%
\pgfmathsetmacro{\myxb}{\innerdistance*cos(\y)}%
\pgfmathsetmacro{\myyb}{\innerdistance*sin(\y)}%
\Line(\myxa,\myya)(\myxb,\myyb)%
\edef\iloop{\inteval{\iloop+1}}%
\ifnum\inteval{\iloop<\imax}\repeat
\end{picture}\endgroup}
\begin{document}
\PlanetDance[ %% Venus and Earth
outer period=365.2,
inner period=224.7,
inner distance=5.41cm,
outer distance=7.48cm,
increment=5,
max times=10000]
\end{document}
```

Please note that, even though this loads `xfp`

, I kept `\pgfmathsetmacro`

s for a reason: they are faster than `\fpeval`

.

We can also use `fpu`

, which however somewhat slows down the compilation. Since `mod`

is not implemented in `fpu`

we have to do that ourselves. (I also switched to pgf keys.)

```
\documentclass[tikz,border=20pt]{standalone}
\usetikzlibrary{fpu}
\tikzset{planets/.cd,outer distance/.store in=\outerdistance,
inner distance/.store in=\innerdistance,
outer period/.store in=\outerperiod,
inner period/.store in=\innerperiod,
max times/.store in=\maxtimes,
increment/.store in=\increment,
outer distance=22.79cm,
inner distance=5.79cm,
inner period=88,
outer period=687,
max times=2700,
increment=2}
\newcommand{\danceofplanets}[1][]{%
%\setkeys{planet}{#1}
\begin{tikzpicture}[planets/.cd,#1]
\edef\iloop{0}
\loop
\let\x\iloop
\begingroup
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathsetmacro{\y}{(\x/\innerperiod)*\outerperiod}
\pgfmathsetmacro{\y}{\y-int(\y/360)*360}
\pgfmathsmuggle\y
\endgroup
\pgfmathsetmacro{\x}{Mod(\x,360)}
\draw[help lines]
(\x:\outerdistance) --
(\y:\innerdistance);%% <<<--- avoids arithmetic overflow
\edef\iloop{\the\numexpr\iloop+\increment}
\ifnum\iloop<\maxtimes\repeat
\end{tikzpicture}%
}
\begin{document}
\danceofplanets[ %% Venus and Earth
outer period=365.2,
inner period=224.7,
inner distance=5.41cm,
outer distance=7.48cm,
increment=5]
% \danceofplanets[ %% Mercury and Mars - TOO LARGE
% outerperiod=687,
% innerperiod=88,
% outerdistance=22.79cm,
% innerdistance=5.79cm,
% increment=5]
\danceofplanets[ %% Mercury and Mars - needs maxtimes<2099 to work
outer period=687,
inner period=88,
outer distance=22.79cm,
inner distance=5.79cm,
increment=1,
max times=2700]
\end{document}
```

One can polish this but before doing this I would like to know if I am missing some key part of the question.

**EXTRA**: This is some prototype version that supports eccentricities and a perihelion phase (is this the right term?). It is based on the first solution, so does not support excessive values of `max times`

.

```
\documentclass[tikz,border=20pt]{standalone}
\tikzset{planets/.cd,outer distance/.store in=\outerdistance,
inner distance/.store in=\innerdistance,
outer period/.store in=\outerperiod,
inner period/.store in=\innerperiod,
outer eccentricity/.store in=\outereccentricity,
inner eccentricity/.store in=\innereccentricity,
max times/.store in=\maxtimes,
increment/.store in=\increment,
phase/.store in=\perihelionphase,
outer distance=22.79cm,
inner distance=5.79cm,
inner period=88,
outer period=687,
max times=2700,
increment=2,
outer eccentricity=1,
inner eccentricity=1,
phase=0}
\newcommand{\danceofplanets}[1][]{%
%\setkeys{planet}{#1}
\begin{tikzpicture}[planets/.cd,#1]
\edef\iloop{0}
\edef\x{0}
\edef\y{\perihelionphase}
\pgfmathsetmacro{\outerydistance}{\outereccentricity*\outerdistance}%
\pgfmathsetmacro{\innerydistance}{\innereccentricity*\innerdistance}%
\loop
\pgfmathsetmacro{\x}{Mod(\x+\increment,360)}
\pgfmathsetmacro{\y}{Mod(\y+(\increment/\innerperiod)*\outerperiod,360)}
\draw[help lines]
(\x:\outerdistance\space and \outerydistance pt) --
([rotate=-\perihelionphase]\y:\innerdistance\space and \innerydistance pt);%
\edef\iloop{\the\numexpr\iloop+\increment}
\ifnum\iloop<\maxtimes\repeat
\end{tikzpicture}%
}
\begin{document}
\danceofplanets[ %% Mercury and Mars
outer period=687,
inner period=88,
outer distance=22.79cm,
outer eccentricity=0.2,
inner distance=5.79cm,
inner eccentricity=0.3,
phase=30,
increment=5]
\end{document}
```

Not an answer; rather the tip of a musing, with pictures.

Ultimately what we are seeing is a visual representation of how gravity works: the transfer of momentum through the Solar System via the mechanism of orbital resonances, since it takes energy(mass) to warp spacetime, especially if someone else is warping it the other way. The end result is a series of 'uphills' and 'downhills', hindering or boosting movement in that direction/time pairing.

A 3D-version would be best, to see the tubes and channels.

===

Distance gives the scale (100 units for both, being a comfortable range for the 'zoom' or 'temperature' - think of gravity as being really, really slow electrons), and the time ratio gives the number of "petals", being the (dimensionless) difference between the two times.

By way of illustration:

Consider:

Difference of 5 = 5 petals

Ratio 1:6

Ratio 8:13

Ratio of 31:36

Difference of 4 = 4 petals

Ratio 1:5

Ratio 8:12

what is going on? 8:12 is really 2:3 = difference of 1. Correct.

Ratio of 31:35

Difference of 6 = 6 petals

Ratio of 1:7

Ratio of 8:14

= 4:7 = difference of 3. Correct.

Ratio of 11:17

Think why 8:14, 9:15, 10:16, all a difference of 6, won't produce 6 petals.

I leave as an exercise for the reader, ...