<p>There are many different ways of doing it.</p>

<p>One way is to use the Keplerian elements and their rates obtained from the <a href="http://ssd.jpl.nasa.gov/" rel="nofollow">JPL Solar System Dynamics</a> 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 <a href="http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf" rel="nofollow">"Keplerian Elements for Approximate Positions of the Major Planets"</a> by E.M. Standish.  </p>

<p>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 <a href="http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf" rel="nofollow">document</a> to obtain the ecliptic coordinates are very easy to implement in software.</p>

CC BY-SA 3.0
2016-10-26T10:11:11.033Z
2016-10-26T10:11:11.033Z
Christo
<p>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 <a href="http://ssd.jpl.nasa.gov/?horizons" rel="nofollow">HORIZONS</a>, but looking over it I did not see anything to fulfill my questions.</p>

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

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

<p>Thanks!</p>

<planet><solar-system><distances><ephemeris>
How to calculate the current distance between two planets
Orbit
# How to calculate the current distance between two planets

(Asked by: username_0 on 2016-10-26)

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](http://ssd.jpl.nasa.gov/?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!

**Tags:** planet, solar-system, distances, ephemeris


---

## 1 answers

### Answer by username_1 on 2016-10-26. Score: 3

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](http://ssd.jpl.nasa.gov/) 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"](http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf) 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](http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf) to obtain the ecliptic coordinates are very easy to implement in software.


---

