src/util/src/geometry.js
function findClosestPointsFromLineToLine(startA, dirA, startB, dirB) {
  vec3.subtract(_tmpVec1, startA, startB);
  const dirBDotDirAToB = vec3.dot(dirB, _tmpVec1);
  const dirADotDirAToB = vec3.dot(dirA, _tmpVec1);

  const sqrLenDirB = vec3.squaredLength(dirB);
  const sqrLenDirA = vec3.squaredLength(dirA);

  const dirADotDirB = vec3.dot(dirA, dirB);

  const denominator = sqrLenDirA * sqrLenDirB - dirADotDirB * dirADotDirB;

  const distA = denominator < EPSILON
      ? 0
      : (dirADotDirB * dirBDotDirAToB - sqrLenDirB * dirADotDirAToB) / denominator;
  const distB = (dirBDotDirAToB + dirADotDirB * distA) / sqrLenDirB;

  return {
    distA: distA,
    distB: distB
  };
}
function findClosestPointsFromLineToLine(startA, dirA, startB, dirB) {
  vec3.subtract(_tmpVec1, startA, startB);
  const dirBDotDirAToB = vec3.dot(dirB, _tmpVec1);
  const dirADotDirAToB = vec3.dot(dirA, _tmpVec1);

  const sqrLenDirB = vec3.squaredLength(dirB);
  const sqrLenDirA = vec3.squaredLength(dirA);

  const dirADotDirB = vec3.dot(dirA, dirB);

  const denominator = sqrLenDirA * sqrLenDirB - dirADotDirB * dirADotDirB;

  const distA = denominator < EPSILON
      ? 0
      : (dirADotDirB * dirBDotDirAToB - sqrLenDirB * dirADotDirAToB) / denominator;
  const distB = (dirBDotDirAToB + dirADotDirB * distA) / sqrLenDirB;

  return {
    distA: distA,
    distB: distB
  };
}
function findClosestPointsFromLineToLine ( startA , dirA , startB , dirB ) { vec3 . subtract ( _tmpVec1 , startA , startB ) ; const dirBDotDirAToB = vec3 . dot ( dirB , _tmpVec1 ) ; const dirADotDirAToB = vec3 . dot ( dirA , _tmpVec1 ) ; const sqrLenDirB = vec3 . squaredLength ( dirB ) ; const sqrLenDirA = vec3 . squaredLength ( dirA ) ; const dirADotDirB = vec3 . dot ( dirA , dirB ) ; const denominator = sqrLenDirA * sqrLenDirB - dirADotDirB * dirADotDirB ; const distA = denominator < EPSILON ? 0 : ( dirADotDirB * dirBDotDirAToB - sqrLenDirB * dirADotDirAToB ) / denominator ; const distB = ( dirBDotDirAToB + dirADotDirB * distA ) / sqrLenDirB ; return { distA : distA , distB : distB } ; }
Finds the closest position on one line to the other line, and vice versa.

The positions are represented as scalar-value distances from the "start" positions of each line.
These are scaled according to the given direction vectors.

----------------------------------------------------------------------------
Originally based on Jukka Jylänki's algorithm at
https://github.com/juj/MathGeoLib/blob/ff2d348a167008c831ae304483b824647f71fbf6/src/Geometry/Line.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} startA The start position of line A.
@param {vec3} dirA The (unnormalized) direction of line A. Cannot be zero.
@param {vec3} startB The start position of line B.
@param {vec3} dirB The (unnormalized) direction of line B. Cannot be zero.
@returns {{distA: Number, distB: Number}}
Finds the closest position on one line to the other line and vice versa .