]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Line.hh
stream-based logging classes
[chaz/yoink] / src / Moof / Line.hh
index 33eecb9b3df6f91dec483b8022011eac8cdda2a4..a88405effe417bf8d3329dc362e3ae64038f6826 100644 (file)
@@ -56,8 +56,7 @@ struct Line : public Drawable, public Shape<D>
                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)
@@ -71,7 +70,8 @@ struct Line : public Drawable, public Shape<D>
                                           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]) +
@@ -80,35 +80,29 @@ struct Line : public Drawable, public Shape<D>
                        // 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
This page took 0.022464 seconds and 4 git commands to generate.