# How to get the correct angle of the ellipse after approximation

I need to get the correct angle of rotation of the ellipses. These ellipses are examples. I have a canonical coefficients of the equation of the five points.

$$Ax ^ 2 + Bxy + Cy ^ 2 + Dx + Ey + F = 0$$

Ellipses:

Points:

Zero ellipse:   [16,46]  [44,19]  [50,35]  [31,61]  [17,54]
First ellipse:  [14,95]  [47,71]  [55,83]  [23,107] [16,103]
Second ellipse: [12,128] [36,117] [58,128] [35,146] [13,136]
Third ellipse:  [16,164] [29,157] [54,188] [40,195] [17,172]
Fourth ellipse: [22,236] [31,207] [50,240] [40,252] [26,244]


Coefficients:

Zero ellipse                 First ellipse                Second ellipse                Third ellipse                  Fourth ellipse
A: 0.00039679682899033947    A: 0.00007365946131786486    A: 0.000021675708916102692    A: 0.00004189611868790682      A: 0.00004418821462144713
B: 0.00021821614636627075    B: 0.00006770936206052314    B: -0.000002834437159146921   B: -0.00004283926773569747     B: -0.000012890924982902275
C: 0.00024184103978866782    C: 0.00009244517004290531    C: 0.000057745675577137415    C: 0.00003944519997403195      C: 0.000020104667194164587
D: -0.03490717401354479      D: -0.01110309000831378      D: -0.0011544916677563865     D: 0.0046141642800698515       D: -0.00016090203479326006
E: -0.026421911476591453     E: -0.01877226426820658      E: -0.015086084806642279      E: -0.012396706675782408       E: -0.008774013189179199
F: 1.0                       F: 1.0                       F: 1.0                        F: 1.0                         F: 1.0


I successfully find the coordinates of the center and the length of the axes. But I get the wrong rotation.

I get the angle using the equation: $$\theta = \frac{1}{2} \arctan \left( \frac{B}{A-C} \right)$$

Angles result:

Zero ellipse:    0.4766612094205555
First ellipse:  -0.6500786401646479
Second ellipse:  0.03921024408607312
Third ellipse:  -0.7568233305427435
Fourth ellipse: -0.24572750447422026


Visualization of the result:

How do I calculate the correct angle? And how to convert the value to degrees without errors?

# UPDATED:

I wrote the algorithm here. The first set points angle is not correct. In the second set of points, the correct angle. You can put your own values and see the result.

# UPDATED: SOLVED!

Based on the response of @Ng Chung Tak, I managed to get into the code and implement the right formula!

Implementation of the code here.

Formula:

$$\theta = \tan^{-1} \left( \frac{C-A}{B}+\frac{\sqrt{(A-C)^{2}+B^{2}}}{B} \: \right)$$

Result:

Thanks to all!

• Wouldn't it be $\theta = \arctan \left( \frac{B}{A-C} \right)$ without the $\frac{1}{2}$ due to the fact that the coefficient of $xy$ is $B$ instead $2B$ ? – Jean Marie Jun 25 '16 at 18:59
• @JeanMarie, this equation gave me here math.stackexchange.com/q/1824974 – Qulery Jun 25 '16 at 19:02
• @JeanMarie, without $1/2$ I get the following result: swinechat.com/other/image_3.jpg – Qulery Jun 25 '16 at 19:07
• I was wrong. Sorry for that. But now I see another reason: I think that you are not doing the rotation around the center of the ellipse but around the origin of the axes. – Jean Marie Jun 25 '16 at 19:15
• @JeanMarie, I make rotation about the red point (center) – Qulery Jun 25 '16 at 19:24

There're two principal axes in general, so

\begin{align*} \theta &=\frac{1}{2} \tan^{-1} \frac{B}{A-C}+\frac{n\pi}{2} \\ &= \tan^{-1} \left( \frac{C-A}{B} \color{red}{\pm} \frac{\sqrt{(A-C)^{2}+B^{2}}}{B} \: \right) \\ \end{align*}

The centre is given by $$(h,k)= \left( \frac{2CD-BE}{B^2-4AC}, \frac{2AE-BD}{B^2-4AC} \right)$$

Transforming to $$\frac{A+C \color{red}{\pm} \sqrt{(A-C)^{2}+B^{2}}}{2} x'^2+ \frac{A+C \color{red}{\mp} \sqrt{(A-C)^{2}+B^{2}}}{2} y'^2+ \frac {\det \begin{pmatrix} A & \frac{B}{2} & \frac{D}{2} \\ \frac{B}{2} & C & \frac{E}{2} \\ \frac{D}{2} & \frac{E}{2} & F \end{pmatrix}} {\det \begin{pmatrix} A & \frac{B}{2} \\ \frac{B}{2} & C \\ \end{pmatrix}}=0$$

where $\begin{pmatrix} x' \\ y' \end{pmatrix}= \begin{pmatrix} \cos \theta & \sin \theta \\ -\sin \theta & \cos \theta \end{pmatrix} \begin{pmatrix} x-h \\ y-k \end{pmatrix}$.

The axes will match, up to reflection about the axes of symmetry, when the $\color{red}{\text{case}}$ (upper or lower) agree.

Numerical example

Given five points: $(2,1)$, $(1,1)$, $(-2,-2)$, $(-1,-2)$, $(1,-1)$

$A=1$, $B=-2$, $C=2$, $D=-1$, $E=2$, $F=-2$

$$(h,k)=(0,-0.5)$$

