如何在一条曲线上,获取到距离指定点最近的点位置?

与上一篇 C# 曲线上的点(一) 获取指定横坐标对应的纵坐标值 类似,
我们通过曲线上获取的密集点,通过俩点之间连线,获取连线上最近的点。我们能够获取到一系列最近的点集,最近只取距离最小的点即可。

我们这样的算法是否精确呢?不算太精确,但是对于获取曲线上最近点,基本能满足。
斜率变化不大的线段,点不密集;斜率变化较大的线段,点相当密集,所以由此点集得到的最近点,是相对准确的。
实现方案,以下代码可以直接复用:
1 1 public static Point GetClosestPointOnPath(Point p, Geometry geometry) 2 2 { 3 3 PathGeometry pathGeometry = geometry.GetFlattenedPathGeometry(); 4 4 5 5 var points = pathGeometry.Figures.Select(f => GetClosestPointOnPathFigure(f, p)) 6 6 .OrderBy(t => t.Item2).FirstOrDefault(); 7 7 return points?.Item1 ?? new Point(0, 0); 8 8 } 9 9 1010 private static Tuple<Point, double> GetClosestPointOnPathFigure(PathFigure figure, Point p) 1111 { 1212 List<Tuple<Point, double>> closePoints = new List<Tuple<Point, double>>(); 1313 Point current = figure.StartPoint; 1414 foreach (PathSegment s in figure.Segments) 1515 { 1616 PolyLineSegment segment = s as PolyLineSegment; 1717 LineSegment line = s as LineSegment; 1818 Point[] points; 1919 if (segment != null) 2020 { 2121 points = segment.Points.ToArray(); 2222 } 2323 else if (line != null) 2424 { 2525 points = new[] { line.Point }; 2626 } 2727 else 2828 { 2929 throw new InvalidOperationException(); 3030 } 3131 foreach (Point next in points) 3232 { 3333 Point closestPoint = GetClosestPointOnLine(current, next, p); 3434 double d = (closestPoint - p).LengthSquared; 3535 closePoints.Add(new Tuple<Point, double>(closestPoint, d)); 3636 current = next; 3737 } 3838 } 3939 return closePoints.OrderBy(t => t.Item2).First(); 4040 }
俩点之间的连线,如果当前点在此方向的投影为负或者大于当前长度,则取俩侧的点:
1 1 private static Point GetClosestPointOnLine(Point start, Point end, Point p) 2 2 { 3 3 double length = (start - end).LengthSquared; 4 4 if (Math.Abs(length) < 0.01) 5 5 { 6 6 return start; 7 7 } 8 8 Vector v = end - start; 9 9 double param = (p - start) * v / length; 1010 return (param < 0.0) ? start : (param > 1.0) ? end : (start + param * v); 1111 }
效果图:
