projectCoord method

Coordinate projectCoord(
  1. Coordinate p
)

Compute the projection of a point onto the line determined by this line segment.

Note that the projected point may lie outside the line segment. If this is the case, the projection factor will lie outside the range [0.0, 1.0].

Implementation

Coordinate projectCoord(Coordinate p) {
  if (p.equals(p0) || p.equals(p1)) return new Coordinate.fromCoordinate(p);

  double r = projectionFactor(p);
  Coordinate coord = new Coordinate.empty2D();
  coord.x = p0.x + r * (p1.x - p0.x);
  coord.y = p0.y + r * (p1.y - p0.y);
  return coord;
}