pointAlongSegmentByFraction static method

Coordinate pointAlongSegmentByFraction(
  1. Coordinate p0,
  2. Coordinate p1,
  3. double frac
)

Computes the {@link Coordinate} of a point a given fraction along the line segment (p0, p1). If the fraction is greater than 1.0 the last point of the segment is returned. If the fraction is less than or equal to 0.0 the first point of the segment is returned. The Z ordinate is interpolated from the Z-ordinates of the given points, if they are specified.

@param p0 the first point of the line segment @param p1 the last point of the line segment @param frac the length to the desired point @return the Coordinate of the desired point

Implementation

static Coordinate pointAlongSegmentByFraction(
    Coordinate p0, Coordinate p1, double frac) {
  if (frac <= 0.0) return p0;
  if (frac >= 1.0) return p1;

  double x = (p1.x - p0.x) * frac + p0.x;
  double y = (p1.y - p0.y) * frac + p0.y;
  // interpolate Z value. If either input Z is NaN, result z will be NaN as well.
  double z = (p1.getZ() - p0.getZ()) * frac + p0.getZ();
  return new Coordinate.fromXYZ(x, y, z);
}