From 6e3f92e47c7c352a40718ace3147329d036217f3 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 8 Sep 2013 18:28:53 -0700 Subject: [PATCH] Comment and formatting tweaks --- src/basics.c | 2 +- src/object.c | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/basics.c b/src/basics.c index 5b787ec..998fcbb 100644 --- a/src/basics.c +++ b/src/basics.c @@ -57,7 +57,7 @@ vector_mul_scalar(Vector3 v, float f) /* * vector_sub_vector -- * - * Subtract s from m. Return a new vector. + * m - s. Return a new vector. */ Vector3 vector_sub_vector(Vector3 m, Vector3 s) diff --git a/src/object.c b/src/object.c index dbc9572..fe5ae29 100644 --- a/src/object.c +++ b/src/object.c @@ -173,17 +173,17 @@ object_sphere_set_radius(Object *obj, float r) int sphere_does_intersect(Object *obj, Ray ray, float **t) { - // Distance from vector point to center of sphere. - Vector3 dist = vector_sub_vector(ray.location, object_get_location(obj)); + // Location of the vector in object space. + Vector3 ray_loc_obj = vector_sub_vector(ray.location, object_get_location(obj)); float r = object_sphere_get_radius(obj); // Coefficients for quadratic equation. float a = vector_dot(ray.direction, ray.direction); - float b = vector_dot(vector_mul_scalar(dist, 2), ray.direction); - float c = vector_dot(dist, dist) - (r * r); + float b = vector_dot(ray.direction, ray_loc_obj) * 2.0; + float c = vector_dot(ray_loc_obj, ray_loc_obj) - (r * r); - // Discriminant for quadratic equation. - float discrim = b * b - 4.0 * a * c; + // Discriminant for the quadratic equation. + float discrim = (b * b) - (4.0 * a * c); // If the discriminant is less than zero, there are no real (as in not imaginary) solutions to this intersection. if (discrim < 0) { @@ -192,7 +192,7 @@ sphere_does_intersect(Object *obj, Ray ray, float **t) /* * Compute most of the quadratic equation as q. Doing this first helps avoid precision errors when - * b =~ * sqrt(b^2 - 4ac). + * b =~ sqrt(b^2 - 4ac). * * See: http://wiki.cgsociety.org/index.php/Ray_Sphere_Intersection */ @@ -205,7 +205,7 @@ sphere_does_intersect(Object *obj, Ray ray, float **t) q = (-b + sqrt_discrim) / 2.0; } - // Compute the intersections. Spheres have at most two intersections. + // Compute the intersections, the roots of the quadratic equation. Spheres have at most two intersections. float t0 = q / a; float t1 = c / q;