src/util/src/geometry.js
function findClosestPointOnSegmentToPoint(closestPoint, segment, point) {
  const dirSquaredLength = vec3.squaredLength(segment.dir);

  if (!dirSquaredLength) {
    // The point is at the segment start.
    vec3.copy(closestPoint, segment.start);
  } else {
    // Calculate the projection of the point onto the line extending through the segment.
    vec3.subtract(_tmpVec1, point, segment.start);
    const t = vec3.dot(_tmpVec1, segment.dir) / dirSquaredLength;

    if (t < 0) {
      // The point projects beyond the segment start.
      vec3.copy(closestPoint, segment.start);
    } else if (t > 1) {
      // The point projects beyond the segment end.
      vec3.copy(closestPoint, segment.end);
    } else {
      // The point projects between the start and end of the segment.
      vec3.scaleAndAdd(closestPoint, segment.start, segment.dir, t);
    }
  }
}
function findClosestPointOnSegmentToPoint(closestPoint, segment, point) {
  const dirSquaredLength = vec3.squaredLength(segment.dir);

  if (!dirSquaredLength) {
    // The point is at the segment start.
    vec3.copy(closestPoint, segment.start);
  } else {
    // Calculate the projection of the point onto the line extending through the segment.
    vec3.subtract(_tmpVec1, point, segment.start);
    const t = vec3.dot(_tmpVec1, segment.dir) / dirSquaredLength;

    if (t < 0) {
      // The point projects beyond the segment start.
      vec3.copy(closestPoint, segment.start);
    } else if (t > 1) {
      // The point projects beyond the segment end.
      vec3.copy(closestPoint, segment.end);
    } else {
      // The point projects between the start and end of the segment.
      vec3.scaleAndAdd(closestPoint, segment.start, segment.dir, t);
    }
  }
}
function findClosestPointOnSegmentToPoint ( closestPoint , segment , point ) { const dirSquaredLength = vec3 . squaredLength ( segment . dir ) ; if ( ! dirSquaredLength ) { // The point is at the segment start. vec3 . copy ( closestPoint , segment . start ) ; } else { // Calculate the projection of the point onto the line extending through the segment. vec3 . subtract ( _tmpVec1 , point , segment . start ) ; const t = vec3 . dot ( _tmpVec1 , segment . dir ) / dirSquaredLength ; if ( t < 0 ) { // The point projects beyond the segment start. vec3 . copy ( closestPoint , segment . start ) ; } else if ( t > 1 ) { // The point projects beyond the segment end. vec3 . copy ( closestPoint , segment . end ) ; } else { // The point projects between the start and end of the segment. vec3 . scaleAndAdd ( closestPoint , segment . start , segment . dir , t ) ; } } }
Finds the closest position on a line segment to a point.

----------------------------------------------------------------------------
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} closestPoint Output param.
@param {LineSegment} segment
@param {vec3} point
@private
Finds the closest position on a line segment to a point .