Open behdad opened 9 years ago
yeah, it pretty much involves (for any curve order) finding all points that have a normal that pass through the off-curve point. I can probably write up a bit on iteratively finding those and then picking the result(s) that have minimal distance. For quadratic that solution can be found directly, cubic and higher will be a little more work although in terms of "what we do", pretty much identical
and interesting shortcut might be to arc-approximate the curve and then do a quick "point close to which arc?" and then us that to restrict the search. No idea if that'd be faster than LUT iteration, but perhaps worth testing?
There is a paper worth reading on this topic:
Improved Algebraic Algorithm On Point Projection For Bézier Curves Xiao-Diao Chen, Yin Zhou, Zhenyu Shu, Hua Su, Jean-Claude Paul
https://hal.archives-ouvertes.fr/inria-00518379/document
Basically it just discusses some details in the root finding approaches, and their contribution is an additional check to filter out / skip calculating some roots that can't be a minimum distance.
Improved Algebraic Algorithm On Point Projection For Bézier Curves
FWIW there's a C++ implementation here: https://github.com/nrtaylor/CubicSplineClosestPoint
For quadratics, it's solvable, eg:
http://blog.gludion.com/2009/08/distance-to-quadratic-bezier-curve.html
For cubics, perhaps introduce a couple approximations.