Prog3/src/gamematrix.cpp

75 lines
2.0 KiB
C++

#include "gamematrix.h"
#include <cmath>
// Entfernt: namespace Matrix3D { ... } <-- GEÄNDERT
// Implementierungen jetzt als Klassenmethoden von gameMatrix <-- GEÄNDERT
// Optional: identity() als Hilfsmethode hinzugefügt
static std::array<std::array<double,4>,4> identity() { // <-- NEU
std::array<std::array<double,4>,4> m{};
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
m[i][j] = (i == j) ? 1.0 : 0.0;
}
}
return m;
}
// -------------------- matmul --------------------
std::array<std::array<double,4>,4> gameMatrix::matmul( // <-- GEÄNDERT
const std::array<std::array<double,4>,4>& A,
const std::array<std::array<double,4>,4>& B)
{
std::array<std::array<double,4>,4> R{};
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
double sum = 0.0;
for(int k = 0; k < 4; k++) {
sum += A[i][k] * B[k][j];
}
R[i][j] = sum;
}
}
return R;
}
// -------------------- translate --------------------
std::array<std::array<double,4>,4> gameMatrix::translate( // <-- GEÄNDERT
const std::array<double,3>& pos)
{
auto t = identity(); // <-- NEU: nutzt die Hilfsmethode
t[0][3] = pos[0];
t[1][3] = pos[1];
t[2][3] = pos[2];
return t;
}
// -------------------- rot3D --------------------
std::array<std::array<double,4>,4> gameMatrix::rot3D( // <-- GEÄNDERT
double angle_deg, char axis)
{
auto r = identity(); // <-- NEU: nutzt die Hilfsmethode
double rad = angle_deg * M_PI / 180.0;
double c = std::cos(rad);
double s = std::sin(rad);
switch(axis) {
case 'x': case 'X':
r[1][1] = c; r[1][2] = -s;
r[2][1] = s; r[2][2] = c;
break;
case 'y': case 'Y':
r[0][0] = c; r[0][2] = s;
r[2][0] = -s; r[2][2] = c;
break;
case 'z': case 'Z':
r[0][0] = c; r[0][1] = -s;
r[1][0] = s; r[1][1] = c;
break;
}
return r;
}