Ejemplo n.º 1
0
        public void ConstructorValuesAreAccessibleByIndexer()
        {
            Matrix2x2 matrix2x2;

            matrix2x2 = new Matrix2x2();

            for (int x = 0; x < matrix2x2.Columns; x++)
            {
                for (int y = 0; y < matrix2x2.Rows; y++)
                {
                    Assert.Equal(0, matrix2x2[x, y], Epsilon);
                }
            }

            double value = 33.33;
            matrix2x2 = new Matrix2x2(value);

            for (int x = 0; x < matrix2x2.Columns; x++)
            {
                for (int y = 0; y < matrix2x2.Rows; y++)
                {
                    Assert.Equal(value, matrix2x2[x, y], Epsilon);
                }
            }

            GenerateFilledMatrixWithValues(out matrix2x2);

            for (int y = 0; y < matrix2x2.Rows; y++)
            {
                for (int x = 0; x < matrix2x2.Columns; x++)
                {
                    Assert.Equal(y * matrix2x2.Columns + x, matrix2x2[x, y], Epsilon);
                }
            }
        }
Ejemplo n.º 2
0
    /// <summary>
    /// This function is called when the object becomes enabled and active
    /// </summary>
    //private void OnEnable()
    //{
    //    // Turn player controls on
    //    Controls.Debug.toggle2D.performed += Toggle2D_performed;
    //    Controls.Debug.toggle2D.Enable();
    //}

    /// <summary>
    /// Enable 2D mode
    /// </summary>
    /// <param name="obj">Input key</param>
    //private void Toggle2D_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
    //{
    //    toggle = !toggle;
    //    if (toggle)
    //    {
    //        offset = offset2D;
    //    }
    //    else
    //    {
    //        offset = offset3D;
    //    }
    //}

    /// <summary>
    /// This function is called when the behaviour becomes disabled or inactive
    /// </summary>
    //private void OnDisable()
    //{
    //    // Turn player controls off
    //    Controls.Debug.toggle2D.performed -= Toggle2D_performed;
    //    Controls.Debug.toggle2D.Disable();
    //}

    /// <summary>
    /// This function is called every fixed framerate frame, if the MonoBehaviour is enabled
    /// </summary>
    private void FixedUpdate()
    {
        // Get desired position
        Matrix2x2 forwardMatrix = new Matrix2x2
                                      (target.transform.forward.x,
                                      target.transform.right.x,
                                      target.transform.forward.z,
                                      target.transform.right.z);

        //Vector2 forward = new Vector2(transform.forward.x, transform.forward.z);
        Matrix2x2 movementMatrix = new Matrix2x2
                                       (offset.z * forwardMatrix.A,
                                       offset.x * forwardMatrix.B,
                                       offset.z * forwardMatrix.C,
                                       offset.x * forwardMatrix.D);
        Vector2 XZOffset        = new Vector2(movementMatrix.A, movementMatrix.C) + new Vector2(movementMatrix.B, movementMatrix.D);
        Vector3 finalOffset     = new Vector3(XZOffset.x, offset.y, XZOffset.y);
        Vector3 desiredPosition = target.position + finalOffset;

        // Move camera smoothly to position
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);

        transform.position = smoothedPosition;

        // Get camera to look at target
        transform.LookAt(target);
    }
Ejemplo n.º 3
0
        void GS(VS_GS[] input, TriangleStream <PS_IN> spriteStream)
        {
            PS_IN v = new PS_IN();
            VS_GS g = input[0];

            v.Color = g.Color;


            Matrix2x2 rot    = GetRotation(input[0].Rotation);
            Vector2   origin = g.Origin.XY;
            Vector2   pos    = g.Pos.XY;
            Vector2   size   = g.Size.XY;
            Vector2   uv     = g.UV.XY;
            Vector2   p;


            for (int i = 0; i < 4; i++)
            {
                p      = Mul(size * (spriteCorners[i] - origin), rot);
                p     += pos;
                v.Pos  = new Vector4(p, 0, 1);
                v.Pos  = Mul(v.Pos, wvp);
                v.UV.X = uv[uvTable[i].X];
                v.UV.Y = uv[uvTable[i].Y];
                v.UV.Z = g.ArraySlice;
                spriteStream.Append(v);
            }
        }
Ejemplo n.º 4
0
 public void Add(Matrix2x2 m2)
 {
     for (int i = 0; i < 4; i++)
     {
         data[i] += m2.data[i];
     }
 }
Ejemplo n.º 5
0
        internal static Matrix2x2 <T> Submatrix <T>(
            this Matrix3x3 <T> a,
            int dropRow,
            int dropCol) where T : IComparable <T>
        {
            var rows = Enumerable.Range(0, 3)
                       .Where(x => x != dropRow)
                       .ToArray();

            var cols = Enumerable.Range(0, 3)
                       .Where(x => x != dropCol)
                       .ToArray();

            var m = new Matrix2x2 <T>(default(T));

            for (var i = 0; i < 2; i++)
            {
                for (var j = 0; j < 2; j++)
                {
                    m[i, j] = a[rows[i], cols[j]];
                }
            }

            return(m);
        }
Ejemplo n.º 6
0
 public Matrices()
 {
     this.ToWorld       = Matrix2x3.Identity;
     this.ToBody        = Matrix2x3.Identity;
     this.ToWorldNormal = Matrix2x2.Identity;
     this.ToBodyNormal  = Matrix2x2.Identity;
 }
Ejemplo n.º 7
0
 public void Add(Matrix2x2 m2)
 {
     r1c1 += m2.r1c1;
     r1c2 += m2.r1c2;
     r2c1 += m2.r2c1;
     r2c2 += m2.r2c2;
 }
