diff --git a/src/matrixmania/__init__.py b/src/matrixmania/__init__.py index 57ef826..ca93196 100644 --- a/src/matrixmania/__init__.py +++ b/src/matrixmania/__init__.py @@ -1,2 +1,2 @@ -from .compute import matmul, transpose, rot_2D, rot_3D -from .projection import project_ortho +from .compute import matmul, rot_2D, rot_3D, transpose +from .projection import project_ortho, project_perspective diff --git a/src/matrixmania/apollo.py b/src/matrixmania/apollo.py index ed34b06..d064b83 100644 --- a/src/matrixmania/apollo.py +++ b/src/matrixmania/apollo.py @@ -1,8 +1,7 @@ from __future__ import annotations import pygame - -from compute import rot_2D, matmul +from compute import matmul, rot_2D from moon import Moon # Apollo visual parameters diff --git a/src/matrixmania/compute.py b/src/matrixmania/compute.py index 6aa69d5..0efdef9 100644 --- a/src/matrixmania/compute.py +++ b/src/matrixmania/compute.py @@ -1,7 +1,7 @@ from __future__ import annotations -from collections.abc import Sequence import math +from collections.abc import Sequence Matrix = Sequence[Sequence[float]] diff --git a/src/matrixmania/moon.py b/src/matrixmania/moon.py index f77b28e..c144605 100644 --- a/src/matrixmania/moon.py +++ b/src/matrixmania/moon.py @@ -13,10 +13,9 @@ from __future__ import annotations import pygame -from src.matrixmania.compute import rot_2D, matmul +from src.matrixmania.compute import matmul, rot_2D from src.matrixmania.game import Game - EARTH_RADIUS = 30 MOON_RADIUS = 10 BLACK = (0, 0, 0) diff --git a/src/matrixmania/projection.py b/src/matrixmania/projection.py index 9bc0a7c..a3788a4 100644 --- a/src/matrixmania/projection.py +++ b/src/matrixmania/projection.py @@ -9,4 +9,10 @@ def project_ortho(point: tuple[float, float, float], scale: float = 1) -> tuple[ Scaling factor applied (default 1) """ x, y, _ = point - return (x * scale, y * scale) \ No newline at end of file + 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]: + x, y, z = point + x_2d = x * focal_length / (z + camera_distance) * scale + y_2d = y * focal_length / (z + camera_distance) * scale + return (x_2d, y_2d) \ No newline at end of file diff --git a/test/test_matrixmania/test_projection.py b/test/test_matrixmania/test_projection.py index 417080b..ffd6f5f 100644 --- a/test/test_matrixmania/test_projection.py +++ b/test/test_matrixmania/test_projection.py @@ -1,6 +1,7 @@ import pytest -from matrixmania import project_ortho +from matrixmania import project_ortho, project_perspective + def test_project_ortho_ohne_skalierung(): # Arrange @@ -13,7 +14,6 @@ def test_project_ortho_ohne_skalierung(): # Assert assert ergebnis == pytest.approx(erwartet) - def test_project_ortho_mit_skalierung_100(): # Arrange punkt = (1.0, 2.0, 3.0) @@ -26,3 +26,26 @@ def test_project_ortho_mit_skalierung_100(): # Assert assert ergebnis == pytest.approx(erwartet) #würde hier auch ohne approx gehen + +def test_project_perspective_ohne_skalierung(): + # Arrange + punkt = (1.0, 2.0, 3.0) + erwartet = (1 / 7, 2 / 7) # 1*1/7*1 und 2*1/7*1 + + # Act + ergebnis = project_perspective(punkt, scale=1, focal_length=1) + + # Assert + assert ergebnis == pytest.approx(erwartet) + + +def test_project_perspective_mit_skalierung_und_brennweite(): + # Arrange + punkt = (1.0, 2.0, 3.0) + erwartet = (200 / 7, 400 / 7) # 1*2/7*100 und 2*2/7*100 + + # Act + ergebnis = project_perspective(punkt, scale=100, focal_length=2) + + # Assert + assert ergebnis == pytest.approx(erwartet) \ No newline at end of file diff --git a/test/test_matrixmania/test_rot.py b/test/test_matrixmania/test_rot.py index caa7b05..ba8d24c 100644 --- a/test/test_matrixmania/test_rot.py +++ b/test/test_matrixmania/test_rot.py @@ -1,7 +1,10 @@ -import math, pytest +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