Exemplo n.º 1
0
    static void Main(string[] args)
    {
        double[] vector1 = { 1, 3 };
        double[] vector2 = { -2, 5 };

        Console.WriteLine($"{VectorToString(vector1)} * {VectorToString(vector2)}");
        Console.WriteLine($" = {VectorMath.DotProduct(vector1, vector2)}");

        double[] vector3 = { -4, 0, 10 };
        double[] vector4 = { 3, 7, -9 };

        Console.WriteLine($"{VectorToString(vector3)} * {VectorToString(vector4)}");
        Console.WriteLine($" = {VectorMath.DotProduct(vector3, vector4)}");

        Console.WriteLine($"{VectorToString(vector2)} * {VectorToString(vector4)}");
        Console.WriteLine($" = {(VectorMath.DotProduct(vector2, vector4))}");

        double[] vector5 = { 14, -2, 0, 2 };
        double[] vector6 = { -3, 23, 50, 9 };

        Console.WriteLine($"{VectorToString(vector5)} * {VectorToString(vector6)}");
        Console.WriteLine($" = {(VectorMath.DotProduct(vector5, vector6))}");

        double[] vector7 = { 14 };
        double[] vector8 = { -3 };

        Console.WriteLine($"{VectorToString(vector7)} * {VectorToString(vector8)}");
        Console.WriteLine($" = {(VectorMath.DotProduct(vector7, vector8))}");
    }
Exemplo n.º 2
0
        public override bool TryCalculateIntersection(Ray ray, out Intersection intersection)
        {
            intersection = new Intersection();

            Vector3f vecDirection        = ray.Direction;
            Vector3f rayToPlaneDirection = ray.Origin - this.Position;

            float D = VectorMath.DotProduct(this.normalDirection, vecDirection);
            float N = -VectorMath.DotProduct(this.normalDirection, rayToPlaneDirection);

            if (Math.Abs(D) <= .0005f)
            {
                return(false);
            }

            float sI = N / D;

            if (sI < 0 || sI > ray.Distance) // Behind or out of range
            {
                return(false);
            }

            var intersectionPoint = ray.Origin + (new Vector3f(sI) * vecDirection);
            var uv = this.GetUVCoordinate(intersectionPoint);

            var color = Material.GetDiffuseColorAtCoordinates(uv);

            intersection = new Intersection(intersectionPoint, this.normalDirection, ray.Direction, this, color, (ray.Origin - intersectionPoint).Magnitude());
            return(true);
        }
Exemplo n.º 3
0
    public static double[,] Multiply(double[,] matrix1, double[,] matrix2)
    {
        if (matrix1.GetLength(1) != (matrix2.GetLength(1)))
        {
            return new double[, ] {
                       { -1 }
            }
        }
        ;

        int rowSize = matrix1.GetLength(0);
        int colSize = matrix1.GetLength(1);
        var result  = new double[rowSize, colSize];

        for (int row = 0; row < rowSize; row++)
        {
            for (int col = 0; col < colSize; col++)
            {
                result[row, col] =
                    VectorMath.DotProduct(
                        GetRow(matrix1, (uint)row),
                        GetColumn(matrix2, (uint)col));
            }
        }

        return(result);
    }
Exemplo n.º 4
0
        private Ray GetReflectionRay(Vector3f origin, Vector3f normal, Vector3f impactDirection)
        {
            float    c1 = VectorMath.DotProduct(-normal, impactDirection);
            Vector3f reflectionDirection = impactDirection + (normal * new Vector3f(2 * c1));

            return(new Ray(origin + reflectionDirection * new Vector3f(.01f), reflectionDirection)); // Ensures the ray starts "just off" the reflected surface
        }
Exemplo n.º 5
0
    static void Main(string[] args)
    {
        double[] vector1     = { 2, 1, 2 };
        double[] vector2     = { 1, 3, 1 };
        double   dot_product = VectorMath.DotProduct(vector1, vector2);

        Console.WriteLine("Dot Product = {0}", dot_product);
    }
        public void DotProductReturnsDotProduct(double ax, double ay, double az, double bx, double by, double bz, double actualProduct)
        {
            Vector3 a = new Vector3(ax, ay, az);
            Vector3 b = new Vector3(bx, by, bz);
            double  computedProduct = VectorMath.DotProduct(a, b);

            Assert.Equal(actualProduct, computedProduct, 12);
        }
Exemplo n.º 7
0
    public static void Main(string[] args)
    {
        double[] vector1 = { -4, 0, 10 };
        double[] vector2 = { 3, 7, -9 };

        double result = VectorMath.DotProduct(vector1, vector2);

        Console.WriteLine("{0}", result);
    }
