How can I multiply two matrices in C#?

后端 未结 10 1816
不思量自难忘°
不思量自难忘° 2021-01-02 03:24

Like described in the title, is there some library in the Microsoft framework which allows to multiply two matrices or do I have to write my own method to do this? // I\'ve

10条回答
  •  天涯浪人
    2021-01-02 04:14

    Whilst there's no built in Maths framework to do this in .NET (could use XNA's Maths library), there is a Matrix in the System.Windows.Media namespace. The Matrix structure has a Multiply method which takes in another Matrix and outputs a Matrix.

    Matrix matrix1 = new Matrix(5, 10, 15, 20, 25, 30);
    Matrix matrix2 = new Matrix(2, 4, 6, 8, 10, 12);
    
    // matrixResult is equal to (70,100,150,220,240,352) 
    Matrix matrixResult = Matrix.Multiply(matrix1, matrix2);
    
    // matrixResult2 is also
    // equal to (70,100,150,220,240,352) 
    Matrix matrixResult2 = matrix1 * matrix2;
    

    This is mainly used for 2D transformation:

    Represents a 3x3 affine transformation matrix used for transformations in 2-D space.

    but if it suits your needs, then there's no need for any third party libraries.

提交回复
热议问题