Ejemplo n.º 8
0
        public Matrix2x2 Minor(int x, int y)
        {
            var m  = new Matrix2x2();
            int nx = 0;

            for (int i = 0; i < 3; i++)
            {
                int ny = 0;
                if (i == x)
                {
                    continue;
                }
                for (int j = 0; j < 3; j++)
                {
                    if (j == y)
                    {
                        continue;
                    }
                    m[nx, ny] = this[i, j];
                    ny++;
                }
                nx++;
            }

            return(m);
        }
Ejemplo n.º 9
0
    //using the existing point objects, transform their positions and update their tooltips based on the input transformation matrix
    public void TransformPoints(Matrix2x2 transformation)
    {
        if (transformation == null)
        {
            Debug.LogError("Can't transform points with a null transformation matrix.");
            return;
        }

        int numPoints = listOfPoints.Length;

        for (int i = 0; i < numPoints; i++)
        {
            //transform the point
            float   newX             = transformation.a * listOfPoints[i].x + transformation.b * listOfPoints[i].y;
            float   newY             = transformation.c * listOfPoints[i].x + transformation.d * listOfPoints[i].y;
            Vector2 transformedPoint = new Vector2(newX, newY);

            transformedPointPositions[i] = transformedPoint;

            //find and move the object to the point
            Vector3 worldPosition = new Vector3(transformedPoint.x * cartesianToWorldScale[0], transformedPoint.y * cartesianToWorldScale[1], -1);

            pointObjects[i].transform.position = worldPosition;
        }

        //update line positions
        for (int i = 0; i < numPoints; i++)
        {
            RenderPoint renderPoint = pointObjects[i].GetComponent <RenderPoint>();
            renderPoint.UpdateLine();
        }

        AdjustZoom();
        UpdatePointTooltips();
    }
Ejemplo n.º 10
0
    //gives the manager an array of matrices to use with the cartesian render
    //it calculates the final cumulative transformation matrix
    public void SetMatrices(Matrix2x2[] matricesArray)
    {
        if (matricesArray == null)
        {
            Debug.LogError("Render manager can't accept null array of input matrices.");
            return;
        }

        transformationMatrices = matricesArray;

        //calculate final matrix
        if (transformationMatrices.Length == 0)
        {
            finalMatrix = Matrix2x2.IdentityMatrix;
        }
        else
        {
            finalMatrix = transformationMatrices[0];
            for (int i = 1; i < transformationMatrices.Length; i++)
            {
                Matrix2x2 currentMatrix = transformationMatrices[i];
                finalMatrix = currentMatrix.Multiply(finalMatrix);
            }
        }
    }
Ejemplo n.º 11
0
        public Matrix2x2d Add(Matrix2x2 other)
        {
            Matrix2x2d _other = ToMatrix2x2d(other);

            return(new Matrix2x2d(getM00() + _other.getM00(), getM01() + _other.getM01(),
                                  getM10() + _other.getM10(), getM11() + _other.getM11()));
        }
Ejemplo n.º 12
0
        public void MultiplyByIdentityReturnsEqualValue()
        {
            GenerateFilledMatrixWithValues(out Matrix2x2 value);
            Matrix2x2 result = value * Matrix2x2.Identity;

            Assert.Equal(value, result);
        }
Ejemplo n.º 13
0
 internal void Set(ref Matrix2x3 toWorld)
 {
     this.ToWorld = toWorld;
     Matrix2x3.Invert(ref toWorld, out ToBody);
     Matrix2x2.CreateNormal(ref toWorld, out ToWorldNormal);
     Matrix2x2.CreateNormal(ref ToBody, out ToBodyNormal);
 }
Ejemplo n.º 14
0
 private Matrices(Matrices copy)
 {
     this.ToWorld       = copy.ToWorld;
     this.ToBody        = copy.ToBody;
     this.ToWorldNormal = copy.ToWorldNormal;
     this.ToBodyNormal  = copy.ToBodyNormal;
 }
Ejemplo n.º 15
0
 internal BodyProxy(Body body1, Body body2, Matrix2x2 transformaiton)
 {
     this.body1 = body1;
     this.body2 = body2;
     this.transformation = transformaiton;
     this.node = new LinkedListNode<BodyProxy>(this);
 }
Ejemplo n.º 16
0
    public static void Main()
    {
        int n    = int.Parse(Console.ReadLine());
        var data = Enumerable.Repeat(0, n).Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()).Select(x => (x[0], x[1])).ToArray();
        int b    = int.Parse(Console.ReadLine());

        ModInt.SetMod(b);
        long digits = 0;
        var  pow10  = new ModInt[11];

        pow10[0] = 1;
        for (int i = 1; i < pow10.Length; i++)
        {
            pow10[i] = pow10[i - 1] * 10;
        }
        ModInt res = 0;

        foreach (var(a, L) in data.Reverse())
        {
            var       aDigit = a.ToString().Length;
            var       offset = Power(10, digits);
            Matrix2x2 mat    = new Matrix2x2()
            {
                aa = pow10[aDigit], ab = 1,
                ba = 0, bb = 1
            };
            var powed = Matrix2x2.Power(mat, L);
            res    += a * powed.ab * offset;
            digits += (long)aDigit * L;
        }
        Console.WriteLine(res);
    }
    /// <summary>
    /// 特異値分解
    /// </summary>
    /// <param name="u">Returns rotation matrix u</param>
    /// <param name="s">Returns sigma matrix</param>
    /// <param name="v">Returns rotation matrix v(not transposed)</param>
    public void SVD(ref Matrix2x2 u, ref Matrix2x2 s, ref Matrix2x2 v)
    {
        // 対角行列であった場合、特異値分解は単純に以下で与えられる。
        if (Mathf.Abs(this[1, 0] - this[0, 1]) < MATRIX_EPSILON && Mathf.Abs(this[1, 0]) < MATRIX_EPSILON)
        {
            u.SetValue(this[0, 0] < 0 ? -1 : 1, 0, 0, this[1, 1] < 0 ? -1 : 1);
            s.SetValue(Mathf.Abs(this[0, 0]), Mathf.Abs(this[1, 1]));
            v.LoadIdentity();
        }

        // 対角行列でない場合、A^T*Aを計算する。
        else
        {
            float i       = this[0, 0] * this[0, 0] + this[1, 0] * this[1, 0]; // 0列ベクトルの長さ(非ルート)
            float j       = this[0, 1] * this[0, 1] + this[1, 1] * this[1, 1]; // 1列ベクトルの長さ(非ルート)
            float i_dot_j = this[0, 0] * this[0, 1] + this[1, 0] * this[1, 1]; // 列ベクトルの内積

            // A^T*Aが対角行列であった場合
            if (Mathf.Abs(i_dot_j) < MATRIX_EPSILON)
            {
                // 特異値行列の対角要素の計算
                float s1 = Mathf.Sqrt(i);
                float s2 = Mathf.Abs(i - j) < MATRIX_EPSILON ? s1 : Mathf.Sqrt(j);

                u.SetValue(this[0, 0] / s1, this[0, 1] / s2, this[1, 0] / s1, this[1, 1] / s2);
                s.SetValue(s1, s2);
                v.LoadIdentity();
            }
            // A^T*Aが対角行列でない場合、固有値を求めるために二次方程式を解く。
            else
            {
                // 固有値/固有ベクトルの算出
                float i_minus_j = i - j;    // 列ベクトルの長さの差
                float i_plus_j  = i + j;    // 列ベクトルの長さの和

                // 2次方程式の解の公式
                float root = Mathf.Sqrt(i_minus_j * i_minus_j + 4 * i_dot_j * i_dot_j);
                float eig  = (i_plus_j + root) * 0.5f;
                float s1   = Mathf.Sqrt(eig);
                float s2   = Mathf.Abs(root) < MATRIX_EPSILON ? s1 : Mathf.Sqrt((i_plus_j - root) / 2);

                s.SetValue(s1, s2);

                // A^T*Aの固有ベクトルをVとして用いる。
                float v_s = eig - i;
                float len = Mathf.Sqrt(v_s * v_s + i_dot_j * i_dot_j);
                i_dot_j /= len;
                v_s     /= len;
                v.SetValue(i_dot_j, -v_s, v_s, i_dot_j);

                // vとsが算出済みなので、回転行列uをAv/sで算出
                u.SetValue(
                    (this[0, 0] * i_dot_j + this[0, 1] * v_s) / s1,
                    (this[0, 1] * i_dot_j - this[0, 0] * v_s) / s2,
                    (this[1, 0] * i_dot_j + this[1, 1] * v_s) / s1,
                    (this[1, 1] * i_dot_j - this[1, 0] * v_s) / s2
                    );
            }
        }
    }
Ejemplo n.º 18
0
 /// <summary>
 /// Gets the mass matrix of the revolute limit.
 /// The revolute limit is special; in terms of solving, it is
 /// actually sometimes TWO constraints; a minimum plane, and a
 /// maximum plane.  The M11 field represents the minimum plane mass matrix
 /// and the M22 field represents the maximum plane mass matrix.
 /// </summary>
 /// <param name="massMatrix">Mass matrix of the constraint.</param>
 public void GetMassMatrix(out Matrix2x2 massMatrix)
 {
     massMatrix.M11 = velocityToImpulse.X;
     massMatrix.M22 = velocityToImpulse.Y;
     massMatrix.M12 = 0;
     massMatrix.M21 = 0;
 }
Ejemplo n.º 19
0
        public void Transform()
        {
            Matrix2x2 matrix2x2 = Matrix2x2.FromRotation(MathHelper.Pi);

            Assert.AreEqual(matrix2x2 * Vector2D.XAxis, Vector2D.Transform(matrix2x2, Vector2D.XAxis), "matrix2x2 rotation");
            UnitHelper.RefOperationTesterRightSame <Matrix2x2, Vector2D>(matrix2x2, Vector2D.XAxis, matrix2x2 * Vector2D.XAxis, Vector2D.Transform, "matrix2x2 rotation");
            Matrix3x3 matrix3x3 = Matrix3x3.FromTranslate2D(-op1);

            Assert.AreEqual(Vector2D.Zero, matrix3x3 * op1, "matrix3x3 Translate1");
            Assert.AreEqual((matrix3x3 * op1), Vector2D.Transform(matrix3x3, op1), "matrix3x3 Translate2");
            UnitHelper.RefOperationTesterRightSame <Matrix3x3, Vector2D>(matrix3x3, op1, matrix3x3 * op1, Vector2D.Transform, "matrix3x3 Translate");
            matrix3x3 = Matrix3x3.FromRotationZ(MathHelper.Pi);
            Assert.AreEqual((matrix3x3 * op1), Vector2D.Transform(matrix3x3, op1), "matrix3x3 rotation");
            UnitHelper.RefOperationTesterRightSame <Matrix3x3, Vector2D>(matrix3x3, op1, matrix3x3 * op1, Vector2D.Transform, "matrix3x3 rotation");


            Matrix2x2 matrix2x2Inv = matrix2x2.Inverted;
            Vector2D  temp         = matrix2x2 * op1;

            temp = matrix2x2Inv * temp;
            Assert.AreEqual(op1, temp, "inv2x2");

            matrix3x3 = matrix3x3 * Matrix3x3.FromTranslate2D(-op1);
            Matrix3x3 matrix3x3Inv = matrix3x3.Inverted;

            temp = matrix3x3 * op1;
            temp = matrix3x3Inv * temp;
            Assert.AreEqual(op1, temp, "inv3x3");
        }
