I've been watching Carl Sagan's Cosmos series recently. What an amazing program! I had seen bits and pieces of it, at school and on the web, but I'd never seen it in its entirety. Carl Sagan was a great man. He is definitely one of my heroes.
This week I watched the episode 3: "The Harmony of the Worlds", in which I re-learned about Kepler's Laws:
- The orbit of every planet is an ellipse with the Sun at one of the two foci.
- A line joining a planet and the Sun sweeps out equal areas during equal intervals of time.
- The square of the orbital period of a planet is directly proportional to the cube of the semi-major axis of its orbit.
- The sum of distances from the two foci to any point on the ellipse is the same as to any other point.
- Given an ellipse with axes aligned on the cartesian plane with half-lengths a and b, any point on the ellipse can be expressed in terms of an angle theta: (a * cos(theta), b * sin(theta))
But what about the focus representing the sun? How do I draw that? Well, I know that the sums of distances from the foci to any two points on the ellipse should be the same... I can approximate focus position with a simple binary search! OK, great, so now I have an approximation of the position of a focus. But I know there must be some very simple way to derive the exact position from the axis lengths... I spent quite some time trying various formulae on my own, before I finally went back to wikipedia for clues.
My first clue was the definition of ellipse eccentricity. Actually, I found *two* definitions, which helped twice as much. One was a formula:
e = sqrt(1-(b/a)^2)The other was a ratio:
The eccentricity of an ellipse is the ratio of the distance between the two foci, to the length of the major axis.Based on these two definitions I came up with:
fx = a * Math.sqrt(1-((b*b)/(a*a)))Success! But a little bit more reading on Wikipedia turned up a simpler solution still:
The distance from the center to either focus is ae, or simply sqrt(a^2-b^2)*facepalm* That's so simple! So... here's my final calculation of focus position:
fx = Math.sqrt((a*a)-(b*b))