Hausaufgabe perspektivische Projektion und Tests

This commit is contained in:
naifla103879 committed 2026-10-26 13:31:58 +01:00
1 parent 9aa475906e
commit f54fc5d4ef
2 files changed
+64 -1

No files matched your search

+37
View File
@@ -53,6 +53,35 @@ def project_ortho(
return (x * scale, y * scale) return (x * scale, y * scale)
# ============================================================
# NEU: HAUSAUFGABE – PERSPEKTIVISCHE PROJEKTION
# ============================================================
def project_perspective(
point: tuple[float, float, float],
focal_length: float = 1,
camera_distance: float = 4.0 ,
scale: float = 1,
) -> tuple[float, float]:
"""Projiziert einen 3D-Punkt perspektivisch auf die 2D-Ebene."""
x, y, z = point # NEU: 3D-Koordinaten auslesen
distance = z + camera_distance # NEU: Entfernung berechnen
if distance == 0: # NEU: Division durch null verhindern
raise ValueError("Der Punkt liegt auf der Kameraebene.")
x_new = (x * focal_length / distance) * scale # NEU: x projizieren
y_new = (y * focal_length / distance) * scale # NEU: y projizieren
return (x_new, y_new) # NEU: 2D-Koordinaten zurückgeben
# ============================================================
# ENDE DER NEUEN HAUSAUFGABE
# ============================================================
def matmul( def matmul(
a: list[list[float | int]], a: list[list[float | int]],
b: list[list[float | int]], b: list[list[float | int]],
@@ -137,3 +166,11 @@ if __name__ == "__main__":
print("\n3D-Rotation um die Z-Achse:") print("\n3D-Rotation um die Z-Achse:")
print(tabulate(rot_3D(math.pi / 2, "z"))) print(tabulate(rot_3D(math.pi / 2, "z")))
# NEU: HAUSAUFGABE – Perspektivische Projektion testen
print("\nPerspektivische Projektion:")
print(project_perspective((1.0, 2.0, 3.0)))
# NEU: HAUSAUFGABE – Perspektivische Projektion mit Skalierung testen
print("\nPerspektivische Projektion mit Skalierung 100:")
print(project_perspective((1.0, 2.0, 3.0), scale=100))
+27 -1
View File
@@ -1,5 +1,7 @@
from matrixmania.compute import project_ortho import pytest
from matrixmania.compute import project_ortho, project_perspective
def test_project_ortho_default(): def test_project_ortho_default():
@@ -24,3 +26,27 @@ def test_project_ortho_scale_100():
# Assert # Assert
assert result == expected assert result == expected
def test_project_perspective_default():
# Arrange
point = (1.0, 2.0, 3.0)
expected = (1.0 / 7.0, 2.0 / 7.0)
# Act
result = project_perspective(point)
# Assert
assert result == pytest.approx(expected)
def test_project_perspective_focal_length_2_scale_100():
# Arrange
point = (1.0, 2.0, 3.0)
expected = ((1.0 * 2.0 / 7.0) * 100, (2.0 * 2.0 / 7.0) * 100)
# Act
result = project_perspective(point, focal_length=2.0, scale=100)
# Assert
assert result == pytest.approx(expected)