src/util/src/geometry.js
function findClosestPointsFromSegmentToSegment(closestA, closestB, segmentA, segmentB) {
  const {distA, distB} = findClosestPointsFromLineToLine(
      segmentA.start, segmentA.dir, segmentB.start, segmentB.dir);

  const isDistAInBounds = distA >= 0 && distA <= 1;
  const isDistBInBounds = distB >= 0 && distB <= 1;

  if (isDistAInBounds) {
    if (isDistBInBounds) {
      // The distances along both line segments are within bounds.
      vec3.scaleAndAdd(closestA, segmentA.start, segmentA.dir, distA);
      vec3.scaleAndAdd(closestB, segmentB.start, segmentB.dir, distB);
    } else {
      // Only the distance along the first line segment is within bounds.
      if (distB < 0) {
        vec3.copy(closestB, segmentB.start);
      } else {
        vec3.copy(closestB, segmentB.end);
      }
      findClosestPointOnSegmentToPoint(closestA, segmentA, closestB);
    }
  } else {
    if (isDistBInBounds) {
      // Only the distance along the second line segment is within bounds.
      if (distA < 0) {
        vec3.copy(closestA, segmentA.start);
      } else {
        vec3.copy(closestA, segmentA.end);
      }
      findClosestPointOnSegmentToPoint(closestB, segmentB, closestA);
    } else {
      // Neither of the distances along either line segment are within bounds.
      if (distA < 0) {
        vec3.copy(closestA, segmentA.start);
      } else {
        vec3.copy(closestA, segmentA.end);
      }

      if (distB < 0) {
        vec3.copy(closestB, segmentB.start);
      } else {
        vec3.copy(closestB, segmentB.end);
      }

      const altClosestA = vec3.create();
      const altClosestB = vec3.create();

      findClosestPointOnSegmentToPoint(altClosestA, segmentA, closestB);
      findClosestPointOnSegmentToPoint(altClosestB, segmentB, closestA);

      if (vec3.squaredDistance(altClosestA, closestB) <
          vec3.squaredDistance(altClosestB, closestA)) {
        vec3.copy(closestA, altClosestA);
      } else {
        vec3.copy(closestB, altClosestB);
      }
    }
  }
}
function findClosestPointsFromSegmentToSegment(closestA, closestB, segmentA, segmentB) {
  const {distA, distB} = findClosestPointsFromLineToLine(
      segmentA.start, segmentA.dir, segmentB.start, segmentB.dir);

  const isDistAInBounds = distA >= 0 && distA <= 1;
  const isDistBInBounds = distB >= 0 && distB <= 1;

  if (isDistAInBounds) {
    if (isDistBInBounds) {
      // The distances along both line segments are within bounds.
      vec3.scaleAndAdd(closestA, segmentA.start, segmentA.dir, distA);
      vec3.scaleAndAdd(closestB, segmentB.start, segmentB.dir, distB);
    } else {
      // Only the distance along the first line segment is within bounds.
      if (distB < 0) {
        vec3.copy(closestB, segmentB.start);
      } else {
        vec3.copy(closestB, segmentB.end);
      }
      findClosestPointOnSegmentToPoint(closestA, segmentA, closestB);
    }
  } else {
    if (isDistBInBounds) {
      // Only the distance along the second line segment is within bounds.
      if (distA < 0) {
        vec3.copy(closestA, segmentA.start);
      } else {
        vec3.copy(closestA, segmentA.end);
      }
      findClosestPointOnSegmentToPoint(closestB, segmentB, closestA);
    } else {
      // Neither of the distances along either line segment are within bounds.
      if (distA < 0) {
        vec3.copy(closestA, segmentA.start);
      } else {
        vec3.copy(closestA, segmentA.end);
      }

      if (distB < 0) {
        vec3.copy(closestB, segmentB.start);
      } else {
        vec3.copy(closestB, segmentB.end);
      }

      const altClosestA = vec3.create();
      const altClosestB = vec3.create();

      findClosestPointOnSegmentToPoint(altClosestA, segmentA, closestB);
      findClosestPointOnSegmentToPoint(altClosestB, segmentB, closestA);

      if (vec3.squaredDistance(altClosestA, closestB) <
          vec3.squaredDistance(altClosestB, closestA)) {
        vec3.copy(closestA, altClosestA);
      } else {
        vec3.copy(closestB, altClosestB);
      }
    }
  }
}
function findClosestPointsFromSegmentToSegment ( closestA , closestB , segmentA , segmentB ) { const { distA , distB } = findClosestPointsFromLineToLine ( segmentA . start , segmentA . dir , segmentB . start , segmentB . dir ) ; const isDistAInBounds = distA >= 0 && distA <= 1 ; const isDistBInBounds = distB >= 0 && distB <= 1 ; if ( isDistAInBounds ) { if ( isDistBInBounds ) { // The distances along both line segments are within bounds. vec3 . scaleAndAdd ( closestA , segmentA . start , segmentA . dir , distA ) ; vec3 . scaleAndAdd ( closestB , segmentB . start , segmentB . dir , distB ) ; } else { // Only the distance along the first line segment is within bounds. if ( distB < 0 ) { vec3 . copy ( closestB , segmentB . start ) ; } else { vec3 . copy ( closestB , segmentB . end ) ; } findClosestPointOnSegmentToPoint ( closestA , segmentA , closestB ) ; } } else { if ( isDistBInBounds ) { // Only the distance along the second line segment is within bounds. if ( distA < 0 ) { vec3 . copy ( closestA , segmentA . start ) ; } else { vec3 . copy ( closestA , segmentA . end ) ; } findClosestPointOnSegmentToPoint ( closestB , segmentB , closestA ) ; } else { // Neither of the distances along either line segment are within bounds. if ( distA < 0 ) { vec3 . copy ( closestA , segmentA . start ) ; } else { vec3 . copy ( closestA , segmentA . end ) ; } if ( distB < 0 ) { vec3 . copy ( closestB , segmentB . start ) ; } else { vec3 . copy ( closestB , segmentB . end ) ; } const altClosestA = vec3 . create ( ) ; const altClosestB = vec3 . create ( ) ; findClosestPointOnSegmentToPoint ( altClosestA , segmentA , closestB ) ; findClosestPointOnSegmentToPoint ( altClosestB , segmentB , closestA ) ; if ( vec3 . squaredDistance ( altClosestA , closestB ) < vec3 . squaredDistance ( altClosestB , closestA ) ) { vec3 . copy ( closestA , altClosestA ) ; } else { vec3 . copy ( closestB , altClosestB ) ; } } } }
Finds the closest position on one line segment to the other line segment, and vice versa.

----------------------------------------------------------------------------
Originally based on Jukka Jylänki's algorithm at
https://github.com/juj/MathGeoLib/blob/ff2d348a167008c831ae304483b824647f71fbf6/src/Geometry/LineSegment.cpp.

Copyright 2011 Jukka Jylänki

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------

@param {vec3} closestA Output param.
@param {vec3} closestB Output param.
@param {LineSegment} segmentA
@param {LineSegment} segmentB
Finds the closest position on one line segment to the other line segment and vice versa .