Beispiel #1
0
        protected override Intersection[] LocalIntersect(Ray ray)
        {
            if (!Bounds().Intersects(ray))
            {
                return new Intersection[] { }
            }
            ;
            var sphereToRay = ray.Origin - Center;
            var a           = PointType.DotProduct(ray.Direction, ray.Direction);
            var b           = 2 * PointType.DotProduct(ray.Direction, sphereToRay);
            var c           = PointType.DotProduct(sphereToRay, sphereToRay) - 1;

            var discriminant = Math.Pow(b, 2) - 4 * a * c;

            if (discriminant < 0)
            {
                return new Intersection[] { }
            }
            ;

            var t1 = (-b - Math.Sqrt(discriminant)) / (2 * a);
            var t2 = (-b + Math.Sqrt(discriminant)) / (2 * a);

            return(new Intersection[]
                   { new Intersection(t1, this), new Intersection(t2, this) });
        }
Beispiel #2
0
        public void DotProduct()
        {
            var v1       = PointType.Vector(1, 2, 3);
            var v2       = PointType.Vector(2, 3, 4);
            var expected = 20;

            Assert.Equal(expected, PointType.DotProduct(v1, v2));
        }