Monday, 20 October 2008

Week 3

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.

Tuesday, 14 October 2008

Week 2

This week we we're asked to modify an incomplete program that would enable the user to add, subtract, multiply, divide and find the dot and cross product of given vectors. This was a bit more trickier then the previous weeks work but by using different texts as reading resources it became easier.



Here is some examples of the code:



To add


float totalX = _x + rhs._x;
float totalY = _y + rhs._y;
float totalZ = _z + rhs._z;
Vector VectorTotal (totalX, totalY, totalZ);



To multiply by a scalar


float totalX = _x * scalar;
float totalY = _y * scalar;
float totalZ = _z * scalar;

Vector VectorTotal (totalX, totalY, totalZ)

To find the Cross Product


float x1 = _x;
float y1 = _y;
float z1 = _z;
Vector vCrossProduct;
vCrossProduct._x = _y * v._z - _z *v._y;
vCrossProduct._y = _z * v._x - _x *v._z;
vCrossProduct._z = _x * v._y - _y *v._x;

There wasn't too many problems with this, just minor errors such as a missing semi-colon or missing out an underscore.

Week 1

This week we we're asked to modify an empty windows application so that it would then display some text and to draw a line.

It was some simple code that had to be added into the the OnPaint void to get them both working.

To get it to create a line the code needed was

//line
Pen pen(Color(255, 0, 0, 255));
graphics.DrawLine(&pen, 0, 0, 200, 100);

and then to create text on the screen as well it meant having to add this code as well,


SolidBrush brush(Color(255, 0, 0, 255));
FontFamily fontFamily(L"Times New Roman");
Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
PointF pointF(10.0f, 20.0f);
graphics.DrawString(L"My World Soon!", -1, &font, pointF, &brush);

The finished product of both of these is the message "My World Soon" with a diaganal blue line going through in.

Tuesday, 7 October 2008

Introduction

My name is Jordan Cordall and I am a second year student at the University of Derby studying Computer Games Programming. This blog will then keep a record of problems that I have come up accross and how I deal with them.

As the year progresses then the amount of entries in here will increase as more difficult problems arise.