Given the ellipse
$$ E(x, \ y) = ax^2 + bxy + cy^2 + dx + ey + f = 0 $$
With $4ac - b^2 = 1$
How can I compute the projection $(x_p, \ y_p)$ of the point $(x_0, y_0)$ in this ellipse?
$$ E(x_p, \ y_p) = 0 $$
Motivation: There is already a direct algorithm to fit ellipses from a set of datapoints as shown here where I can get the coefficients.
In linear interpolation, there's the R-Squared indicator bellow that tells us how good the model is. With $R_2 = 1$, all the points are in the line.
$$ R_2 = \dfrac{\left[n\sum x_iy_i - \left(\sum x_i\right)\left(\sum y_i\right) \right]^2}{\left[n\sum x_i^2 - \left(\sum x_i\right)^2\right]\left[n\sum y_i^2 - \left(\sum y_i\right)^2\right]} $$
But for the ellipse problem I don't know the formula of $R_2$. Then I want to compute the mean distance between the points and the ellipse to know how well my datapoints fits an ellipse.
To compute the distance of each point I need the projection point.
From the algebraic equation
$ a x^2 + b xy + c y^2 + d x + e y + f = 0 $
Obtain the vector parametric equation
$ r = C + E_1 \cos(t) + E_2 \sin(t) $
where $r =[x,y]^T$ is the coordinate vector, $C$ is the center, $E_1, E_2$ are the semi-axes vectors (minor and major), they are all $2 \times 1$ vectors.
The derivative of $r$ with respect to $t$ gives a tangent vector to the ellipse.
$ \dot{r} = - E_1 \sin(t) + E_2 \cos(t) $
Now we want $r$ such that this tangent vector is perpendicular to the vector $r - P$, where $P=(x_0, y_0) $ is the point to be projected onto the ellipse.
Hence, we now have
$ (r - P) \cdot \dot{r} = 0 $
which in expanded form is
$ \left(C - P + E_1 \cos(t) + E_2 \sin(t) \right) \cdot \left(-E_1 \sin(t) + E_2 \cos(t) \right) = 0$
And this leads directly to
$ C_1 \cos(t) + C_2 \sin(t) + C_3 \cos(2t) + C_4 \sin(2t) = 0 \hspace{30pt}(*) $
where
$C_1 = (C-P)\cdot E_2 , \\C_2 = -(C-P) \cdot E_1,\\ C_3 = E_1 \cdot E_2 ,\\ C_4 = \frac{1}{2} \left(E_2 \cdot E_2 - E_1 \cdot E_1 \right) $
Note that since $E_1$ is normally perpendicular to $E_2 $ as they represent the semi-minor and semi-major axes , then $C_3 = 0 $
Equation $(*)$ can be solved exactly for its roots $\{t_i\}$ which are a maximum of $4$ in number. To calculate the projection of $P$ we have to compute
$ P_i = C + E_1 \cos(t_i) + E_2 \sin(t_i) $
And then we find the distances $\| P_i - P \| $ for all the possible points $P_i$, and choose the minimum of these. The minimizing $P_i$ is the projection of $P$.