I have a point and a path, polyline, or set of points to create lines.
How can I find the point on my path which closest to another disconnected point?
It's easy to store or transfer my path / polyline as any of the WPF geometry controls, but do any of these controls come with GetDistanceFrom
type methods? Is there any simply way to achieve this goal?
Here is the algorithm that I've implemented as a solution. There's nothing 'non-obvious' in here if you've spent more than ten minutes thinking about it.
I'll reference the distance algorithm which you can find here: https://stackoverflow.com/a/1501725/146077
The answer linked above uses a projection to test if the point is closest to either end of the interval than any other point. I've modified the function from that answer to return the point's position on that projection. Beware that this answer might not make any sense if you haven't read the linked answer!
The following method
GetClosestPointOnPath()
is a generalization of @KirkBroadhurst'sGetClosestPointOnLine()
method, i.e. it works with any path geometry, i.e. lines, curves, ellipses, etc.Here is a small sample program that demonstrates how to use this method:
MainWindow.xaml:
MainWindow.xaml.cs: