<p>I assume that the Sun is at the origin: (0,0).  Then we can look at this image of an ellipse: <a href="https://i.sstatic.net/8i0c2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/8i0c2.png" alt="enter image description here" /></a></p>
<p>The foci are at +-c, where c = ea, by the <a href="https://en.wikipedia.org/wiki/Ellipse#Eccentricity" rel="nofollow noreferrer">definition of eccentricity</a>. To put the perigee on the negative y-axis, we can then say the center of the ellipse is at (0,ea).  Then, to account for the argument of periapsis, w, we need to rotate the ellipse (and ellipse center) counter-clockwise around the origin to get the coordinates for the ellipse center, <strong>(x,y) =(<em>-ea</em>sin(w),<em>ea</em>cos(w))</strong>.  A better approximation would include the inclination, i, for a 2-D projection, but the inclination is small for Mercury so it may not matter for the purposes of your animation.  This diagram for the orbital elements should also help:</p>
<p><a href="https://i.sstatic.net/ckspN.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ckspN.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2021-03-23T05:20:35.913Z
I had already found a solution, so I'm posting it, but yours seems much simpler. Will try it tomorrow. Thank you!
Rodrigo
CC BY-SA 4.0
2021-03-22T23:49:55.930Z
2021-03-23T01:23:36.513Z
2021-03-23T01:23:36.513Z
Connor Garcia
<p>I have calculated the perihelion and aphelion distances,</p>
<pre><code>q = a*(1-e); // perihelion distance
Q = a*(1+e); // aphelion distance
</code></pre>
<p>the perihelion and aphelion longitudes,</p>
<pre><code>angPeri = (lon-v)*Math.PI/180;
angAph = (lon-v+180)*Math.PI/180;
</code></pre>
<p>the perihelion and aphelion coordinates,</p>
<pre><code>xPeri = q*Math.cos(angPeri);
yPeri = q*Math.sin(angPeri);
xAph = Q*Math.cos(angAph);
yAph = Q*Math.sin(angAph);
</code></pre>
<p>then the center of the ellipse,</p>
<pre><code>xEll = (xPeri+xAph)/2;
yEll = (yPeri+yAph)/2;
</code></pre>
<p>calculated <code>a</code> proportional to the canvas size,</p>
<pre><code>aPx = a*maxPx/maxAU;
</code></pre>
<p><code>maxPx</code> is the radius of the Mars orbit in pixels, <code>maxAU</code> is the radius of the Mars orbit in Astronomic Units. Then I've finally drawn the ellipse,</p>
<pre><code>ctx.ellipse(canvasWidth/2+xEll,canvasHeight/2-yEll,aPx,aPx*Math.sqrt(1-e*e),angAph,0,2*Math.PI);
</code></pre>
<p><code>canvasWidth/2</code> and <code>canvasHeight/2</code> mean that the Sun is positioned in the center of the canvas.</p>

CC BY-SA 4.0
2021-03-23T05:19:16.233Z
2021-03-23T05:19:16.233Z
Rodrigo
<p>I'm developing a website showing the movement of Solar System planets according to the passage of time. I'm using <a href="http://www.stjarnhimlen.se/comp/tutorial.html" rel="nofollow noreferrer">this reference</a> to calculate all the necessary orbital parameters.</p>
<p>These are two images some days apart.</p>
<p><a href="https://i.sstatic.net/BeYuV.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/BeYuV.png" alt="Mercury far from the Sun" /></a></p>
<p><a href="https://i.sstatic.net/jGwoj.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/jGwoj.png" alt="Mercury close to the Sun" /></a></p>
<p>(Only planets up to Mars are shown here. The moon orbit has been highly exaggerated to become visible. Celestial bodies radii are not to scale either, except among themselves—except the Sun.)</p>
<p>The images show the high eccentricity of Mercury's orbit, causing it to be very far from the Sun in the first image, and very close to it in the second.</p>
<p>I want to draw the correct ellipse, instead of circles that constantly change radius. I have all the “orbital elements” given in the link above (N, i, w, a, e, M, r, v, x, y, z, lon, lat), but don't know how to extract an ellipse from them. I'm drawing the planets using x, y heliocentric coordinates. Even if I had the ellipse foci, JavaScript needs the geometric center of the ellipse to draw it, and I don't know either how to convert the former into the latter.</p>
<p>How to calculate the ellipse center from the parameters given above?</p>

