7 commit hausaufgabe
This commit is contained in:
1 parent
83c81f8566
commit
6e76e97a4b
3 files changed
+43
-2
No files matched your search
@@ -233,3 +233,16 @@ if __name__ == "__main__":
|
|||||||
print(rot_2D(theta))
|
print(rot_2D(theta))
|
||||||
print(rot_2D(1.0))
|
print(rot_2D(1.0))
|
||||||
|
|
||||||
|
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]:
|
||||||
|
"""Projiziert einen 3D-Punkt perspektivisch auf eine 2D-Ebene."""
|
||||||
|
x, y, z = point
|
||||||
|
depth = z + camera_distance
|
||||||
|
if depth == 0:
|
||||||
|
raise ValueError("z + camera_distance darf nicht 0 sein.")
|
||||||
|
x_2d = x * focal_length / depth * scale
|
||||||
|
y_2d = y * focal_length / depth * scale
|
||||||
|
return (x_2d, y_2d)
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from matrixmania.compute import project_ortho
|
from matrixmania.compute import project_ortho, project_perspective
|
||||||
|
|
||||||
|
|
||||||
def test_project_ortho_default():
|
def test_project_ortho_default():
|
||||||
@@ -9,7 +9,35 @@ def test_project_ortho_default():
|
|||||||
assert project_ortho(point) == (1.0, 2.0)
|
assert project_ortho(point) == (1.0, 2.0)
|
||||||
|
|
||||||
|
|
||||||
def test_project_ortho_scaled():
|
def test_project_perspective_default():
|
||||||
|
"""Test project_perspective with default scale and focal_length=1"""
|
||||||
|
# Arrange
|
||||||
|
point = (1.0, 2.0, 3.0)
|
||||||
|
# Act
|
||||||
|
result = project_perspective(point)
|
||||||
|
# depth = z + camera_distance = 3 + 4 = 7
|
||||||
|
# Expected x = 1 * 1 / 7 * 1 = 1/7
|
||||||
|
# Expected y = 2 * 1 / 7 * 1 = 2/7
|
||||||
|
expected = (1/7, 2/7)
|
||||||
|
# Assert
|
||||||
|
assert result == pytest.approx(expected)
|
||||||
|
|
||||||
|
|
||||||
|
def test_project_perspective_scaled():
|
||||||
|
"""Test project_perspective with scale=100 and focal_length=2"""
|
||||||
|
# Arrange
|
||||||
|
point = (1.0, 2.0, 3.0)
|
||||||
|
scale = 100
|
||||||
|
focal_length = 2
|
||||||
|
# Act
|
||||||
|
result = project_perspective(point, focal_length=focal_length, scale=scale)
|
||||||
|
# depth = 3 + 4 = 7
|
||||||
|
# Expected x = 1 * 2 / 7 * 100 = 200/7
|
||||||
|
# Expected y = 2 * 2 / 7 * 100 = 400/7
|
||||||
|
expected = (200/7, 400/7)
|
||||||
|
# Assert
|
||||||
|
assert result == pytest.approx(expected)
|
||||||
|
|
||||||
"""Projektionsfunktion mit Skalierung"""
|
"""Projektionsfunktion mit Skalierung"""
|
||||||
point = (1.0, 2.0, 3.0)
|
point = (1.0, 2.0, 3.0)
|
||||||
assert project_ortho(point, scale=100) == (100.0, 200.0)
|
assert project_ortho(point, scale=100) == (100.0, 200.0)
|
||||||
Reference in new issue
Block a user