Ejemplo n.º 1
0
        /// <summary>
        /// Find out if there is a intersection, if so return it
        /// </summary>
        /// <param name="ray">Ray</param>
        /// <returns>intersection</returns>
        public override Intersection GetIntersection(Ray ray)
        {
            Intersection pr = new Intersection();

            Vector normalFloor = normal;
            float  nDir        = Vector.DotProduct(normalFloor, ray.direction);

            if (Math.Abs(nDir) < 0.001)
            {
                return(null);
            }

            else
            {
                float nA = ((normalFloor.x * ray.startPoint.X) + (normalFloor.y * ray.startPoint.Y) + (normalFloor.z * ray.startPoint.Z));
                float t  = -((nA + koefD) / nDir);

                if (t > 0.001)
                {
                    Point  intersection = new Point(ray.startPoint.X + t * ray.direction.x, ray.startPoint.Y + t * ray.direction.y, ray.startPoint.Z + t * ray.direction.z);
                    Vector b            = new Vector(intersection.X - d.X, intersection.Y - d.Y, intersection.Z - d.Z);
                    float  L            = ((p.x / p.Magnitude() * b.x) + (p.y / p.Magnitude() * b.y) + (p.z / p.Magnitude() * b.z));
                    float  g            = ((q.x / q.Magnitude() * b.x) + (q.y / q.Magnitude() * b.y) + (q.z / q.Magnitude() * b.z));

                    float S  = L / p.Magnitude();
                    float S1 = g / q.Magnitude();
                    if (S <= 1 && S >= 0 && S1 <= 1 && S1 >= 0)
                    {
                        pr = InitializeIntersection(ray, t, intersection);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            return(pr);
        }