lyon icon indicating copy to clipboard operation
lyon copied to clipboard

Finding close point on CubicBezierSegment

Open andrewvarga opened this issue 2 years ago • 3 comments

I'm curious what's the best way to find the closest point on a bezier segment given a point. It seems there is no utility function for this currently. Eg. bezier.js has a method project which does this.

andrewvarga avatar Sep 25 '23 13:09 andrewvarga

Sounds like a useful addition to lyon_geom. I haven't looked into it yet so I don't have an answer to the question of the best way to do this, but bezier.js appears to do a rather simpler search through the t parameter of the curve.

nical avatar Sep 25 '23 13:09 nical

Huh, I just remembered I already implemented something for quadratic bézier segments: https://docs.rs/lyon_geom/latest/lyon_geom/struct.QuadraticBezierSegment.html#method.closest_point

It's actually an analytical solution. Certainly more difficult to solve for a higher degree but it might still be possible to use a similar approach. The insight is that the tangent at the closest point on the curve is necessarily perpendicular to the line going from the reference point to the closest point.

nical avatar Sep 25 '23 14:09 nical

Huh, I just remembered I already implemented something for quadratic bézier segments: https://docs.rs/lyon_geom/latest/lyon_geom/struct.QuadraticBezierSegment.html#method.closest_point

It's actually an analytical solution. Certainly more difficult to solve for a higher degree but it might still be possible to use a similar approach. The insight is that the tangent at the closest point on the curve is necessarily perpendicular to the line going from the reference point to the closest point.

Thank you, that sounds useful. So maybe I can estimate my cubic with a quadratic and use that, or implement something similar for cubic. At first look just searching through t doesn't seem too bad either, but this is certainly more elegant.

andrewvarga avatar Sep 26 '23 16:09 andrewvarga