From 34fc56bf82081a07e3ef02ce96c2949b3a0d1827 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 3 Jun 2020 23:29:29 +0200 Subject: [PATCH] Simplified ray intersection for spheres. --- wtracer.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/wtracer.cpp b/wtracer.cpp index 3783243..cd86a06 100644 --- a/wtracer.cpp +++ b/wtracer.cpp @@ -7,14 +7,14 @@ double hit_sphere(const Point3& center, 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 disciminant = 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 disciminant = half_b*half_b - a*c; if (disciminant < 0) { return -1.0; } else { - return (-b - std::sqrt(disciminant)) / (2.0 * a); + return (-half_b - std::sqrt(disciminant)) / a; } }