-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
executable file
·66 lines (60 loc) · 1.29 KB
/
matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "matrix.h"
Matrix::Matrix()
{
for (int i = 0; i < COLUMNS; i++) {
for (int j = 0; j < ROWS; j++) {
m[i][j] = 0;
}
}
}
Matrix::Matrix(float mat[][ROWS])
{
for (int i = 0; i < COLUMNS; i++) {
for (int j = 0; j < ROWS; j++) {
m[i][j] = mat[i][j];
}
}
}
void Matrix::display()
{
for(int i=0; i < COLUMNS; i++)
{
for(int j=0;j<ROWS;j++)
{
std::cout << m[i][j] << " ";
}
std::cout<<std::endl;
}
}
Matrix Matrix::operator *(Matrix x) //matrix multiplication
{
Matrix result;
for(int i=0; i<COLUMNS; i++)
{
for(int j=0; j<ROWS; j++)
{
result.m[i][j] = 0;
for(int k=0; k<COLUMNS; k++) // k < columns or rows?? check and correct
{
result.m[i][j]+=m[i][k]*x.m[k][j];
}
}
}
return(result);
}
const Vector_3d operator *(const Matrix &mat, const Vector_3d &p)
{
float ar[4], tem[4], sum;
ar[0] = p.x;
ar[1] = p.y;
ar[2] = p.z;
ar[3] = p.p;
for (int i = 0; i < 4; i++) {
sum = 0;
for (int j = 0; j < 4; j++) {
sum += mat.m[i][j] * ar[j];
}
tem[i] = sum;
}
return (Vector_3d(tem[0], tem[1], tem[2], tem[3]));
}