public static void ClosestPtPointSegment2D(Vector2 c, Vector2 a, Vector2 b, ref float t, ref Vector2 d) { var vector = b - a; t = Vector2.Dot(c - a, vector); if (t <= 0f) { t = 0f; d = a; } else { var num = Vector2.Dot(vector, vector); if (t >= num) { t = 1f; d = b; } else { t /= num; d = a + (t * vector); } } }