function squaredSegmentDistance(x, y, x1, y1, x2, y2) {
  const dx = x2 - x1;
  const dy = y2 - y1;
  if (dx !== 0 || dy !== 0) {
    const t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy);
    if (t > 1) {
      x1 = x2;
      y1 = y2;
    } else if (t > 0) {
      x1 += dx * t;
      y1 += dy * t;
    }
  }
  return squaredDistance(x, y, x1, y1);
}
Returns the square of the closest distance between the point (x, y) and the
line segment (x1, y1) to (x2, y2).
@param {number} x X.
@param {number} y Y.
@param {number} x1 X1.
@param {number} y1 Y1.
@param {number} x2 X2.
@param {number} y2 Y2.
@return {number} Squared distance.