pointToLinePerpendicular static method
Computes the perpendicular distance from a point p to the (infinite) line containing the points AB
@param p the point to compute the distance for @param A one point of the line @param B another point of the line (must be different to A) @return the distance from p to line AB
Implementation
static double pointToLinePerpendicular(
Coordinate p, Coordinate A, Coordinate B) {
// use comp.graphics.algorithms Frequently Asked Questions method
/*
* (2) s = (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
* -----------------------------
* L^2
*
* Then the distance from C to P = |s|*L.
*/
double len2 = (B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y);
double s = ((A.y - p.y) * (B.x - A.x) - (A.x - p.x) * (B.y - A.y)) / len2;
return s.abs() * math.sqrt(len2);
}