prog3b_652/includes/gamematrix.cpp
2025-11-09 17:29:05 +01:00

151 lines
3.6 KiB
C++

//
// Created by kris- on 03.11.2025.
//
#include <iostream>
#include "gamematrix.h"
// Matrixmultiplikation
// Rotationsmatrix um Achse x/y/z
static std::array<std::array<double,4>,4> rot3D(double angle_deg, char axis);
// Verschiebung
static std::array<std::array<double,4>,4> translate(const std::array<double, 3>& pos);
//Matrixmultiplikation-Funktion
std::array<std::array<double,4>,4> gameMatrix::matmul(
const std::array<std::array<double,4>,4>& A,
const std::array<std::array<double,4>,4>& B)
{ //Matrix-Mathematik (Matrix C = A*B)
std::array<std::array<double,4>,4> C{};
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
for (int k = 0; k < 4; ++k)
C[i][j] += A[i][k] * B[k][j];
return C;
}
//Rotation 3D
std::array<std::array<double,4>,4> gameMatrix::rot3D(double angle_deg, char axis) {
double rad = angle_deg * M_PI / 180.0;
//Mathematik-Rotation
std::array<std::array<double,4>,4> R{};
for (int i = 0; i < 4; ++i)
R[i][i] = 1.0;
if (axis == 'x' || axis == 'X') {
R[1][1] = cos(rad);
R[1][2] = -sin(rad);
R[2][1] = sin(rad);
R[2][2] = cos(rad);
} else if (axis == 'y' || axis == 'Y') {
R[0][0] = cos(rad);
R[0][2] = sin(rad);
R[2][0] = -sin(rad);
R[2][2] = cos(rad);
} else if (axis == 'z' || axis == 'Z') {
R[0][0] = cos(rad);
R[0][1] = -sin(rad);
R[1][0] = sin(rad);
R[1][1] = cos(rad);
}
return R;
}
//Translation
std::array<std::array<double,4>,4> gameMatrix::translate(const std::array<double,3>& pos) {
std::array<std::array<double,4>,4> T{};
for (int i = 0; i < 4; ++i)
T[i][i] = 1.0; // Identitätsmatrix
T[0][3] = pos[0]; // x-Verschiebung
T[1][3] = pos[1]; // y-Verschiebung
T[2][3] = pos[2]; // z-Verschiebung
return T;
}
int main()
{
// Part Matrix
std::array<std::array<double,4>,4> A{};
std::array<std::array<double,4>,4> B{};
std::cout << "Gib die Werte für Matrix A (4x4) ein:\n";
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
std::cout << "A[" << i << "][" << j << "] = ";
std::cin >> A[i][j];
}
std::cout << "\nGib die Werte für Matrix B (4x4) ein:\n";
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
std::cout << "B[" << i << "][" << j << "] = ";
std::cin >> B[i][j];
}
std::array<std::array<double,4>,4> C = gameMatrix::matmul(A, B);
std::cout << "\nErgebnis der Matrixmultiplikation (C = A * B):\n";
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j)
std::cout << C[i][j] << "\t";
std::cout << "\n";
}
//Ende Matrix
// Rotation
double angle;
char axis;
std::cout << "\nGib einen Rotationswinkel in Grad ein: ";
std::cin >> angle;
std::cout << "Gib die Rotationsachse (x/y/z) ein: ";
std::cin >> axis;
std::array<std::array<double,4>,4> R = gameMatrix::rot3D(angle, axis);
std::cout << "\nRotationsmatrix R:\n";
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j)
std::cout << R[i][j] << "\t";
std::cout << "\n";
}
// Translation
std::array<double,3> pos;
std::cout << "\nGib die Translation (x y z) ein: ";
std::cin >> pos[0] >> pos[1] >> pos[2];
std::array<std::array<double,4>,4> T = gameMatrix::translate(pos);
std::cout << "\nTranslationsmatrix T:\n";
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j)
std::cout << T[i][j] << "\t";
std::cout << "\n";
}
return 0;
}