GeometryUtils improvements (#709)
This commit is contained in:
@@ -131,13 +131,9 @@ public final class GeometryUtils {
|
||||
if (out == null)
|
||||
out = new float[2];
|
||||
|
||||
// Calculate center
|
||||
for (int i = 0; i < n; i += 2, pointPos += 2) {
|
||||
float x = points[pointPos];
|
||||
float y = points[pointPos + 1];
|
||||
|
||||
out[0] += x;
|
||||
out[1] += y;
|
||||
out[0] += points[pointPos];
|
||||
out[1] += points[pointPos + 1];
|
||||
}
|
||||
out[0] = out[0] * 2 / n;
|
||||
out[1] = out[1] * 2 / n;
|
||||
@@ -145,6 +141,21 @@ public final class GeometryUtils {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the closest point on a line.
|
||||
* See: https://en.wikipedia.org/wiki/Vector_projection#Vector_projection_2
|
||||
*
|
||||
* @param pP point
|
||||
* @param pL point of line
|
||||
* @param vL vector of line
|
||||
* @return the closest point on line
|
||||
*/
|
||||
public static float[] closestPointOnLine2D(float[] pP, float[] pL, float[] vL) {
|
||||
float[] vPL = diffVec(pL, pP);
|
||||
float[] vPS = diffVec(vPL, scale(vL, dotProduct(vPL, vL) / dotProduct(vL, vL)));
|
||||
return sumVec(pP, vPS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param a first vector
|
||||
* @param b second vector
|
||||
@@ -222,6 +233,9 @@ public final class GeometryUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the distance from a point to a line.
|
||||
* See: https://en.wikipedia.org/wiki/Vector_projection#Vector_projection_2
|
||||
*
|
||||
* @param pP point
|
||||
* @param pL point of line
|
||||
* @param vL vector of line
|
||||
@@ -229,7 +243,7 @@ public final class GeometryUtils {
|
||||
*/
|
||||
public static float distancePointLine2D(float[] pP, float[] pL, float[] vL) {
|
||||
float[] vPL = diffVec(pL, pP);
|
||||
float[] vPS = diffVec(vPL, scale(vL, dotProduct(vPL, vL)));
|
||||
float[] vPS = diffVec(vPL, scale(vL, dotProduct(vPL, vL) / dotProduct(vL, vL)));
|
||||
return (float) Math.sqrt(dotProduct(vPS, vPS));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user