Simplified ray intersection for spheres.

This commit is contained in:
Faerbit 2020-06-03 23:29:29 +02:00
parent 9e16d5f112
commit 34fc56bf82

View File

@ -7,14 +7,14 @@
double hit_sphere(const Point3& center, double radius, const Ray& r) { double hit_sphere(const Point3& center, double radius, const Ray& r) {
Vec3 oc = r.origin() - center; Vec3 oc = r.origin() - center;
auto a = dot(r.direction(), r.direction()); auto a = r.direction().length_squared();
auto b = 2.0 * dot(oc, r.direction()); auto half_b = dot(oc, r.direction());
auto c = dot(oc, oc) - radius * radius; auto c = oc.length_squared() - radius * radius;
auto disciminant = b*b - 4*a*c; auto disciminant = half_b*half_b - a*c;
if (disciminant < 0) { if (disciminant < 0) {
return -1.0; return -1.0;
} else { } else {
return (-b - std::sqrt(disciminant)) / (2.0 * a); return (-half_b - std::sqrt(disciminant)) / a;
} }
} }