ft: 6.2: simplified ray-sphere isct
This commit is contained in:
@@ -21,16 +21,15 @@ impl Sphere {
|
||||
impl Hittable for Sphere {
|
||||
fn hit(&self, r: &crate::ray::Ray) -> Option<Hit> {
|
||||
let oc = self.center - r.origin();
|
||||
let a = r.dir().dot(r.dir());
|
||||
let b = -2. * r.dir().dot(&oc);
|
||||
let c = oc.dot(&oc) - self.radius * self.radius;
|
||||
|
||||
let d = b * b - 4. * a * c;
|
||||
let a = r.dir().length_squared();
|
||||
let h = r.dir().dot(&oc);
|
||||
let c = oc.length_squared() - self.radius * self.radius;
|
||||
let d = h * h - a * c;
|
||||
|
||||
if d < 0. {
|
||||
None
|
||||
} else {
|
||||
let t = (-b - sqrt(d)) / (2.0 * a);
|
||||
let t = (h - sqrt(d)) / a;
|
||||
let p = r.at(t);
|
||||
Some(Hit::new(t, p, self.normal_at(&p)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user