diff --git a/.idea/matrixmania.iml b/.idea/matrixmania.iml index 017f321..d949aee 100644 --- a/.idea/matrixmania.iml +++ b/.idea/matrixmania.iml @@ -2,6 +2,7 @@ + diff --git a/AGENTS.md b/AGENTS.md index 79a2eba..bfe4cf8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,4 +1,7 @@ # 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 +- 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/main.py b/main.py index 912a6a6..ee55973 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,3 @@ -from spiel.game import Game - - def print_hi(name): # Use a breakpoint in the code line below to debug your script. print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. diff --git a/pyproject.toml b/pyproject.toml index 3c9e77d..547979d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,6 +3,8 @@ name = "matrixmania" version = "0.1.0" dependencies = ["pygame"] +[tool.pytest.ini_options] +pythonpath = "src" + [project.optional-dependencies] -dev = ["pytest", "ruff", - 0 ^, "pytest-cov"] \ No newline at end of file +dev = ["pytest", "ruff", "pytest-cov"] \ No newline at end of file diff --git a/src/matrixmania/__init__.py b/src/matrixmania/__init__.py new file mode 100644 index 0000000..e3601ab --- /dev/null +++ b/src/matrixmania/__init__.py @@ -0,0 +1 @@ +from .compute import matmul, transpose, rot_2D, rot_3D diff --git a/spiel/apollo.py b/src/matrixmania/apollo.py similarity index 100% rename from spiel/apollo.py rename to src/matrixmania/apollo.py diff --git a/spiel/compute.py b/src/matrixmania/compute.py similarity index 91% rename from spiel/compute.py rename to src/matrixmania/compute.py index a978c9c..6aa69d5 100644 --- a/spiel/compute.py +++ b/src/matrixmania/compute.py @@ -66,7 +66,26 @@ def rot_2D(theta: float) -> list[list[float]]: s = math.sin(theta) return [[c, -s], [s, c]] -def rot3d(angle: float, axis: str) -> list[list[float]]: +def rot_3D(angle: float, axis: str) -> list[list[float]]: + """Return a 3×3 rotation matrix for a rotation around *axis*. + + Parameters + ---------- + angle : float + Rotation angle in radians. + axis : str + Axis of rotation, one of "x", "y" or "z". + + Returns + ------- + list[list[float]] + The 3×3 rotation matrix. + + Raises + ------ + ValueError + If *axis* is not one of "x", "y" or "z". + """ c = math.cos(angle) s = math.sin(angle) if axis == "x": diff --git a/spiel/game.py b/src/matrixmania/game.py similarity index 100% rename from spiel/game.py rename to src/matrixmania/game.py diff --git a/spiel/moon.py b/src/matrixmania/moon.py similarity index 96% rename from spiel/moon.py rename to src/matrixmania/moon.py index 4a9ffaa..f77b28e 100644 --- a/spiel/moon.py +++ b/src/matrixmania/moon.py @@ -13,8 +13,8 @@ from __future__ import annotations import pygame -from spiel.compute import rot_2D, matmul -from spiel.game import Game +from src.matrixmania.compute import rot_2D, matmul +from src.matrixmania.game import Game EARTH_RADIUS = 30 diff --git a/spiel/__init__.py b/test/test_matrixmania/test_projection.py similarity index 100% rename from spiel/__init__.py rename to test/test_matrixmania/test_projection.py diff --git a/test/test_matrixmania/test_rot.py b/test/test_matrixmania/test_rot.py new file mode 100644 index 0000000..0eb0c7e --- /dev/null +++ b/test/test_matrixmania/test_rot.py @@ -0,0 +1,55 @@ +import math +import pytest + +from matrixmania import rot_2D, rot_3D + +def test_rot2D(): # gibt Rotationsmatrix für 90 grad + # Arrange + theta = math.pi / 2 # 90 Grad in Radiant + erwartet = [[0.0, -1.0], [1.0, 0.0]] + + # Act + ergebnis = rot_2D(theta) + + # Assert + assert len(ergebnis) == 2 + assert all(len(zeile) == 2 for zeile in ergebnis) + for zeile_ist, zeile_soll in zip(ergebnis, erwartet): + assert zeile_ist == pytest.approx(zeile_soll, abs=1e-9) + +@pytest.mark.parametrize( + "axis, erwartet", + [ + ("x", [[1.0, 0.0, 0.0], + [0.0, 0.0, -1.0], + [0.0, 1.0, 0.0]]), + ("y", [[ 0.0, 0.0, 1.0], + [ 0.0, 1.0, 0.0], + [-1.0, 0.0, 0.0]]), + ("z", [[0.0, -1.0, 0.0], + [1.0, 0.0, 0.0], + [0.0, 0.0, 1.0]]), + ], +) +def test_rot3D(axis, erwartet): # bei 90 Grad Rotation um jede Achse + # Arrange + angle = math.pi / 2 # 90 Grad in Radiant + + # Act + ergebnis = rot_3D(angle, axis) + + # Assert + assert len(ergebnis) == 3 + assert all(len(zeile) == 3 for zeile in ergebnis) + for zeile_ist, zeile_soll in zip(ergebnis, erwartet): + assert zeile_ist == pytest.approx(zeile_soll, abs=1e-9) + + +def test_rot3D_ungueltige_achse(): # wirft ValueError bei ungültiger Achse + # Arrange + angle = math.pi / 2 + axis = "w" + + # Act & Assert + with pytest.raises(ValueError, match="Unsupported rotation axis"): + rot_3D(angle, axis)