If (0, 0) is the center, than equation of Your ellipse is:
F(x, y) = Ax^2 +By^2 + Cxy + D = 0
For any given ellipse, not all of the coefficients A, B, C and D are uniquely determined. One can multiply the equation by any nonzero constant and obtain new equation of the same ellipse.
4 points You have, give You 4 equations, but since those points are two pairs of symmetrical points, those equations won't be independent. You will get 2 independent equations. You can get 2 more equations by using the fact, that the ellipse is tangent to the rectangle in hose points (that's how I understand it).
So if F(x, y) = Ax^2 +By^2 + Cxy + D Your conditions are:
dF/dx = 0 in points (-2.5,6) and (2.5,-6)
dF/dy = 0 in points (-5,3) and (5,-3)
Here are four linear equations that You get
F(5, -3) = 5^2 * A + (-3)^2 * B + (-15) * C + D = 0
F(2.5, -6) = (2.5)^2 * A + (-6)^2 * B + (-15) * C + D = 0
dF(2.5, -6)/dx = 2*(2.5) * A + (-6) * C = 0
dF(5, -3)/dy = 2*(-3) * B + 5 * C = 0
After a bit of cleaning:
25A + 9B - 15C + D = 0 //1
6.25A + 36B - 15C + D = 0 //2
5A - 6C = 0 //3
- 6B + 5C = 0 //4
Still not all 4 equations are independent and that's a good thing. The set is homogeneous and if they were independent You would get unique but useless solution A = 0, B = 0, C = 0, D = 0.
As I said before coefficients are not uniquely determined, so You can set one of the coefficient as You like and get rid of one equation. For example
25A + 9B - 15C = 1 //1
5A - 6C = 0 //3
- 6B + 5C = 0 //4
From that You get: A = 4/75, B = 1/27, C = 2/45 (D is of course -1)
Now, to get to the angle, apply transformation of the coordinates:
x = ξcos(φ) - ηsin(φ)
y = ξsin(φ) + ηcos(φ)
(I just couldn't resist to use those letters :) )
to the equation F(x, y) = 0
F(x(ξ, η), y(ξ, η)) = G(ξ, η) =
A (ξ^2cos^2(φ) + η^2sin^2(φ) - 2ξηcos(φ)sin(φ))
+ B (ξ^2sin^2(φ) + η^2cos^2(φ) + 2ξηcos(φ)sin(φ))
+ C (ξ^2cos(φ)sin(φ) - η^2cos(φ)sin(φ) + ξη(cos^2(φ) - sin^2(φ))) + D
Using those two identities:
2cos(φ)sin(φ) = sin(2φ)
cos^2(φ) - sin^2(φ) = cos(2φ)
You will get coefficient C' that stands by the product ξη in G(ξ, η) to be:
C' = (B-A)sin(2φ) + Ccos(2φ)
Now your question is: For what angle φ coefficient C' disappears (equals zero)
There is more than one angle φ as there is more than one axis. In case of the main axis B' > A'
- Transform the coordinates of all four corners
- Find the smallest of all four x's as
min_x
- Find the largest of all four x's and call it
max_x
- Ditto with the y's
- Your bounding box is
(min_x,min_y), (min_x,max_y), (max_x,max_y), (max_x,min_y)
AFAIK, there isn't any royal road that will get you there much faster.
If you are wondering how to transform the coordinates, try:
x2 = x0+(x-x0)*cos(theta)+(y-y0)*sin(theta)
y2 = y0-(x-x0)*sin(theta)+(y-y0)*cos(theta)
where (x0,y0) is the center around which you are rotating. You may need to tinker with this depending on your trig functions (do they expect degrees or radians) the sense / sign of your coordinate system vs. how you are specifying angles, etc.
Best Answer
I assumed atan(x) and cot(x) were interchangeable - turns out they're not. Who would've thought ;)
My working AS3 code ended up looking like this: