[Math] Finding parametric distance on quadratic curve from given $(x,y)$ point

bezier-curvequadratics

I want to get the parametric distance (the "$t$" value) at a location on a quadratic Bezier curve, given the "$x$" and "$y$" coordinates of the point.

I have start point, end point and control point of the curve.

How can I get it? Is there any direct formula to get the "$t$" value?

Please advice.

Best Answer

Assuming you want to compute the shortest distance between a point $\vec P$ and a quadratic Bezier arc $\vec Q(1-t)^2+2t(1-t)\vec R+t^2\vec S$, $0<t<1$, you need to minimize $$\delta^2(t)=\min_{t,0<t<1}\left(\vec{PQ}(1-t)^2+2t(1-t)\vec{PR}+t^2\vec{PS}\right)^2.$$ Deriving with respect to $t$, you have $$\frac{d\delta^2(t)}{dt}=4\left(\vec{PQ}(t-1)+(1-2t)\vec{PR}+t\vec{PS}\right)\cdot\left(\vec{PQ}(1-t)^2+2t(1-t)\vec{PR}+t^2\vec{PS}\right).$$ Expanding, you will find a third degree polynomial that has one or three real roots. Keep the roots in $(0,1)$, compute the corresponding distances, and find the smallest among them, plus the distances to the endpoints, $\vec{PQ}^2$ and $\vec{PS}^2$.