This week I was asked to create my own Matrix class that was to be used with a header file that was provided by our lecturer, as you can expect the difficulty of this task was greater than the previous weeks work, in addition to this I also had to re-visit my Vector class work and enable it to deal with 4D Vectors.
Using my previous Vector class as a reference I was able to create the basic structure of how my new class would look like, I then had to create all of the calculations for matrices and get them working.
First off is the adding and subtracting which is pretty straight forward for matrices as you will either just add to or subtract from the elements that make up the Matrix, this is the code that I used to add the first row of numbers together in the Matrix
mt._m[0][0] = m1._m[0][0] + m2._m[0][0];
mt._m[0][1] = m1._m[0][1] + m2._m[0][1];
mt._m[0][2] = m1._m[0][2] + m2._m[0][2];
mt._m[0][3] = m1._m[0][3] + m2._m[0][3];
To subtract would be just the same code but to have a minus sign instead of the plus.
There are two different ways that you can multiply a Matrix these being by scalar or by another Matrix.
To multiply by a scalar you simply have to make the new total equal the old value multiplied by the set value given or by a value that the user would enter.
mt._m[0][0] = m1._m[0][0] * s;
mt._m[0][1] = m1._m[0][1] * s;
mt._m[0][2] = m1._m[0][2] * s;
mt._m[0][3] = m1._m[0][3] * s;
Again this code is only for the first line of the Matrix.
The second method is to multiply by another matrix, the actual theory of doing it is simple, you multiply out the first row by the first column and add up the numbers to give you the first number of the new matrix, you then multiply out the first row with the second column to give you the second number etc… until the Matrix is finished.
Say you had the Matrices
[1 2 3 4] [1 2 3 4]
[5 6 7 8] [5 6 7 8]
[9 10 11 12] [9 10 11 12]
[13 14 15 16] [13 14 15 16]
If you then label the matrices and work them out on paper it makes it a lot easier to program
A B C D
1 [1 2 3 4] [1 2 3 4]
2 [5 6 7 8] [5 6 7 8]
3 [9 10 11 12] [9 10 11 12]
4 [13 14 15 16] [13 14 15 16]
You would then do 1A, 1B , 1C, 1D then go onto 2A, 2B, 2C etc…
This is the code that would create the first columns of the Matrix
mt._m[0][0] = (m1._m[0][0]* m2._m[0][0])+(m1._m[1][0]* m2._m[0][1])+(m1._m[2][0]* m2._m[0][2])+(m1._m[3][0]* m2._m[0][3]);
mt._m[1][0] = (m1._m[0][0]* m2._m[1][0])+(m1._m[1][0]* m2._m[1][1])+(m1._m[2][0]* m2._m[1][2])+(m1._m[3][0]* m2._m[1][3]);
mt._m[2][0] = (m1._m[0][0]* m2._m[2][0])+(m1._m[1][0]* m2._m[2][1])+(m1._m[2][0]* m2._m[2][2])+(m1._m[3][0]* m2._m[2][3]);
mt._m[3][0] = (m1._m[0][0]* m2._m[3][0])+(m1._m[1][0]* m2._m[3][1])+(m1._m[2][0]* m2._m[3][2])+(m1._m[3][0]* m2._m[3][3]);
There were some errors that I encountered while making up the Matrix class that I couldn’t understand why they were happening until I started reading through my code again from the beginning. One of my errors was this
“error C2676: binary ‘[‘ : ‘Matrix’ does not define this operator or a conversion to a type acceptable to the predefined operator”
It turns out that I had this error because instead of having “mt._m[0][0]” for some of the calculations it only had “mt[0][0]”.
Another one of my errors was the fact that I had called in the main application a function that was spelt slightly different to how it should have been spelt.
This will be edited and updated at a later date.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment