I have the equation of an ellipse, with known coefficients, as follows:
$ A x^{2} + B x y + C y^{2} + D x + E y + F = 0$, where $ B^{2} - 4 A C < 0 $
This ellipse is perfectly general: While it is an ellipse, its center may not be at the origin and it may be rotated in the XY plane.
I also have a point in the XY plane, which may be inside, outside, or on the ellipse.
Is there a closed form solution for the distance between the point, and the nearest point to it on the ellipse?
(Please note that I do not believe this is a duplicate. There are several similar questions, but always with simplifying assumptions restricting the position and/or orientation of the ellipse, either in the question itself or in the answers.)
This is just a try to solve:
The parametric equations of an ellipse at the origin is
$$\begin{align*}\vec{c_0(t)} &= \begin{bmatrix}x(t)\\y(t)\end{bmatrix}\\&= \begin{bmatrix} a ~\cos(t)\\b ~\sin(t)\end{bmatrix}\end{align*} $$
Rotate the ellipse $\theta_z$ about the $z$ axis and get this
$$\begin{align*}\vec{c_1(t)} &=R_{\theta_z}~\vec{c_0}(t)\\&= \begin{bmatrix}\cos(\theta_z)&&-\sin(\theta_z)\\\sin(\theta_z)&&\cos(\theta_z)\end{bmatrix}\vec{c_0}(t)\\&= \begin{bmatrix}\cos(\theta_z)&&-\sin(\theta_z)\\\sin(\theta_z)&&\cos(\theta_z)\end{bmatrix}\begin{bmatrix}x(t)\\y(t)\end{bmatrix}\\&= \begin{bmatrix}x(t)~\cos(\theta_z)- y(t)~\sin(\theta_z) \\ x(t)~\sin(\theta_z)+ y(t)~\cos(\theta_z)\end{bmatrix}\\&= \begin{bmatrix}a ~\cos(t)~\cos(\theta_z)- b ~\sin(t)~\sin(\theta_z) \\ a ~\cos(t)~\sin(\theta_z)+ b ~\sin(t)~\cos(\theta_z)\end{bmatrix} \end{align*}$$
Translate the ellipse by a vector $\vec{v}_{T}=(\Delta x ~ ~ \Delta y)^T $ to some other place other than the origin
$$\begin{align*}\vec{c_2}(t) &= \vec{c_1}(t) + \vec{v}_T \\&=\begin{bmatrix}a ~\cos(t)~\cos(\theta_z)- b ~\sin(t)~\sin(\theta_z) \\ a ~\cos(t)~\sin(\theta_z)+ b ~\sin(t)~\cos(\theta_z)\end{bmatrix}+\begin{bmatrix}\Delta x \\ \Delta y\end{bmatrix}\\&= \begin{bmatrix}a ~\cos(t)~\cos(\theta_z)- b ~\sin(t)~\sin(\theta_z)+\Delta x \\ a ~\cos(t)~\sin(\theta_z)+ b ~\sin(t)~\cos(\theta_z)+\Delta y\end{bmatrix} \end{align*}$$
We could have also first translated and then rotated to get
$$\begin{align*}\vec{c_{2'}}(t)&=R_{\theta_z}~(\vec{c_0}(t)+ \vec{v}_T)\\&=\begin{bmatrix}\cos(\theta_z)&&-\sin(\theta_z)\\\sin(\theta_z)&&\cos(\theta_z)\end{bmatrix} \left( \begin{bmatrix} a ~\cos(t)\\b ~\sin(t)\end{bmatrix}+\begin{bmatrix}\Delta x\\\Delta y \end{bmatrix}\right)\\&= \begin{bmatrix}\cos(\theta_z)&&-\sin(\theta_z)\\\sin(\theta_z)&&\cos(\theta_z)\end{bmatrix} \begin{bmatrix} a ~\cos(t)+\Delta x\\b ~\sin(t)+\Delta y\end{bmatrix}\\&=\begin{bmatrix} a~\cos(\theta_z) ~\cos(t)-b~\sin(\theta_z)~\sin(t)+\Delta x ~\cos(\theta_z)-\Delta y ~\sin(\theta_z)\\a~\sin(\theta_z) ~\cos(t)+b~\cos(\theta_z)~\sin(t)+\Delta x~ \sin(\theta_z)+\Delta y ~\cos(\theta_z)\end{bmatrix}\end{align*}$$
From this expression we can calculate the position of a point on the circumference of an ellipse for a parameter value $t$. Given a point $\vec{P} = (x_0 ~ ~ y_0)^T$ in the plane, on the ellipse, inside oder outside the ellipse, we can calculate the distance $d$ between this point $\vec{P}$ and "a general point" on the ellipse like this:
$$\begin{align*}d(\vec{c_2}(t),\vec{P})&=f(t)\\&=\left\|\vec{c_2}(t)-\vec{P}\right\|\\&=\left\|\begin{bmatrix}a ~\cos(t)~\cos(\theta_z)- b ~\sin(t)~\sin(\theta_z)+\Delta x \\ a ~\cos(t)~\sin(\theta_z)+ b ~\sin(t)~\cos(\theta_z)+\Delta y\end{bmatrix}-\begin{bmatrix} x_0\\y_0\end{bmatrix}\right\|\\&=\left\|\begin{bmatrix}a ~\cos(t)~\cos(\theta_z)- b ~\sin(t)~\sin(\theta_z)+\Delta x-x_0 \\ a ~\cos(t)~\sin(\theta_z)+ b ~\sin(t)~\cos(\theta_z)+\Delta y-y_0\end{bmatrix}\right\|\\&=\sqrt{(a ~\cos(t)~\cos(\theta_z)- b ~\sin(t)~\sin(\theta_z)+\Delta x-x_0 )^2+(a ~\cos(t)~\sin(\theta_z)+ b ~\sin(t)~\cos(\theta_z)+\Delta y-y_0)^2}\end{align*}$$
To find out the shortest distance $d$ between $\vec{P}$ and the ellipse we will have to find the value of $t$ that minimizes the value of the following expression (under the square root above):
$$(a ~\cos(\theta_z)~\cos(t)- b ~\sin(\theta_z)~\sin(t)+\Delta x-x_0 )^2+(a~\sin(\theta_z) ~\cos(t)+ b ~\cos(\theta_z)~\sin(t)+\Delta y-y_0)^2$$
This isn't a very easy task, but if we find such a $t$ then we are done, because we can plug it in $\vec{c_{2}}(t)$ or $\vec{c_{2'}}(t)$ to get our point on the ellipse, which has the shortest distance from $\vec{P}$. Maybe this is not the best approach to find out $t$, look what I found here. Maybe we should calculate the tangent:
$$\vec{c'_2}(t) = \begin{bmatrix}-a ~\sin(t)~\cos(\theta_z)- b ~\cos(t)~\sin(\theta_z)\\ -a ~\sin(t)~\sin(\theta_z)+ b ~\cos(t)~\cos(\theta_z)\end{bmatrix} $$
and calculate the vector $\vec{l}(t)$ from the point $\vec{P}$ to a general point on the ellipse, we have it already :
$$\vec{l}(t) = \vec{c_2}(t)-\vec{P} = \begin{bmatrix}a ~\cos(t)~\cos(\theta_z)- b ~\sin(t)~\sin(\theta_z)+\Delta x-x_0 \\ a ~\cos(t)~\sin(\theta_z)+ b ~\sin(t)~\cos(\theta_z)+\Delta y-y_0\end{bmatrix} $$
Our $t$ can also be found with the condition that the vectors $\vec{l}(t)$ and $\vec{c'_2}(t)$ have to be perpendicular to each other, their dot product has to vanisch. This means: the $t$, that makes the following expression is equal to $0$ is the good old $t$ that we've been looking for:
$$(a ~\cos(t)~\cos(\theta_z)- b ~\sin(t)~\sin(\theta_z)+\Delta x-x_0)(-a ~\sin(t)~\cos(\theta_z)- b ~\cos(t)~\sin(\theta_z))+(a ~\cos(t)~\sin(\theta_z)+ b ~\sin(t)~\cos(\theta_z)+\Delta y-y_0)(-a ~\sin(t)~\sin(\theta_z)+ b ~\cos(t)~\cos(\theta_z))=0 $$
This is just exactly the same condition (Yes, it is!) we get, if we differntiate the above expression (under the square root) and set the result $=0$ to find the min. value! The question remains the same: For which $t$ do we get the minimal value under the square root or for which $t$ does this expression above (=the derivative of the expression under the square root) become $=0$?
Assume $a_1 = a\cos(\theta_z), b_1 = b\sin(\theta_z), a_2 = a\sin(\theta_z), b_2 = b\cos(\theta_z), c_1 = \Delta x - x_0$ and $c_2 = \Delta y - y_0$. Again assume $q_1 =-a_1~c_1-a_2~c_2 ,$ $q_2= -b_1~c_1+b_2~c_2,$ $q_3=(-a_1^2+b_1^2-a_2^2+b_2^2)/2$ and $q_4=-a_1~b_1+a_2~b_2$ and use the geometric identities $\cos^2(t)-\sin^2(t)=\cos(2t)$ and $\sin(t)\cos(t)=\sin(2t)/2$, then the equation becomes $$ q_1 \cdot \sin(t)+q_2 \cdot \cos(t)+ q_3 \cdot \sin(2t) + q_4 \cdot \cos(2t) =0$$
Finding a closed formula is cumbersome, brother! After some research on the topic, this equation can be solved numerically with the newton method to get the desired $t=t^*$. Then the distance will be $d_{min} = \left\|\vec{c}_2(t^*)-\vec{P}\right\|$.
Also have a look at this answer (from Doctor Rob, The Math Forum)