From b054c94fa5479141cb18fd5436f1432343f4187b Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 27 Nov 2022 12:24:39 +0900 Subject: 6.2 --- src/main.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index a60526a..88df76a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -86,16 +86,16 @@ const Ray = struct { fn hitSphere(center: Point3, radius: f64, r: Ray) f64 { const oc = r.origin.sub(center); - const a = Vec3.dot(r.dir, r.dir); - const b = 2.0 * Vec3.dot(oc, r.dir); - const c = Vec3.dot(oc, oc) - radius * radius; - const discriminant = b * b - 4.0 * a * c; + const a = r.dir.normSquared(); + const half_b = Vec3.dot(oc, r.dir); + const c = oc.normSquared() - radius * radius; + const discriminant = half_b * half_b - a * c; if (discriminant < 0.0) { // r does not intersect the sphere. return -1.0; } else { // root of the equation. - return (-b - @sqrt(discriminant)) / (2.0 * a); + return (-half_b - @sqrt(discriminant)) / a; } } -- cgit v1.2.3-70-g09d2