Wednesday, 29th January 2014
Finding the angle around an ellipse
When is the angle around an ellipse, not the around around the an ellipse? This is a problem which tripped me up a few times when working with elliptical orbits and arcs.
The issue is that we can (and often do) define the point on an ellipse at an angle, θ, with
x = a.cos(θ)
y = b.sin(θ)
Where a is the length of the axis aligned with the x-axis and b is the length of the axis aligned with the y-axis (I'm assuming the ellipse is oriented such that the axes). The problem is then, to find the angle, φ, around the ellipse to point (x, y).
With some simple trigonometry, we can see that:
tan(φ) = y / x
tan(φ) = b.sin(θ) / a.cos(θ)
tan(φ) = (b / a) * tan(θ)
φ = atan((b / a) * tan(θ))
Likewise, you can calculate the reverse with:
θ = atan((a / b) * tan(φ))
This is particularly useful for generating arcs in Processing.js where θ is used in the calculation for the angles to start and stop. If you want to find a point on the ellipse which aligns with another point (px, py), and the origin (say placing a planet on an elliptic orbit based on a mouse click), it's even easier.
θ = atan((a * py) / (b * px))
The hardest part of all this was realising that the angle I was using to define an elliptic arc was not the angle I expected.