### How to calculate the current distance between two planets

• I'd like to add a feature to one of my apps that calculates the current distance between the Earth and Mars (or any other planet, for that matter). My google searches have led me to statistics of average distances (as a planets orbit is elliptical, and therefore the distance can vary greatly), as well as articles about Keplers laws of Planetary motion and more. I have also seen some mentions of HORIZONS, but looking over it I did not see anything to fulfill my questions.

I haven't found any readings explicitly describing how to calculate the current distance between two planets. So, how is this done?

Please explain your answer and the steps needed, so that I may understand better. Pseudocode is welcome.

Thanks!

• There are many different ways of doing it.

One way is to use the Keplerian elements and their rates obtained from the JPL Solar System Dynamics web site to determine the Cartesian coordinates of the planets in the ecliptic plane at a given time. Information and data to allow computation of approximate positions for the planets is provided in the document "Keplerian Elements for Approximate Positions of the Major Planets" by E.M. Standish.

Then for each of the planets, $i$, we have the position coordinates in the form
$$\mathbf{r}_i = \left[\begin{array}{l} x_i \\ y_i \\ z_i \end{array}\right]$$
Since these coordinates are all given in the same reference frame, the displacement vector from planet $i$ to planet $j$ can be obtained from
$$\mathbf{r}_{ij} = \left[\begin{array}{l} x_{ij} \\ y_{ij} \\ z_{ij} \end{array}\right] = \left[\begin{array}{l} x_j-x_i \\ y_j-y_i \\ z_j-z_i \end{array}\right]$$
and the distance between planet $i$ and $j$ can be calculated using
$$r_{ij} = \|\mathbf{r}_{ij}\| = \sqrt{x_{ij}^2+y_{ij}^2+z_{ij}^2}$$
The method described in the document to obtain the ecliptic coordinates are very easy to implement in software.