Exemplo n.º 8
0
    /// <summary>
    /// method that shears a square 2D matrix by a given shear factor and returns the resulting matrix.
    /// </summary>
    /// <param name="matrix">"matrix to shear"</param>
    /// <param name="direction">"x,y direction to shear in"</param>
    /// <param name="factor">"shear amount"</param>
    /// <returns>a matrix with shear applied</returns>
    public static double[,] Shear2D(double[,] matrix, char direction, double factor)
    {
        if (matrix.LongLength / 2 != 2)
        {
            return new double[, ] {
                       { -1 }
            }
        }
        ;

        else if (direction != 'x' && direction != 'y')
        {
            return new double[, ] {
                       { -1 }
            }
        }
        ;


        int dir = (int)direction - (int)'x';

        double[,] shear;
        double[,] s = new double[2, 2];


        if (dir == 0)
        {
            shear = new double[2, 2] {
                { 1, factor }, { 0, 1 }
            };

            for (int r1 = 0; r1 < 2; r1++)
            {
                for (int r2 = 0; r2 < 2; r2++)
                {
                    s[r1, r2] = VectorMath.DotProduct(GetRow(shear, (uint)r2), GetRow(matrix, (uint)r1));
                }
            }
        }
        else
        {
            shear = new double[2, 2] {
                { 1, 0 }, { factor, 1 }
            };
            for (int r1 = 0; r1 < 2; r1++)
            {
                for (int r2 = 0; r2 < 2; r2++)
                {
                    s[r1, r2] = VectorMath.DotProduct(GetRow(shear, (uint)r2), GetRow(matrix, (uint)r1));
                }
            }
        }

        return(s);
    }
Exemplo n.º 9
0
    static void Main(string[] args)
    {
        double[] vector1 = new double[3] {
            2, 2, -3
        };
        double[] vector2 = new double[3] {
            4, 1, -3
        };

        double result = VectorMath.DotProduct(vector1, vector2);

        Console.WriteLine("{0}", result);
    }
Exemplo n.º 10
0
        private Ray GetRefractionRay(Vector3f origin, Vector3f normal, Vector3f previousDirection, float refractivity)
        {
            float c1 = VectorMath.DotProduct(normal, previousDirection);
            float c2 = 1 - refractivity * refractivity * (1 - c1 * c1);

            if (c2 < 0)
            {
                c2 = (float)Math.Sqrt(c2);
            }
            Vector3f refractionDirection = (normal * new Vector3f((refractivity * c1 - c2)) - previousDirection * new Vector3f(refractivity)) * new Vector3f(-1);

            return(new Ray(origin, refractionDirection.Normalized())); // no refraction
        }
Exemplo n.º 11
0
    public static void Main()
    {
        double[] vector1 = new double[] { -4, 0, 10 };
        double[] vector2 = new double[] { 3, 7, -9 };
        double   dot     = 0;

        dot = VectorMath.DotProduct(vector1, vector2);
        Console.WriteLine($"Dot product is: {dot}");

        double[] vector3 = { -5.0, 1, 11, 15 };
        double[] vector4 = { -3.0, 10, 2, 17 };
        dot = VectorMath.DotProduct(vector3, vector4);
        Console.WriteLine($"It's no the same size or not 2D/3D vector => {dot}");
    }
Exemplo n.º 12
0
    static void Main(string[] args)
    {
        double[] vector  = { -4, 0, 10 };
        double[] vector1 = { 3, 7, -9 };
        double[] vector2 = { 1, 3 };
        double[] vector3 = { -2, 5 };
        double   res;
        double   res1;

        res  = VectorMath.DotProduct(vector, vector1);
        res1 = VectorMath.DotProduct(vector2, vector3);

        Console.WriteLine(res);
        Console.WriteLine(res1);
    }
Exemplo n.º 13
0
 //TODO: Test this. Pretty sure it works?
 public DoubleMatrix Multiply(DoubleMatrix other)
 {
     if (!CanMultiply(other))
     {
         throw new MultiplicationDimensionMismatchException();                      //todo
     }
     else
     {
         DoubleMatrix ret = new DoubleMatrix(RowSize, other.ColumnSize);
         for (int i = 0; i < RowSize; i++)
         {
             for (int j = 0; j < other.ColumnSize; j++)
             {
                 double[]           ro = GetRow(i).ToArray <double>();
                 double[]           co = other.GetColumn(j).ToArray <double>();
                 DoubleRowVector    v  = new DoubleRowVector(ro);
                 DoubleColumnVector w  = new DoubleColumnVector(co);
                 ret.Replace(i, j, VectorMath.DotProduct <double>(v, w));
             }
         }
         return(ret);
     }
 }
