109 lines
1.7 KiB
C
109 lines
1.7 KiB
C
#ifndef MATRIX_H
|
|
|
|
|
|
#include "point.h"
|
|
|
|
|
|
// STRUCTURE
|
|
struct Matrix
|
|
{
|
|
inline Matrix(void)
|
|
{
|
|
e11 = 1.0; e12 = 0.0; e13 = 0.0; e14 = 0.0;
|
|
e21 = 0.0; e22 = 1.0; e23 = 0.0; e24 = 0.0;
|
|
e31 = 0.0; e32 = 0.0; e33 = 1.0; e34 = 0.0;
|
|
e41 = 0.0; e42 = 0.0; e43 = 0.0; e44 = 1.0;
|
|
}
|
|
|
|
union
|
|
{
|
|
float e[4][4];
|
|
|
|
struct
|
|
{
|
|
float e11, e12, e13, e14;
|
|
float e21, e22, e23, e24;
|
|
float e31, e32, e33, e34;
|
|
float e41, e42, e43, e44;
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
// OPERATORS
|
|
// m1 * m2
|
|
inline Matrix operator*(Matrix m1, Matrix m2)
|
|
{
|
|
Matrix result;
|
|
|
|
for (int row = 0; row < 4; ++row)
|
|
{
|
|
for (int col = 0; col < 4; ++col)
|
|
{
|
|
float sum = 0.0;
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
{
|
|
sum += m1.e[row][i] * m2.e[i][col];
|
|
}
|
|
|
|
result.e[row][col] = sum;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// v * m
|
|
inline Point operator*(Point v, Matrix m)
|
|
{
|
|
Point result;
|
|
|
|
for (int col = 0; col < 4; ++col)
|
|
{
|
|
float sum = 0.0;
|
|
|
|
for (int row = 0; row < 4; ++row)
|
|
{
|
|
sum += v.e[row] * m.e[row][col];
|
|
}
|
|
|
|
result.e[col] = sum;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// v *=m
|
|
inline Point &operator*=(Point &v, Matrix m)
|
|
{
|
|
v = v * m;
|
|
|
|
return v;
|
|
}
|
|
|
|
// v * m
|
|
inline Vector operator*(Vector v, Matrix m)
|
|
{
|
|
Vector result;
|
|
|
|
for (int col = 0; col < 4; ++col)
|
|
{
|
|
float sum = 0.0;
|
|
|
|
for (int row = 0; row < 4; ++row)
|
|
{
|
|
sum += v.e[row] * m.e[row][col];
|
|
}
|
|
|
|
result.e[col] = sum;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
#define MATRIX_H
|
|
#endif
|
|
|