public static float DistanceBetweenPointAndLineSegment(ref Vector2 point, ref Vector2 lineEndPoint1, ref Vector2 lineEndPoint2) { Vector2 v = Vector2.Subtract(lineEndPoint2, lineEndPoint1); Vector2 w = Vector2.Subtract(point, lineEndPoint1); float c1 = Vector2.Dot(w, v); if (c1 <= 0) { return(DistanceBetweenPointAndPoint(ref point, ref lineEndPoint1)); } float c2 = Vector2.Dot(v, v); if (c2 <= c1) { return(DistanceBetweenPointAndPoint(ref point, ref lineEndPoint2)); } float b = c1 / c2; Vector2 pointOnLine = Vector2.Add(lineEndPoint1, Vector2.Multiply(v, b)); return(DistanceBetweenPointAndPoint(ref point, ref pointOnLine)); }
public static float DistanceBetweenPointAndPoint(Vector2 point1, Vector2 point2) { Vector2 v = Vector2.Subtract(point1, point2); return(v.Length()); }