closestPoint method

Coordinate closestPoint(
  1. Coordinate p
)

Computes the closest point on this line segment to another point. @param p the point to find the closest point to @return a Coordinate which is the closest point on the line segment to the point p

Implementation

Coordinate closestPoint(Coordinate p) {
  double factor = projectionFactor(p);
  if (factor > 0 && factor < 1) {
    return projectCoord(p);
  }
  double dist0 = p0.distance(p);
  double dist1 = p1.distance(p);
  if (dist0 < dist1) return p0;
  return p1;
}