public static double distancePointLine(Coordinate p,Coordinate A,Coordinate B){
  if (A.x == B.x && A.y == B.y)   return p.distance(A);
  double len2=(B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y);
  double r=((p.x - A.x) * (B.x - A.x) + (p.y - A.y) * (B.y - A.y)) / len2;
  if (r <= 0.0)   return p.distance(A);
  if (r >= 1.0)   return p.distance(B);
  double s=((A.y - p.y) * (B.x - A.x) - (A.x - p.x) * (B.y - A.y)) / len2;
  return Math.abs(s) * Math.sqrt(len2);
}

public static double distancePointLine ( Coordinate p , Coordinate A , Coordinate B ) { if ( A . x == B . x && A . y == B . y ) return p . distance ( A ) ; double len2 = ( B . x - A . x ) * ( B . x - A . x ) + ( B . y - A . y ) * ( B . y - A . y ) ; double r = ( ( p . x - A . x ) * ( B . x - A . x ) + ( p . y - A . y ) * ( B . y - A . y ) ) / len2 ; if ( r <= 0.0 ) return p . distance ( A ) ; if ( r >= 1.0 ) return p . distance ( B ) ; double s = ( ( A . y - p . y ) * ( B . x - A . x ) - ( A . x - p . x ) * ( B . y - A . y ) ) / len2 ; return Math . abs ( s ) * Math . sqrt ( len2 ) ; }
computes the distance from a point p to a line segment ab note : non - robust !
