From 0d7a04c58ddc19c95b92dd87f277e7baad9a2a9a Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Mon, 25 Jul 2022 19:18:28 +0200 Subject: [PATCH] Simplifying the Ray-Sphere Intersection --- rtiww/src/main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rtiww/src/main.cpp b/rtiww/src/main.cpp index a19ceaa..7895dae 100644 --- a/rtiww/src/main.cpp +++ b/rtiww/src/main.cpp @@ -7,14 +7,14 @@ double hit_sphere(const point3 ¢er, double radius, const ray &r) { vec3 oc = r.origin() - center; - auto a = dot(r.direction(), r.direction()); - auto b = 2.0 * dot(oc, r.direction()); - auto c = dot(oc, oc) - radius * radius; - auto discriminant = b * b - 4 * a * c; + auto a = r.direction().length_squared(); + auto half_b = dot(oc, r.direction()); + auto c = oc.length_squared() - radius * radius; + auto discriminant = half_b * half_b - a * c; if (discriminant < 0) { return -1.0; } else { - return (-b - sqrt(discriminant)) / (2.0*a); + return (-half_b - sqrt(discriminant)) / a; } }