computeOffsetSegment method

void computeOffsetSegment(
  1. LineSegment seg,
  2. int side,
  3. double distance,
  4. LineSegment offset,
)

Compute an offset segment for an input segment on a given side and at a given distance. The offset points are computed in full double precision, for accuracy.

@param seg the segment to offset @param side the side of the segment ({@link Position}) the offset lies on @param distance the offset distance @param offset the points computed for the offset segment

Implementation

void computeOffsetSegment(
    LineSegment seg, int side, double distance, LineSegment offset) {
  int sideSign = side == Position.LEFT ? 1 : -1;
  double dx = seg.p1.x - seg.p0.x;
  double dy = seg.p1.y - seg.p0.y;
  double len = math.sqrt(dx * dx + dy * dy);
  // u is the vector that is the length of the offset, in the direction of the segment
  double ux = sideSign * distance * dx / len;
  double uy = sideSign * distance * dy / len;
  offset.p0.x = seg.p0.x - uy;
  offset.p0.y = seg.p0.y + ux;
  offset.p1.x = seg.p1.x - uy;
  offset.p1.y = seg.p1.y + ux;
}