diff --git a/src/matrixmania/__init__.py b/src/matrixmania/__init__.py index 224ed5f..8358f34 100644 --- a/src/matrixmania/__init__.py +++ b/src/matrixmania/__init__.py @@ -1,5 +1,5 @@ """MatrixMania - Paket für Matrixoperationen.""" -from .compute import matmul, rot_2d, rot_3d, transpose +from .compute import matmul, project_ortho, rot_2d, rot_3d, transpose -__all__ = ["matmul", "rot_2d", "rot_3d", "transpose"] +__all__ = ["matmul", "project_ortho", "rot_2d", "rot_3d", "transpose"] diff --git a/src/matrixmania/compute.py b/src/matrixmania/compute.py index 8e27a0d..d0cff61 100644 --- a/src/matrixmania/compute.py +++ b/src/matrixmania/compute.py @@ -132,6 +132,22 @@ def rot_3d(angle: float, axis: str) -> list[list[float]]: ] +def project_ortho( + point: tuple[float, float, float], scale: float = 1 +) -> tuple[float, float]: + """Projiziert einen 3D-Punkt orthogonal auf eine 2D-Ebene und skaliert ihn. + + Die z-Koordinate wird ignoriert und die x- und y-Koordinaten werden + mit dem Skalierungsfaktor multipliziert. + + :param point: Der 3D-Punkt als Tupel (x, y, z). + :param scale: Der Skalierungsfaktor für die Projektion (Standardwert 1). + :return: Der skalierte 2D-Punkt als Tupel (x, y). + """ + x, y, _ = point + return x * scale, y * scale + + def test_functions() -> None: """Testet die mathematischen Hilfsfunktionen für Matrixoperationen.""" a1 = [ @@ -266,6 +282,13 @@ def test_functions() -> None: except ValueError: print("Test 13 (rot_3d ungültige Achse) erfolgreich abgefangen!") + # Test 14: project_ortho + projected = project_ortho((0.5, -0.5, 10.0), scale=200) + assert projected == (100.0, -100.0), ( + f"Fehler in project_ortho: Erwartet (100.0, -100.0), erhalten {projected}" + ) + print("Test 14 (project_ortho) erfolgreich bestanden!") + if __name__ == "__main__": print(tabulate(rot_2d(1.0))) diff --git a/test/test_matrixmania/test_projection.py b/test/test_matrixmania/test_projection.py new file mode 100644 index 0000000..2242f34 --- /dev/null +++ b/test/test_matrixmania/test_projection.py @@ -0,0 +1,28 @@ +"""Unit-Tests für orthogonale Projektionen in compute.py.""" + +from matrixmania.compute import project_ortho + + +def test_project_ortho_default_scale() -> None: + """Testet die orthogonale Projektion mit Standard-Skalierungsfaktor.""" + # Arrange + point = (1.0, 2.0, 3.0) + + # Act + result = project_ortho(point) + + # Assert + assert result == (1.0, 2.0) + + +def test_project_ortho_scaled() -> None: + """Testet die orthogonale Projektion mit einem Skalierungsfaktor von 100.""" + # Arrange + point = (1.0, 2.0, 3.0) + scale = 100 + + # Act + result = project_ortho(point, scale=scale) + + # Assert + assert result == (100.0, 200.0)