# Find enpoints of major axis of an arbitrary ellipse using its general equation

I have a general equation of an ellipse in the form of $Ax^2+Bxy+Cy^2+Dx+Ey+f=0$. How do I find the equation of (or even endpoints would work) major axis of an ellipse. I am aware of following things:

If the ellipse was aligned to coordinate axis, the problem can be solved by converting above equation into standard form by completing squares and then just applying standard formulas. Here, since the ellipse can be arbitrary, we have to replace $x$ by $x'cos\theta-y'sin\theta$ and $y$ by $x'sin\theta+y'cos\theta$. Then perform some calculations and get the equation in standard form and get the major axis endpoints.

My question is: I want to write a code for it. I have $A, B, C, D, E, F$. I can find $\theta$ by using $cot(2\theta)=\frac{A-C}{B}$. But I am stuck beyond this point. I would also mention the following:

I do not mind using any method as long as it is possible to wrap it up in a code. To get an analytical expression by using the method of replacing $x$ by $x'cos\theta-y'sin\theta$ really seems impossible since when I tried to write it down, the equation became too long and I felt it can't be coded. When I have actual numbers, then some terms would certainly vanish and thats when equation becomes tractable.

-

You can rewrite your ellipse equation as \begin{align} x^{T}Qx-r^{T}x+f=0 \end{align} where $Q$ is the symmetric $2 \times 2$ matrix \begin{align} Q=\begin{bmatrix}A & \frac{B}{2} \\ \frac{B}{2} & C\end{bmatrix} \end{align} and $r$ is the $2 \times 1$ vector \begin{align} r=\begin{bmatrix}-D \\ -E\end{bmatrix} \end{align} Consider eigen decomposition of $Q=U^{T}DU$ where $D$ is diagonal matrix and contains its eigenvalues $\lambda_1,\lambda_2$, and $U$ is the matrix containing eigenvectors. Define $y=Ux$. Then your ellipse can be written as \begin{align} y^{T}Dy-v^{T}y+f=0 \end{align} where $v=Ur$. Define $z=D^{\frac{1}{2}}y$ where $D^{\frac{1}{2}}$ is a diagonal matrix with diagonal entries as $\sqrt{\lambda_1},\sqrt{\lambda_2}$. Thus, your ellipse equation should become \begin{align} z^{T}z-w^{T}z+f=0 \end{align} where $w=D^{\frac{-1}{2}}v$.
If $z=[z_1,z_2]^{T}$, the last equation can be written as \begin{align} (z_1-X_c)^{2} + (z_2-Y_c)^{2}=a^{2} \end{align} This you can do, using completion of squares for each terms $z_1$ and $z_2$. This is a circle with radius $a$ and center $(X_c,Y_c)$. The endpoints of the major axis are $(a,0)$ and $(-a,0)$. Now, you have to work backwards. First you shift the endpoints by adding $(X_c,Y_c)$. Then, you scale back using $D^{\frac{1}{2}}$ and then rotate using $U^{T}$.
I understood almost everything you have written. Thanks. Only doubt is, I am getting an analytical expression of $X_c = Y_c = \frac{1}{2}*\omega$. That means, $X_c$ and $Y_c$ will always be equal. I am getting a little doubt over here. Could you please clarify this? For further information, I got $a^2=\frac{1}{2}*\omega*\omega^T + f$. Or do we have to split $\omega$ also into $\omega_1$ and $\omega_2$? If yes, then $X_c = \frac{1}{2}*\omega_1$ and $Y_c = \frac{1}{2}*\omega_2$. Which is correct? – Parag S. Chandakkar Mar 27 '13 at 8:11
I got the last one, $x=1/2*w_1$,$y=1/2*w_2$ – dineshdileep Mar 27 '13 at 8:50