Skip to content

Commit cec6dfa

Browse files
committed
Listing 14 Ray-sphere intersection code (after)
1 parent 85025c2 commit cec6dfa

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/InOneWeekend/main.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
double hit_sphere(const point3& center, double radius, const ray& r) {
88
vec3 oc = center - r.origin();
9-
auto a = dot(r.direction(), r.direction());
10-
auto b = -2.0 * dot(r.direction(), oc);
11-
auto c = dot(oc, oc) - radius*radius;
12-
auto discriminant = b*b - 4*a*c;
9+
auto a = r.direction().length_squared();
10+
auto h = dot(r.direction(), oc);
11+
auto c = oc.length_squared() - radius*radius;
12+
auto discriminant = h*h - a*c;
1313

1414
if (discriminant < 0) {
1515
return -1.0;
1616
} else {
17-
return (-b - sqrt(discriminant) ) / (2.0*a);
17+
return (h - sqrt(discriminant)) / a;
1818
}
1919
}
2020

0 commit comments

Comments
 (0)