$$\det \begin{pmatrix} A & \frac{B}{2} & \frac{D}{2} \\ \frac{B}{2} & C & \frac{E}{2} \\ \frac{D}{2} & \frac{E}{2} & F \end{pmatrix} = ACF-\frac{A E^2+C D^2+F B^2-EDB}{4}=-\frac{5}{2}$$

$$\det \begin{pmatrix} A & \frac{B}{2} \\ \frac{B}{2} & C \end{pmatrix} = -\frac{B^2}{4}+AC=1$$

$$\frac{A+C \pm \sqrt{(A-C)^{2}+B^{2}}}{2}=\frac{3 \pm \sqrt{5}}{2}$$

Using upper case convention:

$$\frac{3+\sqrt{5}}{2} x'^2+\frac{3-\sqrt{5}}{2} y'^2=\frac{5}{2}$$

$$\frac{x'^2}{a^2}+\frac{y'^2}{b^2}=1$$

$$(x',y')= (a\cos t,b\sin t)$$

where $\displaystyle \begin{pmatrix} a \\ b \end{pmatrix}= \begin{pmatrix} \sqrt{\frac{5}{3+\sqrt{5}}} \\ \sqrt{\frac{5}{3-\sqrt{5}}} \end{pmatrix}$

$$\theta = \tan^{-1} \left( \frac{C-A}{B}+\frac{\sqrt{(A-C)^{2}+B^{2}}}{B} \: \right) = \tan^{-1} \left( -\frac{\sqrt{5}+1}{2} \right) \approx -58.28^{\circ}$$

\begin{align*} \begin{pmatrix} x \\ y \end{pmatrix} &= \begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix} \begin{pmatrix} x' \\ y' \end{pmatrix}+ \begin{pmatrix} h \\ k \end{pmatrix} \\ &&\\ &= \begin{pmatrix} h+x'\cos \theta-y'\sin \theta \\ k+x'\sin \theta+y'\cos \theta \end{pmatrix} \\ &&\\ &= \begin{pmatrix} \sqrt{\frac{5}{2}+\sqrt{5}\,} \, \sin t+ \sqrt{\frac{5}{2}-\sqrt{5}\,} \, \cos t \\ -\frac{1}{2}+ \frac{\sqrt{5+\sqrt{5}}}{2} \, \sin t- \frac{\sqrt{5-\sqrt{5}}}{2} \, \cos t \end{pmatrix} \end{align*}

• On the basis of your formula, I received the following values. How can I apply them to produce correct angle? swinechat.com/other/image_7.jpg – Qulery Jun 26 '16 at 11:22
• Rearrange your transformed ellipse as $\displaystyle \left( \frac{x'}{a} \right)^2+\left( \frac{y'}{b} \right)^2=1$, then $(x',y')=( a\cos t, b\sin t)$, now do the inverse transform $\begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix} \begin{pmatrix} x' \\ y' \end{pmatrix} +\begin{pmatrix} h \\ k \end{pmatrix}$ – Ng Chung Tak Jun 26 '16 at 15:12
• Please write in the answer the call sequence of formulas that I can integrate into my code, to get the correct angle of the ellipse – Qulery Jun 26 '16 at 15:38
• I only familiar with Mathematica or Geogebra, is it OK for you? – Ng Chung Tak Jun 26 '16 at 15:50
• Yes. I need help to get the formula that I can use to get the right angle. You have written a lot of formulas and I do not understand how to properly and in what order to use them, that would get an angle on the basis of the coefficients after approximation. – Qulery Jun 26 '16 at 15:55

Justification of the formula:

After centering (translation to let the linear terms vanish), the equation becomes $$Ax^2+Bxy+Cy^2+F'=0.$$

Then you apply a rotation of angle $\theta$ to let the mixed term $Bxy$ vanish from the quadratic terms,

$$A(x\cos\theta-y\sin\theta)^2+B(x\cos\theta-y\sin\theta)(x\sin\theta+y\cos\theta)+C(x\sin\theta+y\cos\theta)^2.$$

This is achieved when

$$2(C-A)\cos\theta\sin\theta+2B(\cos^2\theta-\sin^2\theta)=(C-A)\sin(2\theta)+2B\cos(2\theta)=0.$$

(Then you can rewrite $\frac{x^2}{a^2}+\frac{y^2}{b^2}=1$.)

Hence,

$$\tan(2\theta)=\frac{2B}{A-C}.$$

The angle $\theta$ is undeterminate by a multiple of a quater turn, as the ellipse has two symmetry axis.

Although it sounds like a question, for calculation did you use atan2 function or atan function? Quadrant placement is also important.

• I used atan. I made the same way as written here math.stackexchange.com/q/1824974 – Qulery Jun 25 '16 at 21:37
• The given formula is correct. It is better to first check for trapping the error if your program runs ok when D=E=0 for the (easier, no center displacement) central conics, – Narasimham Jun 25 '16 at 22:02
• This site calculates the correct values for the ellipses on the coefficients. The problem is not coefficients? The calculation for the first three ellipses: goo.gl/qaQ7UZ goo.gl/hR0kaa goo.gl/5KU0Br – Qulery Jun 25 '16 at 22:03
• As you can see, this website makes the correct calculation using my coefficients. So, evidently, not a problem in the coefficients. swinechat.com/other/image_5.jpg – Qulery Jun 25 '16 at 22:17
• How to use my coefficients to get the right angle? Just as it makes "wolframalpha". – Qulery Jun 25 '16 at 22:29