Kepler's first law

Coming Soon?

Kepler's first law

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:
  1. The orbit of every planet is an ellipse with the Sun at one of the two foci.
  2. A line joining a planet and the Sun sweeps out equal areas during equal intervals of time.
  3. The square of the orbital period of a planet is directly proportional to the cube of the semi-major axis of its orbit.
So, I decided that I want to make some toys to illustrate Kepler's Laws. I realized, though, that I don't remember much about ellipses. I remember that there are two foci, and that a ray from either focus reflected off of the inner surface of the ellipse will intersect the other focus. That doesn't really help me draw one, though. Wikipedia to the rescue! Other useful things to know about ellipses:
  1. The sum of distances from the two foci to any point on the ellipse is the same as to any other point.
  2. 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))
Bingo! That will let me draw a simple ellipse at the origin, bounded by a rectangle with corners at (-a, -b) and (a, b):
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))