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; } }