Exemplo n.º 14
0
        public override bool TryCalculateIntersection(Ray ray, out Intersection intersection)
        {
            intersection = new Intersection();

            Vector3f rayToSphere = ray.Origin - this.Position;
            float    B           = VectorMath.DotProduct(rayToSphere, ray.Direction);
            float    C           = VectorMath.DotProduct(rayToSphere, rayToSphere) - (Radius * Radius);
            float    D           = B * B - C;

            if (D > 0)
            {
                var          distance    = -B - (float)Math.Sqrt(D);
                var          hitPosition = ray.Origin + (ray.Direction * new Vector3f(distance));
                var          normal      = hitPosition - this.Position;
                UVCoordinate uv          = this.GetUVCoordinate(hitPosition);
                intersection = new Intersection(hitPosition, normal, ray.Direction, this, Material.GetDiffuseColorAtCoordinates(uv), distance);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 15
0
        public override UVCoordinate GetUVCoordinate(Vector3f position)
        {
            var uvPosition = this.Position + position;

            var uMag = VectorMath.DotProduct(uvPosition, uDirection);
            var u    = (new Vector3f(uMag) * uDirection).Magnitude();

            if (uMag < 0)
            {
                u += cellWidth / 2f;
            }
            u = (u % cellWidth) / cellWidth;

            var vMag = VectorMath.DotProduct(uvPosition, vDirection);
            var v    = (new Vector3f(vMag) * vDirection).Magnitude();

            if (vMag < 0)
            {
                v += cellWidth / 2f;
            }
            v = (v % cellWidth) / cellWidth;

            return(new UVCoordinate(u, v));
        }
Exemplo n.º 16
0
 static void Main(string[] args)
 {
     double[] vector1 = { -4, 0, 10 };
     double[] vector2 = { 3, 7, -9 };
     Console.WriteLine(VectorMath.DotProduct(vector1, vector2));
 }
Exemplo n.º 17
0
        static void Main(string[] args)
        {
            double product = VectorMath.DotProduct(new double[] { 3, 4 }, new double[] { 3, 4 });

            Console.WriteLine("Product is {0}", product);
        }
Exemplo n.º 18
0
 public static float Magnitude(this Vector3f v)
 {
     return((float)Math.Abs(Math.Sqrt(VectorMath.DotProduct(v, v))));
 }
Exemplo n.º 19
0
        public static Vector3f Projection(Vector3f projectedVector, Vector3f directionVector)
        {
            var mag = VectorMath.DotProduct(projectedVector, directionVector.Normalized());

            return(directionVector * mag);
        }
Exemplo n.º 20
0
 public RationalNumber DotProduct(IVector <RationalNumber> other)
 {
     return(VectorMath.DotProduct <RationalNumber>(this, other));
 }
Exemplo n.º 21
0
        /// <summary>
        /// Recursive algorithm base
        /// </summary>
        /// <param name="intersection">The intersection the recursive step started from</param>
        /// <param name="ray">The ray, starting from the intersection</param>
        /// <param name="scene">The scene to trace</param>
        private Color CalculateRecursiveColor(Intersection intersection, Scene scene, int depth)
        {
            // Ambient light:
            var color = Color.Lerp(Color.Black, intersection.Color * scene.AmbientLightColor, scene.AmbientLightIntensity);

            foreach (Light light in scene.Lights)
            {
                var lightContribution = new Color();
                var towardsLight      = (light.Position - intersection.Point).Normalized();
                var lightDistance     = Util.Distance(intersection.Point, light.Position);

                // Accumulate diffuse lighting:
                var lightEffectiveness = VectorMath.DotProduct(towardsLight, intersection.Normal);
                if (lightEffectiveness > 0.0f)
                {
                    lightContribution = lightContribution + (intersection.Color * light.GetIntensityAtDistance(lightDistance) * light.Color * lightEffectiveness);
                }

                // Render shadow
                var          shadowRay = new Ray(intersection.Point, towardsLight);
                Intersection shadowIntersection;
                if (TryCalculateIntersection(shadowRay, scene, intersection.ObjectHit, out shadowIntersection) && shadowIntersection.Distance < lightDistance)
                {
                    var transparency     = shadowIntersection.ObjectHit.Material.Transparency;
                    var lightPassThrough = Util.Lerp(.25f, 1.0f, transparency);
                    lightContribution = Color.Lerp(lightContribution, Color.Zero, 1 - lightPassThrough);
                }

                color += lightContribution;
            }

            if (depth < ReflectionDepth)
            {
                // Reflection ray
                var objectReflectivity = intersection.ObjectHit.Material.Reflectivity;
                if (objectReflectivity > 0.0f)
                {
                    var          reflectionRay = GetReflectionRay(intersection.Point, intersection.Normal, intersection.ImpactDirection);
                    Intersection reflectionIntersection;
                    if (TryCalculateIntersection(reflectionRay, scene, intersection.ObjectHit, out reflectionIntersection))
                    {
                        color = Color.Lerp(color, CalculateRecursiveColor(reflectionIntersection, scene, depth + 1), objectReflectivity);
                    }
                }

                // Refraction ray
                var objectRefractivity = intersection.ObjectHit.Material.Refractivity;
                if (objectRefractivity > 0.0f)
                {
                    var          refractionRay = GetRefractionRay(intersection.Point, intersection.Normal, intersection.ImpactDirection, objectRefractivity);
                    Intersection refractionIntersection;
                    if (TryCalculateIntersection(refractionRay, scene, intersection.ObjectHit, out refractionIntersection))
                    {
                        var refractedColor = CalculateRecursiveColor(refractionIntersection, scene, depth + 1);
                        color = Color.Lerp(color, refractedColor, 1 - (intersection.ObjectHit.Material.Opacity));
                    }
                }
            }

            color = color.Limited;
            return(color);
        }
Exemplo n.º 22
0
    static void Main(string[] args)
    {
        var s = VectorMath.DotProduct(new double[] { -4, 0, 10 }, new double[] { 3, 7, -9 });

        Console.WriteLine("{0}", s);
    }