From 72168eaa790f9de1f76f1b0030a84e474206b734 Mon Sep 17 00:00:00 2001 From: linkse100241 Date: Sun, 16 Nov 2025 16:46:56 +0100 Subject: [PATCH] test.cpp aktualisiert --- includes/gamematrix.cpp | 150 ++++++++++++++++++++++++++++++++++++++++ src/gamematrix.h | 19 +++++ 2 files changed, 169 insertions(+) create mode 100644 includes/gamematrix.cpp create mode 100644 src/gamematrix.h diff --git a/includes/gamematrix.cpp b/includes/gamematrix.cpp new file mode 100644 index 0000000..7fef98b --- /dev/null +++ b/includes/gamematrix.cpp @@ -0,0 +1,150 @@ +// +// Created by kris- on 03.11.2025. +// +#include +#include "gamematrix.h" + +// Matrixmultiplikation + + +// Rotationsmatrix um Achse x/y/z +static std::array,4> rot3D(double angle_deg, char axis); + +// Verschiebung +static std::array,4> translate(const std::array& pos); + + + + +//Matrixmultiplikation-Funktion +std::array,4> gameMatrix::matmul( + const std::array,4>& A, + const std::array,4>& B) + +{ //Matrix-Mathematik (Matrix C = A*B) + std::array,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,4> gameMatrix::rot3D(double angle_deg, char axis) { + double rad = angle_deg * M_PI / 180.0; + + //Mathematik-Rotation + std::array,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,4> gameMatrix::translate(const std::array& pos) { + std::array,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,4> A{}; + std::array,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,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,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 pos; + std::cout << "\nGib die Translation (x y z) ein: "; + std::cin >> pos[0] >> pos[1] >> pos[2]; + + std::array,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; +} + + + + diff --git a/src/gamematrix.h b/src/gamematrix.h new file mode 100644 index 0000000..7a644d6 --- /dev/null +++ b/src/gamematrix.h @@ -0,0 +1,19 @@ +#pragma once +#include +#include +#include +#include + +class gameMatrix +{ +public: + // Matrix Multiplikation + static std::array,4> matmul(const std::array,4>& A, + const std::array,4>& B); + + // Rotationsmatrix um Achse x/y/z + static std::array,4> rot3D(double angle_deg, char axis); + + // Verschiebung + static std::array,4> translate(const std::array& pos); +};