TITLE: Finding the angle of rotation of an ellipse from its general equation and the other way around
QUESTION [17 upvotes]: The general equation for an ellipse is $Ax^2+Bxy+Cy^2+D=0$. How do I find the angle of rotation, the dimensions, and the coordinates of the center of the ellipse from the general equation and vice versa? Please avoid using matrices or parametric equations. I'd like all-in-one equations for each parameter.

REPLY [4 votes]: For the vice versa part of the question: the parameterization for the ellipse centered at the origin and with semi major axis equal to $a$ (parallel to the frame of reference) and semi minor axis equal to $b$ is
$$\begin{pmatrix}x\\y\end{pmatrix}=\begin{pmatrix}a\cos\varphi\\b\sin\varphi\end{pmatrix}\qquad\varphi\in[0,2\pi)$$
If I rotate the ellipse of the angle $\theta$ with the rotation matrix $R=\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix}$ I will get:
$$\begin{pmatrix}x\\y\end{pmatrix}=\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix}\cdot\begin{pmatrix}a\cos\varphi\\b\sin\varphi\end{pmatrix}$$
If I translate the center from $(0,0)$ to $(x_c,y_c)$ I will get:
$$\begin{pmatrix}x\\y\end{pmatrix}=\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix}\cdot\begin{pmatrix}a\cos\varphi\\b\sin\varphi\end{pmatrix}+\begin{pmatrix}x_c\\y_c\end{pmatrix}$$
Now I would like to isolate $\cos\varphi$ and $\sin\varphi$, then square them and finally get the quadratic form.
$$\begin{pmatrix}x-x_c\\y-y_c\end{pmatrix}=\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix}\cdot\begin{pmatrix}a\cos\varphi\\b\sin\varphi\end{pmatrix}$$
Since $R$ is a rotation matrix, it holds $R^{-1}=R^T$ and so I can multiply both sides by $R^T$:
$$\begin{pmatrix}\cos\theta&\sin\theta\\-\sin\theta&\cos\theta\end{pmatrix}\cdot\begin{pmatrix}x-x_c\\y-y_c\end{pmatrix}=\begin{pmatrix}a\cos\varphi\\b\sin\varphi\end{pmatrix}$$
Now I do the matrix vector product:
$$\cos(\theta)(x-x_c)+\sin(\theta)(y-y_c)=a\cos\varphi$$
$$-\sin(\theta)(x-x_c)+\cos(\theta)(y-y_c)=b\sin\varphi$$
and I isolate $\cos\varphi$ and $\sin\varphi$:
$$\frac{\cos(\theta)(x-x_c)+\sin(\theta)(y-y_c)}{a}=\cos\varphi$$
$$\frac{-\sin(\theta)(x-x_c)+\cos(\theta)(y-y_c)}{b}=\sin\varphi$$
Now, since $\cos^2\varphi+\sin^2\varphi=1$, I get
$$\left(\frac{\cos(\theta)(x-x_c)+\sin(\theta)(y-y_c)}{a}\right)^2+
\left(\frac{\cos(\theta)(y-y_c)-\sin(\theta)(x-x_c)}{b}\right)^2=1$$
I will now use sympy to extract the coefficients $A$, $B$, $C$, $D$, $E$, $F$ of the quadratic form $$Ax^2 + By^2 + Cxy + Dx + Ey + F = 0$$
from sympy import *
theta,x_c,y_c,a,b,x,y=symbols('theta x_c y_c a b x y')
exp_1=((cos(theta)*(x-x_c)+sin(theta)*(y-y_c))/a)**2+((cos(theta)*(y-y_c)-sin(theta)*(x-x_c))/b)**2-1
exp_2=expand(exp_1)
P=poly(exp_2,x,y)
A,B,C,D,E,F=symbols('A B C D E F')
A_=P.coeff_monomial(x**2)
B_=P.coeff_monomial(y**2)
C_=P.coeff_monomial(x*y)
D_=P.coeff_monomial(x)
E_=P.coeff_monomial(y)
F_=P.coeff_monomial(1)
print(latex(relational.Eq(A, A_)))
print(latex(relational.Eq(B, B_)))
print(latex(relational.Eq(C, C_)))
print(latex(relational.Eq(D, D_)))
print(latex(relational.Eq(E, E_)))
print(latex(relational.Eq(F, F_)))

$$A = \frac{1}{b^{2}} \sin^{2}{\left (\theta \right )} + \frac{1}{a^{2}} \cos^{2}{\left (\theta \right )}$$
$$B = \frac{1}{b^{2}} \cos^{2}{\left (\theta \right )} + \frac{1}{a^{2}} \sin^{2}{\left (\theta \right )}$$
$$C = - \frac{2}{b^{2}} \sin{\left (\theta \right )} \cos{\left (\theta \right )} + \frac{2}{a^{2}} \sin{\left (\theta \right )} \cos{\left (\theta \right )}$$
$$D = - \frac{2 x_{c}}{b^{2}} \sin^{2}{\left (\theta \right )} + \frac{2 y_{c}}{b^{2}} \sin{\left (\theta \right )} \cos{\left (\theta \right )} - \frac{2 x_{c}}{a^{2}} \cos^{2}{\left (\theta \right )} - \frac{2 y_{c}}{a^{2}} \sin{\left (\theta \right )} \cos{\left (\theta \right )}$$
$$E = \frac{2 x_{c}}{b^{2}} \sin{\left (\theta \right )} \cos{\left (\theta \right )} - \frac{2 y_{c}}{b^{2}} \cos^{2}{\left (\theta \right )} - \frac{2 x_{c}}{a^{2}} \sin{\left (\theta \right )} \cos{\left (\theta \right )} - \frac{2 y_{c}}{a^{2}} \sin^{2}{\left (\theta \right )}$$
$$F = -1 + \frac{x_{c}^{2}}{b^{2}} \sin^{2}{\left (\theta \right )} - \frac{2 x_{c}}{b^{2}} y_{c} \sin{\left (\theta \right )} \cos{\left (\theta \right )} + \frac{y_{c}^{2}}{b^{2}} \cos^{2}{\left (\theta \right )} + \frac{x_{c}^{2}}{a^{2}} \cos^{2}{\left (\theta \right )} + \frac{2 x_{c}}{a^{2}} y_{c} \sin{\left (\theta \right )} \cos{\left (\theta \right )} + \frac{y_{c}^{2}}{a^{2}} \sin^{2}{\left (\theta \right )}$$
Inspired by Compute center, axes and rotation from equation of ellipse
See also https://en.wikipedia.org/wiki/Ellipse#General_ellipse