From 72d00b4c01035a32a0f38905d434769d45b6ad20 Mon Sep 17 00:00:00 2001 From: Amalia Sucipto Date: Thu, 29 Oct 2026 11:11:19 +0100 Subject: [PATCH] Add perspective projection and tests --- compute.py | 40 ++++++++++++++++++++++-- test_matrixmania/test_projection.py | 48 +++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/compute.py b/compute.py index ee4d6da..8625f88 100644 --- a/compute.py +++ b/compute.py @@ -1,5 +1,6 @@ -from typing import TypeAlias from math import cos, sin +from typing import TypeAlias + Number: TypeAlias = int | float Matrix: TypeAlias = list[list[Number]] @@ -192,6 +193,38 @@ def project_ortho( return (x * scale, y * scale) +def project_perspective( + point: tuple[float, float, float], + focal_length: float = 1.0, + scale: float = 1.0, + camera_distance: float = 4.0, +) -> tuple[float, float]: + """Project a 3D point onto a 2D plane using perspective. + + Args: + point: The 3D point containing x, y, and z coordinates. + focal_length: Controls the strength of the perspective. + scale: Scaling factor for the projected coordinates. + camera_distance: Distance offset of the camera. + + Returns: + The projected 2D coordinates. + + Raises: + ValueError: If the projection denominator is zero. + """ + x, y, z = point + + depth = z + camera_distance + + if depth == 0: + raise ValueError("Projection depth must not be zero.") + + projected_x = x * focal_length / depth * scale + projected_y = y * focal_length / depth * scale + + return (projected_x, projected_y) + if __name__ == "__main__": # Beispiel Multiplikation @@ -215,4 +248,7 @@ if __name__ == "__main__": expected = [[1, 4], [2, 5], [3, 6]] assert transpose(matrix) == expected - print("Transponierung erfolgreich:", transpose(matrix)) \ No newline at end of file + print("Transponierung erfolgreich:", transpose(matrix)) + + + diff --git a/test_matrixmania/test_projection.py b/test_matrixmania/test_projection.py index c995459..e403a4a 100644 --- a/test_matrixmania/test_projection.py +++ b/test_matrixmania/test_projection.py @@ -1,4 +1,6 @@ -from compute import project_ortho +import pytest + +from compute import project_ortho, project_perspective def test_project_ortho_without_scaling() -> None: @@ -25,4 +27,46 @@ def test_project_ortho_with_scaling() -> None: result = project_ortho(point, scale) # Assert - assert result == expected \ No newline at end of file + assert result == expected + +def test_project_perspective_without_scaling() -> None: + """Test perspective projection with default parameters.""" + # Arrange + point = (1.0, 2.0, 3.0) + expected = (1 / 7, 2 / 7) + + # Act + result = project_perspective(point) + + # Assert + assert result == pytest.approx(expected) + + +def test_project_perspective_with_scaling() -> None: + """Test perspective projection with scaling and focal length.""" + # Arrange + point = (1.0, 2.0, 3.0) + focal_length = 2.0 + scale = 100.0 + expected = (200 / 7, 400 / 7) + + # Act + result = project_perspective( + point, + focal_length=focal_length, + scale=scale, + ) + + # Assert + assert result == pytest.approx(expected) + +def test_project_perspective_zero_depth() -> None: + """Test that zero projection depth raises an error.""" + # Arrange + point = (1.0, 2.0, -4.0) + + # Act & Assert + with pytest.raises(ValueError): + project_perspective(point) + +# python -m pytest test_matrixmania/test_projection.py -v \ No newline at end of file