Ejemplo n.º 20
0
        void Run(Mat src1, Mat src2)
        {
            IList <DMatch> matches;
            MatOfKeyPoint  keypoints1 = new MatOfKeyPoint(), keypoints2 = new MatOfKeyPoint();

            if (OutLookAR.Utils.ORBMatcher(src1, src2, keypoints1, keypoints2, out matches) == 0)
            {
                IList <DMatch> lowPassFilterMatches = matches.LowPassFilter();
                List <Point>   matchPoint1L         = new List <Point>();
                List <Point>   matchPoint2L         = new List <Point>();
                foreach (DMatch match in lowPassFilterMatches)
                {
                    matchPoint1L.Add(keypoints1.toArray()[match.queryIdx].pt);
                    matchPoint2L.Add(keypoints2.toArray()[match.trainIdx].pt);
                }
                if (matchPoint1L.Count > 0)
                {
                    ARTransform ARtorans = _lMedS.findARTransform(matchPoint2L.ToArray(), matchPoint1L.ToArray());

                    Debug.Log(string.Format("matchPoint1L.Count = {0} ,ARtorans.Error = {1}", matchPoint1L.Count, ARtorans.Error));

                    if (ARtorans.Error < filter)
                    {
                        Vector2   TTheta  = new Vector2(HorizontalAngle * ARtorans.Tx / Width, VerticalAngle * ARtorans.Ty / Height);
                        Matrix2x2 R       = new Matrix2x2((float)Math.Cos(-(double)ARtorans.Radian), -(float)Math.Sin(-(double)ARtorans.Radian), (float)Math.Sin(-(double)ARtorans.Radian), (float)Math.Cos(-(double)ARtorans.Radian));
                        Vector2   TRTheta = R * TTheta;
                        Debug.Log(R + " * " + TTheta + " = " + TRTheta);
                        float LthetaX = TRTheta.x,
                              LthetaY = TRTheta.y;
                        CameraObject.transform.localRotation = Quaternion.Euler(-LthetaY, -LthetaX, ARtorans.Radian.Degree());
                        Debug.Log(ARtorans);
                    }
                }
            }
        }
Ejemplo n.º 21
0
        private Func <double, double, double> GetGrad(int i, int j, Point a, Point b, Point c)
        {
            var J = new Matrix2x2(
                a.X - c.X, a.Y - c.Y,
                b.X - c.X, b.Y - c.Y);

            var JT = J.Transpose();

            var JJT    = J * JT;
            var JJTinv = JJT.Inverse();

            return((double ksi, double etta) =>
            {
                double[] gradi = new double[2] {
                    psiDers["ksi"][i](ksi, etta), psiDers["etta"][i](ksi, etta)
                };
                double[] gradj = new double[2] {
                    psiDers["ksi"][j](ksi, etta), psiDers["etta"][j](ksi, etta)
                };

                double[] v = gradi * JJTinv;
                return v[0] * gradj[0] + v[1] * gradj[1];
                //return
                //(psiDers["ksi"][i](ksi, etta) * JJTinv.a11 + psiDers["etta"][i](ksi, etta) * JJTinv.a21) * psiDers["ksi"][j](ksi, etta) +
                //(psiDers["ksi"][i](ksi, etta) * JJTinv.a12 + psiDers["etta"][i](ksi, etta) * JJTinv.a22) * psiDers["etta"][j](ksi, etta);
            });

            //Func<double, double, double> ksiDer = (double ksi, double etta) => psiDers["ksi"][i](ksi, etta) * psiDers["ksi"][j](ksi, etta);
            //Func<double, double, double> ettaDer = (double ksi, double etta) => psiDers["etta"][i](ksi, etta) * psiDers["etta"][j](ksi, etta);

            //return (double ksi, double etta) => ksiDer(ksi, etta) + ettaDer(ksi, etta);
        }
Ejemplo n.º 22
0
        public Matrix2x2f Sub(Matrix2x2 other)
        {
            Matrix2x2f _other = ToMatrix2x2f(other);

            return(new Matrix2x2f(getM00() - _other.getM00(), getM01() - _other.getM01(),
                                  getM10() - _other.getM10(), getM11() - _other.getM11()));
        }
Ejemplo n.º 23
0
 internal BodyProxy(Body body1, Body body2, Matrix2x2 transformaiton)
 {
     this.body1          = body1;
     this.body2          = body2;
     this.transformation = transformaiton;
     this.node           = new LinkedListNode <BodyProxy>(this);
 }
