ft: 6.2: simplified ray-sphere isct

This commit is contained in:
2026-04-15 13:36:24 +02:00
parent ebf39d014b
commit 3a660f900a

View File

@@ -21,16 +21,15 @@ impl Sphere {
impl Hittable for Sphere { impl Hittable for Sphere {
fn hit(&self, r: &crate::ray::Ray) -> Option<Hit> { fn hit(&self, r: &crate::ray::Ray) -> Option<Hit> {
let oc = self.center - r.origin(); let oc = self.center - r.origin();
let a = r.dir().dot(r.dir()); let a = r.dir().length_squared();
let b = -2. * r.dir().dot(&oc); let h = r.dir().dot(&oc);
let c = oc.dot(&oc) - self.radius * self.radius; let c = oc.length_squared() - self.radius * self.radius;
let d = h * h - a * c;
let d = b * b - 4. * a * c;
if d < 0. { if d < 0. {
None None
} else { } else {
let t = (-b - sqrt(d)) / (2.0 * a); let t = (h - sqrt(d)) / a;
let p = r.at(t); let p = r.at(t);
Some(Hit::new(t, p, self.normal_at(&p))) Some(Hit::new(t, p, self.normal_at(&p)))
} }