closestPoints method

List<Coordinate> closestPoints(
  1. LineSegment line
)

Computes the closest points on two line segments.

@param line the segment to find the closest point to @return a pair of Coordinates which are the closest points on the line segments

Implementation

List<Coordinate> closestPoints(LineSegment line) {
  // test for intersection
  Coordinate? intPt = intersection(line);
  if (intPt != null) {
    return [intPt, intPt];
  }

  /**
   *  if no intersection closest pair contains at least one endpoint.
   * Test each endpoint in turn.
   */
  List<Coordinate> closestPt = []; //..length = 2;
  double minDistance = double.maxFinite;
  double dist;

  Coordinate close00 = closestPoint(line.p0);
  minDistance = close00.distance(line.p0);
  closestPt.add(close00);
  closestPt.add(line.p0);
  // closestPt[0] = close00;
  // closestPt[1] = line.p0;

  Coordinate close01 = closestPoint(line.p1);
  dist = close01.distance(line.p1);
  if (dist < minDistance) {
    minDistance = dist;
    closestPt[0] = close01;
    closestPt[1] = line.p1;
  }

  Coordinate close10 = line.closestPoint(p0);
  dist = close10.distance(p0);
  if (dist < minDistance) {
    minDistance = dist;
    closestPt[0] = p0;
    closestPt[1] = close10;
  }

  Coordinate close11 = line.closestPoint(p1);
  dist = close11.distance(p1);
  if (dist < minDistance) {
    minDistance = dist;
    closestPt[0] = p1;
    closestPt[1] = close11;
  }

  return closestPt;
}