From 6ededbc4c29d61fb84535726ece66f5f09e3dd1a Mon Sep 17 00:00:00 2001 From: Bayu Tranggono Date: Thu, 29 Oct 2026 01:53:22 +0100 Subject: [PATCH] =?UTF-8?q?Orthogonale=20Projektion=20und=203D=5Frot=20mit?= =?UTF-8?q?=20Tests=20erg=C3=A4nzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AGENTS.md | 12 ++- pyproject.toml | 5 +- src/matrixmania/__init__.py | 1 + compute.py => src/matrixmania/compute.py | 77 +++++++++++++++++++ test/test_matrixmania/__init__.py | 0 test/test_matrixmania/test_projection.py | 26 +++++++ test/test_matrixmania/test_rot.py | 98 ++++++++++++++++++++++++ 7 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 src/matrixmania/__init__.py rename compute.py => src/matrixmania/compute.py (67%) create mode 100644 test/test_matrixmania/__init__.py create mode 100644 test/test_matrixmania/test_projection.py create mode 100644 test/test_matrixmania/test_rot.py diff --git a/AGENTS.md b/AGENTS.md index fa8e3b1..55c734b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,5 +1,9 @@ - # Regeln für dieses Projekt +# Regeln für dieses Projekt - - Code nach PEP 8, Zeilenlänge maximal 88 Zeichen. - - Jede Funktion und Methode hat einen Docstring und Type Hints. - - Vor jedem Commit: `ruff check` und `ruff format`. \ No newline at end of file +- Code nach PEP 8, Zeilenlänge maximal 88 Zeichen. +- Jede Funktion und Methode hat einen Docstring und Type Hints. +- Vor jedem Commit: `ruff check` und `ruff format`. + +## Projektstruktur +- Quellcode liegt unter "src/matrixmania/", Tests unter "test/test_matrixmania/" +- Testpakete, Testdateien und Testfunktionen beginnen mit "test_" \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index d9f90c0..f2ac542 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,4 +4,7 @@ version = "0.1.0" dependencies = ["tabulate", "ruff"] [project.optional-dependencies] -dev = ["pytest", "ruff", "pytest-cov"] \ No newline at end of file +dev = ["pytest", "ruff", "pytest-cov"] + +[tool.pytest.ini_options] +pythonpath = "src" \ No newline at end of file diff --git a/src/matrixmania/__init__.py b/src/matrixmania/__init__.py new file mode 100644 index 0000000..2201c3b --- /dev/null +++ b/src/matrixmania/__init__.py @@ -0,0 +1 @@ +from .compute import matmul, project_ortho, rot_2D, rot_3D, transpose \ No newline at end of file diff --git a/compute.py b/src/matrixmania/compute.py similarity index 67% rename from compute.py rename to src/matrixmania/compute.py index 9f73292..81359fa 100644 --- a/compute.py +++ b/src/matrixmania/compute.py @@ -83,6 +83,74 @@ def rot_2D(angle: float) -> Matrix: ] +def rot_3D(angle: float, axis: str) -> Matrix: + """ + Liefert die Rotationsmatrix für eine Drehung um eine Koordinatenachse im 3D-Raum. + + Rotation um die x-Achse: + Rx(θ) = [[1, 0, 0], + [0, cos θ, -sin θ], + [0, sin θ, cos θ]] + + Rotation um die y-Achse: + Ry(θ) = [[ cos θ, 0, sin θ], + [ 0, 1, 0], + [-sin θ, 0, cos θ]] + + Rotation um die z-Achse: + Rz(θ) = [[cos θ, -sin θ, 0], + [sin θ, cos θ, 0], + [ 0, 0, 1]] + + Ein Punkt (x, y, z) wird gedreht, indem die Rotationsmatrix mit dem + Spaltenvektor [[x], [y], [z]] multipliziert wird. + + :param angle: Drehwinkel θ im Bogenmaß + :param axis: Drehachse, entweder "x", "y" oder "z" + :return: Rotationsmatrix der Größe 3x3 + :raises ValueError: Wenn axis nicht "x", "y" oder "z" ist + """ + cos_angle = math.cos(angle) + sin_angle = math.sin(angle) + if axis == "x": + return [ + [1, 0, 0], + [0, cos_angle, -sin_angle], + [0, sin_angle, cos_angle], + ] + if axis == "y": + return [ + [cos_angle, 0, sin_angle], + [0, 1, 0], + [-sin_angle, 0, cos_angle], + ] + if axis == "z": + return [ + [cos_angle, -sin_angle, 0], + [sin_angle, cos_angle, 0], + [0, 0, 1], + ] + raise ValueError(f"Ungültige Achse '{axis}', erlaubt sind 'x', 'y' oder 'z'") + + +def project_ortho( + point: tuple[float, float, float], scale: float = 1 +) -> tuple[float, float]: + """ + Projiziert einen 3D-Punkt orthogonal auf die xy-Ebene. + + Die z-Koordinate wird ignoriert, x und y werden mit scale skaliert: + (x, y, z) -> (x * scale, y * scale) + + :param point: 3D-Punkt als Tupel (x, y, z) + :param scale: Skalierungsfaktor für den projizierten Punkt + :return: Projizierter 2D-Punkt als Tupel (x, y) + """ + x, y, _ = point + return x * scale, y * scale + + + if __name__ == "__main__": matrix_a = [ [3, 4, -1, 4], @@ -154,4 +222,13 @@ if __name__ == "__main__": assert math.isclose(rotated_point[0][0], -2) assert math.isclose(rotated_point[1][0], -1) + for axis in ("x", "y", "z"): + print(f"3D-Rotationsmatrix um die {axis}-Achse für 90°:") + print(tabulate(rot_3D(math.radians(90), axis))) + + try: + rot_3D(1.0, "w") + except ValueError as error: + print(f"Erwarteter Fehler bei Achse 'w': {error}") + print("Alle Beispiele wurden erfolgreich geprüft.") \ No newline at end of file diff --git a/test/test_matrixmania/__init__.py b/test/test_matrixmania/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_matrixmania/test_projection.py b/test/test_matrixmania/test_projection.py new file mode 100644 index 0000000..47b1bce --- /dev/null +++ b/test/test_matrixmania/test_projection.py @@ -0,0 +1,26 @@ +from matrixmania import project_ortho + + +def test_project_ortho_without_scale(): + # Arrange + point = (1.0, 2.0, 3.0) + expected = (1.0, 2.0) + + # Act + result = project_ortho(point) + + # Assert + assert result == expected + + +def test_project_ortho_with_scale_100(): + # Arrange + point = (1.0, 2.0, 3.0) + scale = 100 + expected = (100.0, 200.0) + + # Act + result = project_ortho(point, scale=scale) + + # Assert + assert result == expected \ No newline at end of file diff --git a/test/test_matrixmania/test_rot.py b/test/test_matrixmania/test_rot.py new file mode 100644 index 0000000..bef7a81 --- /dev/null +++ b/test/test_matrixmania/test_rot.py @@ -0,0 +1,98 @@ +import math + +import pytest + +from matrixmania import matmul, rot_2D, rot_3D + + +def test_rot_2D_zero_angle_returns_identity(): + # Arrange + angle = 0.0 + expected = [[1.0, 0.0], [0.0, 1.0]] + + # Act + result = rot_2D(angle) + + # Assert + assert result == expected + + +def test_rot_2D_rotates_point_by_90_degrees(): + # Arrange + angle = math.radians(90) + point = [[1], [0]] + expected = [0.0, 1.0] + + # Act + result = [row[0] for row in matmul(rot_2D(angle), point)] + + # Assert + assert result == pytest.approx(expected, abs=1e-9) + + +def test_rot_2D_rotates_point_by_180_degrees(): + # Arrange + angle = math.pi + point = [[2], [1]] + expected = [-2.0, -1.0] + + # Act + result = [row[0] for row in matmul(rot_2D(angle), point)] + + # Assert + assert result == pytest.approx(expected, abs=1e-9) + + +@pytest.mark.parametrize("axis", ["x", "y", "z"]) +def test_rot_3D_zero_angle_returns_identity(axis): + # Arrange + angle = 0.0 + expected = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] + + # Act + result = rot_3D(angle, axis) + + # Assert + assert result == expected + + +@pytest.mark.parametrize( + "axis, point, expected", + [ + ("x", [[0], [1], [0]], [0, 0, 1]), + ("y", [[0], [0], [1]], [1, 0, 0]), + ("z", [[1], [0], [0]], [0, 1, 0]), + ], +) +def test_rot_3D_rotates_point_by_90_degrees(axis, point, expected): + # Arrange + angle = math.radians(90) + + # Act + result = [row[0] for row in matmul(rot_3D(angle, axis), point)] + + # Assert + assert result == pytest.approx(expected, abs=1e-9) + + +@pytest.mark.parametrize("axis", ["x", "y", "z"]) +def test_rot_3D_returns_3x3_matrix(axis): + # Arrange + angle = 1.0 + + # Act + result = rot_3D(angle, axis) + + # Assert + assert len(result) == 3 + assert all(len(row) == 3 for row in result) + + +@pytest.mark.parametrize("invalid_axis", ["w", "", "X", "xy"]) +def test_rot_3D_invalid_axis_raises_value_error(invalid_axis): + # Arrange + angle = 1.0 + + # Act / Assert + with pytest.raises(ValueError): + rot_3D(angle, invalid_axis) \ No newline at end of file