Ejemplo n.º 24
0
    /// <summary>
    /// Compares the answer <see cref="Matrix2x2"/> provided by the Player with the <see cref="solutionMatrix"/>.
    /// <para>
    ///     The <see cref="ResultText"/> will display whether or not the Player got the right answer.
    ///     This information will be recorded in the Google Sheets for Unity system (GSFU) via <see cref="CloudConnectorCore"/>.
    /// </para>
    /// <para>
    ///     Additional information can be sent to the <see cref="MatrixLogger"/> as well.
    /// </para>
    /// </summary>
    /// <param name="answerMatrix">The final answer the User submitted.</param>
    public void CheckAnswer(Matrix2x2 answerMatrix)
    {
        bool isCorrect = Matrix2x2.IsEqual(solutionMatrix, answerMatrix);

        if (isCorrect)
        {
            ResultText.text = "Correct!";
            MatrixLogger.Add("Correct! The answer was:\n" + solutionMatrix.ToString());
            SubmissionResultPanel.SetActive(true);

            answertime = Time.timeSinceLevelLoad;
            CloudConnectorCore.UpdateObjects("playerInfo", "name", Playername, q, answertime.ToString(), true);
            StopCoroutine("RemoveResultPanelAfterSomeSeconds");
            StartCoroutine("RemoveResultPanelAfterSomeSeconds");
        }
        else
        {
            MatrixLogger.Add("Incorrect! Your answer was:\n" + answerMatrix.ToString());

            ResultText.text = "Incorrect...";
            SubmissionResultPanel.SetActive(true);

            StopCoroutine("RemoveResultPanelAfterSomeSeconds");
            StartCoroutine("RemoveResultPanelAfterSomeSeconds");
        }
    }
Ejemplo n.º 25
0
        private unsafe void ProcessDelayStereo(ref DelayState state, Span <IntPtr> outputBuffers, ReadOnlySpan <IntPtr> inputBuffers, uint sampleCount)
        {
            const ushort channelCount = 2;

            float delayFeedbackBaseGain  = state.DelayFeedbackBaseGain;
            float delayFeedbackCrossGain = state.DelayFeedbackCrossGain;
            float inGain  = FixedPointHelper.ToFloat(Parameter.InGain, FixedPointPrecision);
            float dryGain = FixedPointHelper.ToFloat(Parameter.DryGain, FixedPointPrecision);
            float outGain = FixedPointHelper.ToFloat(Parameter.OutGain, FixedPointPrecision);

            Matrix2x2 delayFeedback = new Matrix2x2(delayFeedbackBaseGain, delayFeedbackCrossGain,
                                                    delayFeedbackCrossGain, delayFeedbackBaseGain);

            for (int i = 0; i < sampleCount; i++)
            {
                Vector2 channelInput = new Vector2
                {
                    X = *((float *)inputBuffers[0] + i) * 64,
                    Y = *((float *)inputBuffers[1] + i) * 64,
                };

                Vector2 delayLineValues = new Vector2()
                {
                    X = state.DelayLines[0].Read(),
                    Y = state.DelayLines[1].Read(),
                };

                Vector2 temp = MatrixHelper.Transform(ref channelInput, ref delayFeedback) + channelInput * inGain;

                state.UpdateLowPassFilter(ref Unsafe.As <Vector2, float>(ref temp), channelCount);

                *((float *)outputBuffers[0] + i) = (channelInput.X * dryGain + delayLineValues.X * outGain) / 64;
                *((float *)outputBuffers[1] + i) = (channelInput.Y * dryGain + delayLineValues.Y * outGain) / 64;
            }
        }
Ejemplo n.º 26
0
    static void Main()
    {
        const int MAX = 2;

        Matrix2x2[] matrix = new Matrix2x2[MAX];

        for (int i = 0; i < MAX; i++)
        {
            Console.WriteLine("Matrix2x2 nº" + (i + 1) + ":");
            Console.Write("Data row 1, 1: ");
            double d00 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Data row 1, 2: ");
            double d01 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Data row 2, 1: ");
            double d10 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Data row 2, 2: ");
            double d11 = Convert.ToDouble(Console.ReadLine());

            matrix[i] = new Matrix2x2(d00, d01, d10, d11);
        }

        matrix[1].Multiply(2);
        Matrix2x2 sum = matrix[0] + matrix[1];

        for (int i = 0; i < MAX; i++)
        {
            matrix[i].Display();
            Console.WriteLine("----------------------------");
        }
        Console.WriteLine(sum.ToString());
    }
Ejemplo n.º 27
0
    public static Vector Multiply(Matrix2x2 a, Vector b)
    {
        b.X = a.row0 * b.X;
        b.Y = a.row1 * b.Y;

        return(b);
    }
Ejemplo n.º 28
0
        public void Write(Matrix2x2 value)
        {
            Write(value.m00);
            Write(value.m01);

            Write(value.m10);
            Write(value.m11);
        }
Ejemplo n.º 29
0
 public static Vector2 Transform(ref Vector2 value1, ref Matrix2x2 value2)
 {
     return(new Vector2
     {
         X = value2.M11 * value1.X + value2.M12 * value1.Y,
         Y = value2.M21 * value1.X + value2.M22 * value1.Y,
     });
 }
Ejemplo n.º 30
0
        /// <summary>
        /// Multiplies the two matrices.
        /// </summary>
        /// <param name="a">First matrix to multiply.</param>
        /// <param name="b">Second matrix to multiply.</param>
        /// <param name="result">Product of the multiplication.</param>
        public static void Multiply(ref Matrix2x3 a, ref Matrix3x2 b, out Matrix2x2 result)
        {
            result.M11 = a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31;
            result.M12 = a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32;

            result.M21 = a.M21 * b.M11 + a.M22 * b.M21 + a.M23 * b.M31;
            result.M22 = a.M21 * b.M12 + a.M22 * b.M22 + a.M23 * b.M32;
        }
Ejemplo n.º 31
0
 /// <summary>
 /// Default constructor used during file parsing.
 /// </summary>
 /// <param name="stf">The STFreader containing the file stream</param>
 public LightTexture(STFReader stf)
 {
     stf.MustMatchBlockStart();
     Name        = stf.ReadString().ToLowerInvariant();
     TextureFile = stf.ReadString();
     uv          = new Matrix2x2(stf.ReadFloat(null), stf.ReadFloat(null), stf.ReadFloat(null), stf.ReadFloat(null));
     stf.SkipRestOfBlock();
 }
