[Math] Find enpoints of major axis of an arbitrary ellipse using its general equation

conic sectionslinear algebra

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.

Best Answer

If you are familiar with eigenvalues and eigenvectors, and some linear-algebra, following should be pretty much straight forward. Otherwise, you should look for other answers. Implementing them in code should also be easy in any language. I leave some of the details to be filled in.

The basic idea is that every ellipse is a rotated, axis-scaled and translated version of a circle. Axis-scaled in the sense, each axis will be given a different scaling. So once you find the endpoints of this circle, you work backwards, through all this operations (in the reverse order) to find the endpoints of the ellipse.

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}$.

Related Question