CC BY-SA 4.0
2021-03-23T05:04:47.073Z
@uhoh The place is already accurate (at least I think so). That's why the circles change radius between different dates.
Rodrigo
CC BY-SA 4.0
2021-03-23T06:00:40.497Z
Oh I've got you now; you have the correct trajectory, you just need the "trail". What I normally do is just run each planet for exactly one period, then connected the points with a curve, spline, or just line segments rather than force an other object to match reality. I think it's easier because you have everything you need already, no extra function to break later if you change units.
uhoh
<solar-system><orbital-elements>
How to calculate center (not focus) of planetary orbit?
Rodrigo
# How to calculate center (not focus) of planetary orbit?

(Asked by: username_0 on 2021-03-22)

I'm developing a website showing the movement of Solar System planets according to the passage of time. I'm using [this reference](http://www.stjarnhimlen.se/comp/tutorial.html) to calculate all the necessary orbital parameters.

These are two images some days apart.

[![Mercury far from the Sun](https://i.sstatic.net/BeYuV.png)](https://i.sstatic.net/BeYuV.png)

[![Mercury close to the Sun](https://i.sstatic.net/jGwoj.png)](https://i.sstatic.net/jGwoj.png)

(Only planets up to Mars are shown here. The moon orbit has been highly exaggerated to become visible. Celestial bodies radii are not to scale either, except among themselves—except the Sun.)

The images show the high eccentricity of Mercury's orbit, causing it to be very far from the Sun in the first image, and very close to it in the second.

I want to draw the correct ellipse, instead of circles that constantly change radius. I have all the “orbital elements” given in the link above (N, i, w, a, e, M, r, v, x, y, z, lon, lat), but don't know how to extract an ellipse from them. I'm drawing the planets using x, y heliocentric coordinates. Even if I had the ellipse foci, JavaScript needs the geometric center of the ellipse to draw it, and I don't know either how to convert the former into the latter.

How to calculate the ellipse center from the parameters given above?

**Tags:** solar-system, orbital-elements
> **Comments:**
> **username_0**: @uhoh The place is already accurate (at least I think so). That's why the circles change radius between different dates.  
> **username_1**: Oh I've got you now; you have the correct trajectory, you just need the "trail". What I normally do is just run each planet for exactly one period, then connected the points with a curve, spline, or just line segments rather than force an other object to match reality. I think it's easier because you have everything you need already, no extra function to break later if you change units.  

---

## 2 answers

### Answer by username_2 on 2021-03-22. Score: 3

I assume that the Sun is at the origin: (0,0). Then we can look at this image of an ellipse: [![enter image description here](https://i.sstatic.net/8i0c2.png)](https://i.sstatic.net/8i0c2.png)

The foci are at +-c, where c = ea, by the [definition of eccentricity](https://en.wikipedia.org/wiki/Ellipse#Eccentricity). To put the perigee on the negative y-axis, we can then say the center of the ellipse is at (0,ea). Then, to account for the argument of periapsis, w, we need to rotate the ellipse (and ellipse center) counter-clockwise around the origin to get the coordinates for the ellipse center, **(x,y) =(*-ea*sin(w),*ea*cos(w))**. A better approximation would include the inclination, i, for a 2-D projection, but the inclination is small for Mercury so it may not matter for the purposes of your animation. This diagram for the orbital elements should also help:

[![enter image description here](https://i.sstatic.net/ckspN.png)](https://i.sstatic.net/ckspN.png)

> **Comments:**
> **username_0**: I had already found a solution, so I'm posting it, but yours seems much simpler. Will try it tomorrow. Thank you!  

---

### Answer by username_0 on 2021-03-23. Score: 1

I have calculated the perihelion and aphelion distances,
q = a*(1-e); // perihelion distance
Q = a*(1+e); // aphelion distance

the perihelion and aphelion longitudes,
angPeri = (lon-v)*Math.PI/180;
angAph = (lon-v+180)*Math.PI/180;

the perihelion and aphelion coordinates,
xPeri = q*Math.cos(angPeri);
yPeri = q*Math.sin(angPeri);
xAph = Q*Math.cos(angAph);
yAph = Q*Math.sin(angAph);

then the center of the ellipse,
xEll = (xPeri+xAph)/2;
yEll = (yPeri+yAph)/2;

calculated a proportional to the canvas size,
aPx = a*maxPx/maxAU;

maxPx is the radius of the Mars orbit in pixels, maxAU is the radius of the Mars orbit in Astronomic Units. Then I've finally drawn the ellipse,
ctx.ellipse(canvasWidth/2+xEll,canvasHeight/2-yEll,aPx,aPx*Math.sqrt(1-e*e),angAph,0,2*Math.PI);

canvasWidth/2 and canvasHeight/2 mean that the Sun is positioned in the center of the canvas.


---

