This long time in learning kinetics, encounter a very tricky problem - the calculation of contact force, that is, for a certain point of one object with another problem (VR = (V1 - V2) points N) For the force of 0 or near 0, it is necessary to use a matrix of random rows and columns (I call it Bigmatrix), and Microsoft's DXSDK's math library does not provide a matrix greater than 4x4, so I decide yourself. Some efforts, complete it, now I put the source code, I hope to help everyone in the future!
Bigmatrix is a class, =, - =, * = and / = a member function of BigmaTrix, and overloaded, returns a reference! And , -, *, / is non-member function, these operators are not Overload, the reason is due to the defect of C itself, in this case, only return values, can not be a pointer or reference, because , -, *, / as a member function, return pointer or reference can only be Pointer or reference for local variables, this is a serious error (why please see more about C books for more understanding) and return value to call the copy constructor, this efficiency is very low, so I use one more here. Contains functions of the return value, such as MatrixMultiply, in fact Microsoft is really doing this in the DXSDK math library!!
The code is as follows: Two files, one is matrix.cpp, the other is Matrix.h.
//Matrix.cpp//#include "matrix.h"
BigMatrix :: Bigmatrix (int IROW, IC iColumn) {m_IROW = IROW; m_IColumn = iColumn; // Now defines an array m_f_pmatrix = new float * [iRow]; for (int I = 0; i For (int i = 0; i / / Now define an array /*matrix.resize (I = 0; i } BigMatrix :: ~ Bigmatrix (Void) {for (int i = 0; i / / Establish a Row X ROW unit matrix, used to store the result float ** f_presult = new float * [iRow]; for (int i = 0; i // Initialization unit matrix; for (int i = 0; i Return pout;} Bigmatrix * MatrixTranspose (Bigmatrix * Pout, Const BigmaTrix * PM) {float ** f_ptemp = new float * [PM-> getColumn ()]; for (int i = 0; i For (int i = 0; i For (int i = 0; i Return pout;} Bigmatrix * MatrixAdd (Bigmatrix * Pout, Const Bigmatrix * PM1, Const Bigmatrix * PM2) {for (INT i = 0; I Bigmatrix * MatrixSubtract (Bigmatrix * Pout, Const Bigmatrix * PM1, Const BigmaTrix * PM2) { For (int i = 0; I Bigmatrix * Matrixmultiply (Bigmatrix * POUT, Const Bigmatrix * PM1, Const Bigmatrix * PM2) {for (int i = 0; i } Return pout;} ///Matrix.h///matr @ include PRIVATE: INT M_IROW, M_ICOLUMN; } BigMatrix * MatrixInverse (BigMatrix * pOut, const BigMatrix * pM); BigMatrix * MatrixTranspose (BigMatrix * pOut, const BigMatrix * pM); BigMatrix * MatrixAdd (BigMatrix * pOut, const BigMatrix * pM1, const BigMatrix * pM2); BigMatrix * MatrixSubtract (Bigmatrix * Punt, Const Bigmatrix * PM1, Const BigmaTrix * PM2); Bigmatrix * Matrixmultiply (Bigmatrix * Pout, Const Bigmatrix * PM1, Const Bigmatrix * PM2); Execute Demo Results: