Example #1
0
        public void When_Calculating_Adjoint_Of_Matrix_Matrix_With_Result_Is_Returned()
        {
            //Arrange
            Matrix4 matrixOne = new Matrix4(1.0, 2.0, 0.0, 3.0, 3.0, 2.0, 1.0, 2.0, 1.0, 0.0, 1.0, 1.0, 2.0, 1.0, 3.0, 3.0);

            //Act
            Matrix4 result = matrixOne.Adjoint();

            //Assert
            Assert.AreEqual(-1.0, result.XX);
            Assert.AreEqual(3.0, result.XY);
            Assert.AreEqual(9.0, result.XZ);
            Assert.AreEqual(-4.0, result.XW);
            Assert.AreEqual(-1.0, result.YX);
            Assert.AreEqual(3.0, result.YY);
            Assert.AreEqual(-18.0, result.YZ);
            Assert.AreEqual(5.0, result.YW);
            Assert.AreEqual(-3.0, result.ZX);
            Assert.AreEqual(0.0, result.ZY);
            Assert.AreEqual(-9.0, result.ZZ);
            Assert.AreEqual(6.0, result.ZW);
            Assert.AreEqual(4.0, result.WX);
            Assert.AreEqual(-3.0, result.WY);
            Assert.AreEqual(9.0, result.WZ);
            Assert.AreEqual(-2.0, result.WW);
        }
Example #2
0
        public Vector3 Multiply(Matrix4 other)
        {
            double w = 1.0;

            return new Vector3
                (
                    ((x * other.XX) + (y * other.YX) + (z * other.ZX) + (w * other.WX)),
                    ((x * other.XY) + (y * other.YY) + (z * other.ZY) + (w * other.WY)),
                    ((x * other.XZ) + (y * other.YZ) + (z * other.ZZ) + (w * other.WZ))
                );
        }
Example #3
0
        public void When_Calculating_Determinant_Of_Matrix_Double_With_Result_Is_Returned()
        {
            //Arrange
            Matrix4 matrixOne = new Matrix4(1.0, 2.0, 0.0, 3.0, 3.0, 2.0, 1.0, 2.0, 1.0, 0.0, 1.0, 1.0, 2.0, 1.0, 3.0, 3.0);

            //Act
            double result = matrixOne.Determinant();

            //Assert
            Assert.AreEqual(9.0, result);
        }
Example #4
0
        public Matrix4 Multiply(Matrix4 other)
        {
            return new Matrix4
                (
                    ((xx * other.xx) + (xy * other.yx) + (xz * other.zx) + (xw * other.wx)),
                    ((xx * other.xy) + (xy * other.yy) + (xz * other.zy) + (xw * other.wy)),
                    ((xx * other.xz) + (xy * other.yz) + (xz * other.zz) + (xw * other.wz)),
                    ((xx * other.xw) + (xy * other.yw) + (xz * other.zw) + (xw * other.ww)),

                    ((yx * other.xx) + (yy * other.yx) + (yz * other.zx) + (yw * other.wx)),
                    ((yx * other.xy) + (yy * other.yy) + (yz * other.zy) + (yw * other.wy)),
                    ((yx * other.xz) + (yy * other.yz) + (yz * other.zz) + (yw * other.wz)),
                    ((yx * other.xw) + (yy * other.yw) + (yz * other.zw) + (yw * other.ww)),

                    ((zx * other.xx) + (zy * other.yx) + (zz * other.zx) + (zw * other.wx)),
                    ((zx * other.xy) + (zy * other.yy) + (zz * other.zy) + (zw * other.wy)),
                    ((zx * other.xz) + (zy * other.yz) + (zz * other.zz) + (zw * other.wz)),
                    ((zx * other.xw) + (zy * other.yw) + (zz * other.zw) + (zw * other.ww)),

                    ((wx * other.xx) + (wy * other.yx) + (wz * other.zx) + (ww * other.wx)),
                    ((wx * other.xy) + (wy * other.yy) + (wz * other.zy) + (ww * other.wy)),
                    ((wx * other.xz) + (wy * other.yz) + (wz * other.zz) + (ww * other.wz)),
                    ((wx * other.xw) + (wy * other.yw) + (wz * other.zw) + (ww * other.ww))
                );
        }
Example #5
0
        public Matrix4 Inverse()
        {
            Matrix4 matrixOne = new Matrix4(xx, xy, xz, xw, yx, yy, yz, yw, zx, zy, zz, zw, wx, wy, wz, ww);

            Matrix4 adjOne = matrixOne.Adjoint();

            double detOne = matrixOne.Determinant();

            return new Matrix4( adjOne.xx / detOne, adjOne.xy / detOne, adjOne.xz / detOne, adjOne.xw / detOne,
                                adjOne.yx / detOne, adjOne.yy / detOne, adjOne.yz / detOne, adjOne.yw / detOne,
                                adjOne.zx / detOne, adjOne.zy / detOne, adjOne.zz / detOne, adjOne.zw / detOne,
                                adjOne.wx / detOne, adjOne.wy / detOne, adjOne.wz / detOne, adjOne.ww / detOne);
        }
