Implements RayTracer::IPrimitive.
Definition at line 29 of file Cylinder.cpp.
30{
31 Math::Vector3D oc = ray.origin -
_center;
32
33 double a = ray.direction.x * ray.direction.x + ray.direction.z * ray.direction.z;
34 double b = 2.0 * (oc.
x * ray.direction.x + oc.
z * ray.direction.z);
36
37 double discriminant = b * b - 4 * a * c;
38 if (discriminant < 0)
39 return false;
40
41 double sqrtDisc = std::sqrt(discriminant);
42 double t0 = (-b - sqrtDisc) / (2 * a);
43 double t1 = (-b + sqrtDisc) / (2 * a);
44
45 if (t0 > t1)
46 std::swap(t0, t1);
47
48 double y0 = oc.
y + t0 * ray.direction.y;
49 double y1 = oc.
y + t1 * ray.direction.y;
50
53 return false;
54 t = t1;
55 } else {
56 t = t0;
57 }
58
59 hitPoint = ray.origin + ray.direction * t;
60
62 double length = std::sqrt(temp.x * temp.x + temp.y * temp.y + temp.z * temp.z);
63 if (length != 0)
64 normal = temp / length;
65 else
66 normal = temp;
67
68 return true;
69}
References _center, _height, _radius, RayTracer::Ray::direction, RayTracer::Ray::origin, Math::Point3D::x, Math::Vector3D::x, Math::Vector3D::y, Math::Point3D::z, and Math::Vector3D::z.