Ejemplo n.º 32
0
        /// <summary>
        /// Constructs a uniform scaling matrix.
        /// </summary>
        /// <param name="scale">Value to use in the diagonal.</param>
        /// <param name="matrix">Scaling matrix.</param>
        public static void CreateScale(float scale, out Matrix2x2 matrix)
        {
            matrix.M11 = scale;
            matrix.M22 = scale;

            matrix.M12 = 0;
            matrix.M21 = 0;
        }
Ejemplo n.º 33
0
        public void EqualityOperatorWorksCorrectly()
        {
            Matrix2x2 value1 = new Matrix2x2(100);
            Matrix2x2 value2 = new Matrix2x2(50) * 2;

            Assert.Equal(value1, value2);
            Assert.True(value1 == value2, "Equality operator failed.");
        }
Ejemplo n.º 34
0
        public void ConstantValuesAreCorrect()
        {
            Matrix2x2 matrix2x2 = new Matrix2x2();

            Assert.Equal(2, matrix2x2.Columns);
            Assert.Equal(2, matrix2x2.Rows);
            Assert.Equal(Matrix2x2.ColumnCount, matrix2x2.Columns);
            Assert.Equal(Matrix2x2.RowCount, matrix2x2.Rows);
        }
Ejemplo n.º 35
0
        /// <summary>
        /// Adds the two matrices together on a per-element basis.
        /// </summary>
        /// <param name="a">First matrix to add.</param>
        /// <param name="b">Second matrix to add.</param>
        /// <param name="result">Sum of the two matrices.</param>
        public static void Add(ref Matrix2x2 a, ref Matrix2x2 b, out Matrix2x2 result)
        {
            float m11 = a.M11 + b.M11;
            float m12 = a.M12 + b.M12;

            float m21 = a.M21 + b.M21;
            float m22 = a.M22 + b.M22;

            result.M11 = m11;
            result.M12 = m12;

            result.M21 = m21;
            result.M22 = m22;
        }
Ejemplo n.º 36
0
        static void Main(string[] args)
        {
            Vector2D vec1 = new Vector2D(1.0, 3.0);
            Vector2D vec2 = new Vector2D(2.0, 4.0);
            Vector2D vec3 = new Vector2D(1.0, 3.0);
            Vector2D vec4 = new Vector2D(3.0, 1.0);
            Matrix2x2 m = new Matrix2x2(vec1, vec2);
            Matrix2x2 n = new Matrix2x2(vec3, vec4);
            Matrix2x2 p;

            Console.WriteLine("m is");

            m.WriteMatrix();

            Console.WriteLine("n is");

            n.WriteMatrix();

            Console.WriteLine("Transpose matrix m");

            m.Transpose();
            m.WriteMatrix();

            Console.WriteLine("Add matrix m and n");

            m.AddMatrix(n);
            m.WriteMatrix();

            Console.WriteLine("subtract matrix m-n");
            m.SubtractMatrix(n);
            m.WriteMatrix();

            Console.WriteLine("Multiply m and n");

            p = m.MultiplyMatrix(n);
            p.WriteMatrix();

            m.SetScalingMatrix(4, 4);
            m.WriteMatrix();

            m.SetRotationMatrixInDegrees(60.0);
            m.WriteMatrix();

            m.SetIdentityMatrix();
            m.WriteMatrix();

            Console.ReadLine();
        }
Ejemplo n.º 37
0
        public void IndexerGetAndSetValuesCorrectly()
        {
            Matrix2x2 matrix2x2 = new Matrix2x2();

            for (int x = 0; x < matrix2x2.Columns; x++)
            {
                for (int y = 0; y < matrix2x2.Rows; y++)
                {
                    matrix2x2[x, y] = y * matrix2x2.Columns + x;
                }
            }

            for (int y = 0; y < matrix2x2.Rows; y++)
            {
                for (int x = 0; x < matrix2x2.Columns; x++)
                {
                    Assert.Equal(y * matrix2x2.Columns + x, matrix2x2[x, y], Epsilon);
                }
            }
        }
Ejemplo n.º 38
0
        public void AccessorThrowsWhenOutOfBounds()
        {
            Matrix2x2 matrix2x2 = new Matrix2x2();

            try
            {
                matrix2x2[-1, 0] = 0;
                Assert.Fail("Matrix2x2[-1, 0] did not throw when it should have.");
            }
            catch (ArgumentOutOfRangeException)
            { }

            try
            {
                matrix2x2[0, -1] = 0;
                Assert.Fail("Matrix2x2[0, -1] did not throw when it should have.");
            }
            catch (ArgumentOutOfRangeException)
            { }

            try
            {
                matrix2x2[2, 0] = 0;
                Assert.Fail("Matrix2x2[2, 0] did not throw when it should have.");
            }
            catch (ArgumentOutOfRangeException)
            { }

            try
            {
                matrix2x2[0, 2] = 0;
                Assert.Fail("Matrix2x2[0, 2] did not throw when it should have.");
            }
            catch (ArgumentOutOfRangeException)
            { }
        }
Ejemplo n.º 39
0
 public static void Multiply(ref Matrix2x2 left, ref Matrix2D right, out Matrix2D result)
 {
     Matrix2x2.Multiply(ref left, ref right.NormalMatrix, out result.NormalMatrix);
     Matrix3x3.Multiply(ref left, ref right.VertexMatrix, out result.VertexMatrix);
 }
