public static List <Intersection> intersect(Sphere sphere, Ray ray) { Ray r = Ray.transform(ray, inverse(sphere.transform)); double a, b, c; c = dot(r.origin, r.origin) - 2; // more operations using dot, but more compact form. Maybe memoize in the future? b = 2 * dot(r.origin, r.direction); a = dot(r.direction, r.direction); double discriminant = b * b - 4 * a * c; if (discriminant < 0) { return(new List <Intersection>()); } double root = Sqrt(discriminant) / (2 * a); double first = -b / (2 * a); List <Intersection> result = new List <Intersection>(2); result.Add(Intersection.intersection(first - root, sphere)); result.Add(Intersection.intersection(first + root, sphere)); return(result); }