package com.mygdx.game.utilities;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;

/**
 * @author jsjolund
 */
public class GeometryUtils {

	/**
	 * Projects a point to a line segment.
	 *
	 * @param nearest Output for the nearest vector to the segment.
	 * @param start Segment start point
	 * @param end Segment end point
	 * @param point Point to project from
	 * @return Squared distance between point and nearest.
	 */
	public static float nearestSegmentPointSquareDistance
	(Vector3 nearest, Vector3 start, Vector3 end, Vector3 point) {
		nearest.set(start);
		float abX = end.x - start.x;
		float abY = end.y - start.y;
		float abZ = end.z - start.z;
		float t = ((point.x - start.x) * abX + (point.y - start.y) * abY + (point.z - start.z) * abZ) /
				(abX * abX + abY * abY + abZ * abZ);
		float s = MathUtils.clamp(t, 0, 1);
		nearest.x += abX * s;
		nearest.y += abY * s;
		nearest.z += abZ * s;
		return nearest.dst2(point);
	}


}


@@ -683,16 +683,136 @@
 tart.z;%0A
+%09%09float abLen2 = abX * abX + abY * abY + abZ * abZ;%0A%09%09if (abLen2 %3E 0) %7B // Avoid NaN due to the indeterminate form 0/0%0A%09
 %09%09float 
@@ -904,50 +904,18 @@
 Z) /
-%0A%09%09%09%09(abX * abX + abY * abY + abZ * abZ)
+ abLen2
 ;%0A
+%09
 %09%09fl
@@ -946,24 +946,25 @@
 t, 0, 1);%0A%09%09
+%09
 nearest.x +=
@@ -969,24 +969,25 @@
 += abX * s;%0A
+%09
 %09%09nearest.y 
@@ -1000,16 +1000,17 @@
  * s;%0A%09%09
+%09
 nearest.
@@ -1023,16 +1023,20 @@
 bZ * s;%0A
+%09%09%7D%0A
 %09%09return

Avoid NaN due to the indeterminate form 0/0