public static PointF FindClosestPointOnSpine(PointF[] spine, PointF point) { if (spine == null || spine.Length <= 1) { return(PointF.Empty); } double minDist = double.MaxValue; PointF bestPoint = PointF.Empty; for (int i = 1; i < spine.Length; i++) { PointF previousPoint = spine[i - 1]; PointF currentPoint = spine[i]; PointF closestPoint; double currentMinDist = MathExtension.MinDistanceFromLineToPoint(previousPoint, currentPoint, point, out closestPoint); if (currentMinDist < minDist) { minDist = currentMinDist; bestPoint = closestPoint; } } return(bestPoint); }
public static double FindDistanceAlongSpine(PointF[] spine, PointF point) { if (spine == null || spine.Length <= 1) { return(-1); } double minDist = double.MaxValue; PointF bestPoint = PointF.Empty; PointF[] closestPoints = new PointF[2]; double totalDist = 0; for (int i = 1; i < spine.Length; i++) { PointF previousPoint = spine[i - 1]; PointF currentPoint = spine[i]; PointF closestPoint; double currentMinDist = MathExtension.MinDistanceFromLineToPoint(previousPoint, currentPoint, point, out closestPoint); if (currentMinDist < minDist) { minDist = currentMinDist; bestPoint = closestPoint; closestPoints[0] = previousPoint; closestPoints[1] = currentPoint; } } for (int i = 1; i < spine.Length; i++) { PointF previousPoint = spine[i - 1]; PointF currentPoint = spine[i]; if (previousPoint == closestPoints[0]) { //We're on the line totalDist += previousPoint.Distance(bestPoint); break; } else { //Not on this line totalDist += previousPoint.Distance(currentPoint); } } return(totalDist); }