project method

LineSegment? project(
  1. LineSegment seg
)

Project a line segment onto this line segment and return the resulting line segment. The returned line segment will be a subset of the target line line segment. This subset may be null, if the segments are oriented in such a way that there is no projection.

Note that the returned line may have zero length (i.e. the same endpoints). This can happen for instance if the lines are perpendicular to one another.

@param seg the line segment to project @return the projected line segment, or null if there is no overlap

Implementation

LineSegment? project(LineSegment seg) {
  double pf0 = projectionFactor(seg.p0);
  double pf1 = projectionFactor(seg.p1);
  // check if segment projects at all
  if (pf0 >= 1.0 && pf1 >= 1.0) return null;
  if (pf0 <= 0.0 && pf1 <= 0.0) return null;

  Coordinate newp0 = projectCoord(seg.p0);
  if (pf0 < 0.0) newp0 = p0;
  if (pf0 > 1.0) newp0 = p1;

  Coordinate newp1 = projectCoord(seg.p1);
  if (pf1 < 0.0) newp1 = p0;
  if (pf1 > 1.0) newp1 = p1;

  return new LineSegment.fromCoordinates(newp0, newp1);
}