Example #6
0
        public void When_Calculating_Inverse_Of_Matrix_Matrix_With_Result_Is_Returned()
        {
            //Arrange
            Matrix4 matrixOne = new Matrix4(1.0, 2.0, 0.0, 3.0, 3.0, 2.0, 1.0, 2.0, 1.0, 0.0, 1.0, 1.0, 2.0, 1.0, 3.0, 3.0);

            //Act
            Matrix4 result = matrixOne.Inverse();

            //Assert
            Assert.AreEqual((-1.0/9.0), result.XX);
            Assert.AreEqual((1.0/3.0), result.XY);
            Assert.AreEqual(1.0, result.XZ);
            Assert.AreEqual((-4.0/9.0), result.XW);
            Assert.AreEqual((-1.0/9.0), result.YX);
            Assert.AreEqual((1.0/3.0), result.YY);
            Assert.AreEqual(-2.0, result.YZ);
            Assert.AreEqual((5.0/9.0), result.YW);
            Assert.AreEqual((-1.0/3.0), result.ZX);
            Assert.AreEqual(0.0, result.ZY);
            Assert.AreEqual(-1.0, result.ZZ);
            Assert.AreEqual((2.0/3.0), result.ZW);
            Assert.AreEqual((4.0/9.0), result.WX);
            Assert.AreEqual((-1.0/3.0), result.WY);
            Assert.AreEqual(1.0, result.WZ);
            Assert.AreEqual((-2.0/9.0), result.WW);
        }
Example #7
0
        public void When_Multiplying_Two_Matrices_Matrix_With_Result_Is_Returned()
        {
            //Arrange
            Matrix4 matrixOne = new Matrix4(1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 4.0, 1.0, 3.0, 4.0, 1.0, 2.0, 4.0, 1.0, 2.0, 3.0);
            Matrix4 matrixTwo = new Matrix4(1.0, 2.0, 5.0, 3.0, 3.0, 5.0, 4.0, 1.0, 1.0, 3.0, 2.0, 4.0, 2.0, 4.0, 1.0, 5.0);

            //Act
            Matrix4 result = matrixOne.Multiply(matrixTwo);

            //Assert
            Assert.AreEqual(18.0, result.XX);
            Assert.AreEqual(37.0, result.XY);
            Assert.AreEqual(23.0, result.XZ);
            Assert.AreEqual(37.0, result.XW);
            Assert.AreEqual(17.0, result.YX);
            Assert.AreEqual(35.0, result.YY);
            Assert.AreEqual(31.0, result.YZ);
            Assert.AreEqual(30.0, result.YW);
            Assert.AreEqual(20.0, result.ZX);
            Assert.AreEqual(37.0, result.ZY);
            Assert.AreEqual(35.0, result.ZZ);
            Assert.AreEqual(27.0, result.ZW);
            Assert.AreEqual(15.0, result.WX);
            Assert.AreEqual(31.0, result.WY);
            Assert.AreEqual(31.0, result.WZ);
            Assert.AreEqual(36.0, result.WW);
        }
Example #8
0
        public void When_Multiplying_A_Matrix_By_A_Scalar_Matrix_With_Result_Is_Returned()
        {
            //Arrange
            Matrix4 matrixOne = new Matrix4(1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 6.0, 4.0, 5.0, 6.0, 7.0);
            double scalarOne = 2.0;

            //Act
            Matrix4 result = matrixOne.Multiply(scalarOne);

            //Assert
            Assert.AreEqual(2.0, result.XX);
            Assert.AreEqual(4.0, result.XY);
            Assert.AreEqual(6.0, result.XZ);
            Assert.AreEqual(8.0, result.XW);
            Assert.AreEqual(4.0, result.YX);
            Assert.AreEqual(6.0, result.YY);
            Assert.AreEqual(8.0, result.YZ);
            Assert.AreEqual(10.0, result.YW);
            Assert.AreEqual(6.0, result.ZX);
            Assert.AreEqual(8.0, result.ZY);
            Assert.AreEqual(10.0, result.ZZ);
            Assert.AreEqual(12.0, result.ZW);
            Assert.AreEqual(8.0, result.WX);
            Assert.AreEqual(10.0, result.WY);
            Assert.AreEqual(12.0, result.WZ);
            Assert.AreEqual(14.0, result.WW);
        }
Example #9
0
        public void When_Vector_Multiplied_With_A_Matrix_Vector_With_Result_Is_Returned()
        {
            //Arrange
            Vector3 vectorOne = new Vector3(3.0, 2.0, 1.0);
            Matrix4 matrixOne = new Matrix4(1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 4.0, 1.0, 3.0, 4.0, 1.0, 2.0, 4.0, 1.0, 2.0, 3.0);

            //Act
            Vector3 result = vectorOne.Multiply(matrixOne);

            //Assert
            Assert.AreEqual(result.X, 14.0);
            Assert.AreEqual(result.Y, 17.0);
            Assert.AreEqual(result.Z, 20.0);
        }