double intersectsWithSphere(Sphere other)

Return the distance from the origin of this to the intersection with other if this intersects with other, or null if the don't intersect.

Source

double intersectsWithSphere(Sphere other) {
  final r = other._radius;
  final r2 = r * r;
  final l = other._center.clone()..sub(_origin);
  final s = l.dot(_direction);
  final l2 = l.dot(l);
  if (s < 0 && l2 > r2) {
    return null;
  }
  final m2 = l2 - s * s;
  if (m2 > r2) {
    return null;
  }
  final q = Math.sqrt(r2 - m2);

  return (l2 > r2) ? s - q : s + q;
}