Skip to content

Commit

Permalink
Simplifying the ray-sphere intersection code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBiscardi committed Aug 27, 2023
1 parent 9bec2b1 commit 843ceaa
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main.rs
Expand Up @@ -104,14 +104,14 @@ fn hit_sphere(
ray: &Ray,
) -> f64 {
let oc: DVec3 = ray.origin - *center;
let a = ray.direction.dot(ray.direction);
let b = 2.0 * oc.dot(ray.direction);
let c = oc.dot(oc) - radius * radius;
let discriminant = b * b - 4. * a * c;
let a = ray.direction.length_squared();
let half_b = oc.dot(ray.direction);
let c = oc.length_squared() - radius * radius;
let discriminant = half_b * half_b - a * c;

if discriminant < 0. {
-1.0
} else {
(-b - discriminant.sqrt()) / (2.0 * a)
(-half_b - discriminant.sqrt()) / a
}
}

0 comments on commit 843ceaa

Please sign in to comment.