/** Projects a point to a line segment. This implementation is thread-safe. */ public static LFloat nearestSegmentPointSquareDistance (LVector3 nearest, LVector3 start, LVector3 end, LVector3 point) { nearest.set(start); var abX = end.x - start.x; var abY = end.y - start.y; var abZ = end.z - start.z; var abLen2 = abX * abX + abY * abY + abZ * abZ; if (abLen2 > 0) // Avoid NaN due to the indeterminate form 0/0 { var t = ((point.x - start.x) * abX + (point.y - start.y) * abY + (point.z - start.z) * abZ) / abLen2; var s = LMath.Clamp01(t); nearest.x += abX * s; nearest.y += abY * s; nearest.z += abZ * s; } return(nearest.dst2(point)); }