diff --git a/src/geom/region.rs b/src/geom/region.rs index 8cf650f..32458b1 100644 --- a/src/geom/region.rs +++ b/src/geom/region.rs @@ -91,7 +91,15 @@ impl Region for Torus { // 3. Consider the distance from `p` to `q`. let rel_p = *p - *self.center; let p_on_plane = rel_p - self.normal.with_mag(self.normal.dot(rel_p)); - let q = p_on_plane.with_mag(self.major_rad.into_inner()); + let q = if p_on_plane == Vec3::zero() { + // avoid division by zero. + // The point is precisely on the axis of the torus. + // We can choose any point on the major radius to compare it to + // and they all give the same answer + Vec3::new(self.major_rad.into(), 0.0, 0.0) + } else { + p_on_plane.with_mag(self.major_rad.into_inner()) + }; let distance_to_circle = (rel_p - q).mag(); distance_to_circle < self.minor_rad.into_inner() }