def intersect(self, r: Ray): ray2 = r.transform(self.transform.inverse()) sphere_to_ray = ray2.origin - point(0, 0, 0) a = ray2.direction.dot(ray2.direction) b = 2 * ray2.direction.dot(sphere_to_ray) c = sphere_to_ray.dot(sphere_to_ray) - 1 delta = b * b - 4 * a * c if delta < 0: return [] t1 = (-b - sqrt(delta)) / (2 * a) t2 = (-b + sqrt(delta)) / (2 * a) i1 = intersections.Intersection(t1, self) i2 = intersections.Intersection(t2, self) return [i1, i2]
def test_scaling_ray(self): r = Ray(point(1, 2, 3), vector(0, 1, 0)) m = scaling(2, 3, 4) r2 = r.transform(m) self.assertEqual(point(2, 6, 12), r2.origin) self.assertEqual(vector(0, 3, 0), r2.direction)
def test_translating_ray(self): r = Ray(point(1, 2, 3), vector(0, 1, 0)) m = translation(3, 4, 5) r2 = r.transform(m) self.assertEqual(point(4, 6, 8), r2.origin) self.assertEqual(vector(0, 1, 0), r2.direction)