Ejemplo n.º 40
0
 private void GenerateFilledMatrixWithValues(out Matrix2x2 matrix)
 {
     matrix = new Matrix2x2(0, 1, 
                            2, 3);
 }
Ejemplo n.º 41
0
        public void AccessorThrowsWhenOutOfBounds()
        {
            Matrix2x2 matrix2x2 = new Matrix2x2();

            Assert.Throws<ArgumentOutOfRangeException>(() => { matrix2x2[-1, 0] = 0; });
            Assert.Throws<ArgumentOutOfRangeException>(() => { matrix2x2[0, -1] = 0; });
            Assert.Throws<ArgumentOutOfRangeException>(() => { matrix2x2[2, 0] = 0; });
            Assert.Throws<ArgumentOutOfRangeException>(() => { matrix2x2[0, 2] = 0; });
        }
Ejemplo n.º 42
0
        public void MuliplyByMatrix2x1ProducesMatrix2x2()
        {
            Matrix1x2 matrix1 = new Matrix1x2(3);
            Matrix2x1 matrix2 = new Matrix2x1(2);
            Matrix2x2 result = matrix1 * matrix2;
            Matrix2x2 expected = new Matrix2x2(6, 6,
                                               6, 6);

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 43
0
        /// <summary>
        /// Multiplies the two matrices.
        /// </summary>
        /// <param name="a">First matrix to multiply.</param>
        /// <param name="b">Second matrix to multiply.</param>
        /// <param name="result">Product of the multiplication.</param>
        public static void Multiply(ref Matrix2x2 a, ref Matrix b, out Matrix2x2 result)
        {
            float resultM11 = a.M11 * b.M11 + a.M12 * b.M21;
            float resultM12 = a.M11 * b.M12 + a.M12 * b.M22;

            float resultM21 = a.M21 * b.M11 + a.M22 * b.M21;
            float resultM22 = a.M21 * b.M12 + a.M22 * b.M22;

            result.M11 = resultM11;
            result.M12 = resultM12;

            result.M21 = resultM21;
            result.M22 = resultM22;
        }
Ejemplo n.º 44
0
        /// <summary>
        /// Multiplies the two matrices.
        /// </summary>
        /// <param name="a">First matrix to multiply.</param>
        /// <param name="b">Second matrix to multiply.</param>
        /// <param name="result">Product of the multiplication.</param>
        public static void Multiply(ref System.Numerics.Matrix4x4 a, ref Matrix2x2 b, out Matrix2x2 result)
        {
            float resultM11 = a.M11 * b.M11 + a.M12 * b.M21;
            float resultM12 = a.M11 * b.M12 + a.M12 * b.M22;

            float resultM21 = a.M21 * b.M11 + a.M22 * b.M21;
            float resultM22 = a.M21 * b.M12 + a.M22 * b.M22;

            result.M11 = resultM11;
            result.M12 = resultM12;

            result.M21 = resultM21;
            result.M22 = resultM22;
        }
Ejemplo n.º 45
0
        public void EqualityOperatorWorksCorrectly()
        {
            Matrix2x2 value1 = new Matrix2x2(100);
            Matrix2x2 value2 = new Matrix2x2(50) * 2;

            Assert.Equal(value1, value2);
            Assert.True(value1 == value2, "Equality operator failed.");
        }
Ejemplo n.º 46
0
        public void MuliplyByMatrix2x4ProducesMatrix2x2()
        {
            Matrix4x2 matrix1 = new Matrix4x2(3);
            Matrix2x4 matrix2 = new Matrix2x4(2);
            Matrix2x2 result = matrix1 * matrix2;
            Matrix2x2 expected = new Matrix2x2(24, 24,
                                               24, 24);

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 47
0
        /// <summary>
        /// Multiplies the two matrices.
        /// </summary>
        /// <param name="a">First matrix to multiply.</param>
        /// <param name="b">Second matrix to multiply.</param>
        /// <param name="result">Product of the multiplication.</param>
        public static void Multiply(ref Matrix2x3 a, ref Matrix3x2 b, out Matrix2x2 result)
        {
            result.M11 = a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31;
            result.M12 = a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32;

            result.M21 = a.M21 * b.M11 + a.M22 * b.M21 + a.M23 * b.M31;
            result.M22 = a.M21 * b.M12 + a.M22 * b.M22 + a.M23 * b.M32;
        }
Ejemplo n.º 48
0
        public void MuliplyByMatrix2x2ProducesMatrix2x1()
        {
            Matrix2x1 matrix1 = new Matrix2x1(3);
            Matrix2x2 matrix2 = new Matrix2x2(2);
            Matrix2x1 result = matrix1 * matrix2;
            Matrix2x1 expected = new Matrix2x1(12, 12);

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 49
0
        public void MuliplyByMatrix2x2ProducesMatrix2x3()
        {
            Matrix2x3 matrix1 = new Matrix2x3(3);
            Matrix2x2 matrix2 = new Matrix2x2(2);
            Matrix2x3 result = matrix1 * matrix2;
            Matrix2x3 expected = new Matrix2x3(12, 12, 
                                               12, 12, 
                                               12, 12);

            Assert.Equal(expected, result);
        }
Ejemplo n.º 50
0
        /// <summary>
        /// Negates every element in the matrix.
        /// </summary>
        /// <param name="matrix">System.Numerics.Matrix4x4 to negate.</param>
        /// <param name="result">Negated matrix.</param>
        public static void Negate(ref Matrix2x2 matrix, out Matrix2x2 result)
        {
            float m11 = -matrix.M11;
            float m12 = -matrix.M12;

            float m21 = -matrix.M21;
            float m22 = -matrix.M22;

            result.M11 = m11;
            result.M12 = m12;

            result.M21 = m21;
            result.M22 = m22;
        }
Ejemplo n.º 51
0
        /// <summary>
        /// Subtracts the two matrices from each other on a per-element basis.
        /// </summary>
        /// <param name="a">First matrix to subtract.</param>
        /// <param name="b">Second matrix to subtract.</param>
        /// <param name="result">Difference of the two matrices.</param>
        public static void Subtract(ref Matrix2x2 a, ref Matrix2x2 b, out Matrix2x2 result)
        {
            float m11 = a.M11 - b.M11;
            float m12 = a.M12 - b.M12;

            float m21 = a.M21 - b.M21;
            float m22 = a.M22 - b.M22;

            result.M11 = m11;
            result.M12 = m12;

            result.M21 = m21;
            result.M22 = m22;
        }
Ejemplo n.º 52
0
 /// <summary>
 /// Transforms the vector by the matrix.
 /// </summary>
 /// <param name="v">System.Numerics.Vector2 to transform.</param>
 /// <param name="matrix">System.Numerics.Matrix4x4 to use as the transformation.</param>
 /// <param name="result">Product of the transformation.</param>
 public static void Transform(ref System.Numerics.Vector2 v, ref Matrix2x2 matrix, out System.Numerics.Vector2 result)
 {
     float vX = v.X;
     float vY = v.Y;
     #if !WINDOWS
     result = new System.Numerics.Vector2();
     #endif
     result.X = vX * matrix.M11 + vY * matrix.M21;
     result.Y = vX * matrix.M12 + vY * matrix.M22;
 }
Ejemplo n.º 53
0
        /// <summary>
        /// Computes the transposed matrix of a matrix.
        /// </summary>
        /// <param name="matrix">System.Numerics.Matrix4x4 to transpose.</param>
        /// <param name="result">Transposed matrix.</param>
        public static void Transpose(ref Matrix2x2 matrix, out Matrix2x2 result)
        {
            float m21 = matrix.M12;

            result.M11 = matrix.M11;
            result.M12 = matrix.M21;

            result.M21 = m21;
            result.M22 = matrix.M22;
        }
Ejemplo n.º 54
0
        public void MuliplyByMatrix2x3ProducesMatrix2x2()
        {
            Matrix3x2 matrix1 = new Matrix3x2(3);
            Matrix2x3 matrix2 = new Matrix2x3(2);
            Matrix2x2 result = matrix1 * matrix2;
            Matrix2x2 expected = new Matrix2x2(18, 18,
                                               18, 18);

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 55
0
        /// <summary>
        /// Adds the two matrices together on a per-element basis.
        /// </summary>
        /// <param name="a">First matrix to add.</param>
        /// <param name="b">Second matrix to add.</param>
        /// <param name="result">Sum of the two matrices.</param>
        public static void Add(ref System.Numerics.Matrix4x4 a, ref System.Numerics.Matrix4x4 b, out Matrix2x2 result)
        {
            float m11 = a.M11 + b.M11;
            float m12 = a.M12 + b.M12;

            float m21 = a.M21 + b.M21;
            float m22 = a.M22 + b.M22;

            result.M11 = m11;
            result.M12 = m12;

            result.M21 = m21;
            result.M22 = m22;
        }
Ejemplo n.º 56
0
        public void SimpleSubtractionGeneratesCorrectValues()
        {
            Matrix2x2 value1 = new Matrix2x2(100);
            Matrix2x2 value2 = new Matrix2x2(1);
            Matrix2x2 result = value1 - value2;

            for (int y = 0; y < Matrix2x2.RowCount; y++)
            {
                for (int x = 0; x < Matrix2x2.ColumnCount; x++)
                {
                    Assert.Equal(100 - 1, result[x, y], Epsilon);
                }
            }
        }
Ejemplo n.º 57
0
 public static Matrix2D Multiply(Matrix2x2 left, Matrix2D right)
 {
     Matrix2D result;
     Multiply(ref left, ref right, out result);
     return result;
 }
Ejemplo n.º 58
0
        /// <summary>
        /// Constructs a uniform scaling matrix.
        /// </summary>
        /// <param name="scale">Value to use in the diagonal.</param>
        /// <param name="matrix">Scaling matrix.</param>
        public static void CreateScale(float scale, out Matrix2x2 matrix)
        {
            matrix.M11 = scale;
            matrix.M22 = scale;

            matrix.M12 = 0;
            matrix.M21 = 0;
        }
Ejemplo n.º 59
0
 public Matrix2D(Matrix2x2 normalMatrix, Matrix3x3 vertexMatrix)
 {
     this.NormalMatrix = normalMatrix;
     this.VertexMatrix = vertexMatrix;
 }
Ejemplo n.º 60
0
        /// <summary>
        /// Inverts the given matix.
        /// </summary>
        /// <param name="matrix">System.Numerics.Matrix4x4 to be inverted.</param>
        /// <param name="result">Inverted matrix.</param>
        public static void Invert(ref Matrix2x2 matrix, out Matrix2x2 result)
        {
            float determinantInverse = 1 / (matrix.M11 * matrix.M22 - matrix.M12 * matrix.M21);
            float m11 = matrix.M22 * determinantInverse;
            float m12 = -matrix.M12 * determinantInverse;

            float m21 = -matrix.M21 * determinantInverse;
            float m22 = matrix.M11 * determinantInverse;

            result.M11 = m11;
            result.M12 = m12;

            result.M21 = m21;
            result.M22 = m22;
        }