### 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.
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://wikimho.com/us/q/astronomy/18865", "fetch_time": 1675006136000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2023-06/segments/1674764499744.74/warc/CC-MAIN-20230129144110-20230129174110-00470.warc.gz", "warc_record_offset": 624977110, "warc_record_length": 11712, "token_count": 487, "char_count": 1951, "score": 3.671875, "int_score": 4, "crawl": "CC-MAIN-2023-06", "snapshot_type": "latest", "language": "en", "language_score": 0.8817921876907349, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00034-of-00064.parquet"}