pointAlongOffset method

Coordinate pointAlongOffset(
  1. double segmentLengthFraction,
  2. double offsetDistance
)

Computes the {@link Coordinate} that lies a given fraction along the line defined by this segment and offset from the segment by a given distance. A fraction of 0.0 offsets from the start point of the segment; a fraction of 1.0 offsets from the end point of the segment. The computed point is offset to the left of the line if the offset distance is positive, to the right if negative.

@param segmentLengthFraction the fraction of the segment length along the line @param offsetDistance the distance the point is offset from the segment (positive is to the left, negative is to the right) @return the point at that distance and offset

@throws IllegalStateException if the segment has zero length

Implementation

Coordinate pointAlongOffset(
    double segmentLengthFraction, double offsetDistance) {
  // the point on the segment line
  double segx = p0.x + segmentLengthFraction * (p1.x - p0.x);
  double segy = p0.y + segmentLengthFraction * (p1.y - p0.y);

  double dx = p1.x - p0.x;
  double dy = p1.y - p0.y;
  double len = math.sqrt(dx * dx + dy * dy);
  double ux = 0.0;
  double uy = 0.0;
  if (offsetDistance != 0.0) {
    if (len <= 0.0)
      throw new StateError(
          "Cannot compute offset from zero-length line segment");

    // u is the vector that is the length of the offset, in the direction of the segment
    ux = offsetDistance * dx / len;
    uy = offsetDistance * dy / len;
  }

  // the offset point is the seg point plus the offset vector rotated 90 degrees CCW
  double offsetx = segx - uy;
  double offsety = segy + ux;

  Coordinate coord = new Coordinate(offsetx, offsety);
  return coord;
}