code: public static Coordinate nearest(Coordinate c1, Coordinate c2, Coordinate c) {
  		double len = distance(c1, c2);
  
  		double u = (c.getX() - c1.getX()) * (c2.getX() - c1.getX()) + (c.getY() - c1.getY()) * (c2.getY() - c1.getY());
  		u = u / (len * len);
  
  		if (u < 0.00001 || u > 1) {
  			// Shortest point not within LineSegment, so take closest end-point.
  			double len1 = distance(c, c1);
  			double len2 = distance(c, c2);
  			if (len1 < len2) {
  				return c1;
  			}
  			return c2;
  		} else {
  			// Intersecting point is on the line, use the formula: P = P1 + u (P2 - P1)
  			double x1 = c1.getX() + u * (c2.getX() - c1.getX());
  			double y1 = c1.getY() + u * (c2.getY() - c1.getY());
  			return new Coordinate(x1, y1);
  		}
  	}
docs: Calculate which point on a line segment is nearest to the given coordinate. Will be perpendicular or one of the
  end-points.
  
  @param c1 First coordinate of the line segment.
  @param c2 Second coordinate of the line segment.
  @param c The coordinate to search the nearest point for.
  @return The point on the line segment nearest to the given coordinate.