a(point1),
b(point2) {}
- Scalar intersectRay(const Ray<2>& ray,
- Ray<2>::Intersection& intersection) const
+ bool intersectRay(const Ray<2>& ray, Ray<2>::Intersection& hit) const
{
// solve: Cx + r*Dx = Ax + s(Bx - Ax)
// Cy + r*Dy = Ay + s(By - Ay)
ray.direction[1] * (a[0] - b[0]);
// check if the ray and line are parallel
- if (isEqual(denom, SCALAR(0.0)))
+ //if (isEqual(denom, SCALAR(0.0)))
+ if (denom == SCALAR(0.0))
{
Scalar numer = a[0] * (ray.point[1] - b[1]) +
b[0] * (a[1] - ray.point[1]) +
// check if they are collinear
if (isEqual(numer, SCALAR(0.0)))
{
- intersection.point = ray.point;
- intersection.normal.set(0.0, 0.0);
- return SCALAR(0.0);
+ hit.distance = SCALAR(0.0);
+ hit.normal.set(0.0, 0.0);
+ return true;
}
- return SCALAR(-1.0);
+ return false;
}
Scalar s = (ray.direction[0] * (ray.point[1] - a[1]) +
ray.direction[1] * (a[0] - ray.point[0])) / denom;
// check if the ray hits the segment
- if (s < SCALAR(0.0) || s > SCALAR(1.0)) return SCALAR(-1.0);
+ if (s < SCALAR(0.0) || s > SCALAR(1.0)) return false;
- Scalar r = -(a[0] * (ray.point[1] - b[1]) +
- b[0] * (a[1] - ray.point[1]) +
- ray.point[0] * (b[1] - a[1])) / denom;
+ hit.distance = -(a[0] * (ray.point[1] - b[1]) +
+ b[0] * (a[1] - ray.point[1]) +
+ ray.point[0] * (b[1] - a[1])) / denom;
+ if (hit.distance < SCALAR(0.0)) return false;
- // make sure we're dealing with the right side of the ray
- if (r < SCALAR(0.0)) return SCALAR(-1.0);
-
- intersection.point = ray.point + r * ray.direction;
-
- // gotta use the correct normal
- Vector n = cml::perp(a - b);
- if (cml::dot(a - ray.point, n) < 0) intersection.normal = n;
- else intersection.normal = -n;
-
- return r;
+ Vector normal = cml::perp(a - b);
+ if (cml::dot(a - ray.point, normal) < 0) hit.normal = normal;
+ else hit.normal = -normal;
+ return true;
}
void draw(Scalar alpha = 0.0) const