/// <summary>Finds the lowest distance of point P to a line segment defined by points A and B</summary> public static float distance_from_point_to_segment(crds2 a, crds2 b, crds2 p) { float l2 = a.dist_squared(b); if (l2 < 0.000001f) return a.dist(p); // case : a == b float t = crds2.dot(p-a, b-a) / l2; if (t < 0f) return a.dist(p); if (t > 1f) return b.dist(p); crds2 proj = a.lerp(b, t); return proj.dist(p); }
/// <summary>Returns the point of projection of point P on a segment defined by A and B, expressed as a float interpolation value between A and B</returns> public static float projection_of_point_on_segment(crds2 a, crds2 b, crds2 p) { return crds2.dot(p - a, b - a) / a.dist_squared(b); }