intersectsWithSphere method Null safety

double? intersectsWithSphere(
  1. 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.

Implementation

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;
}