diff --git a/compute.py b/compute.py index bd0db1a..976853d 100644 --- a/compute.py +++ b/compute.py @@ -1,4 +1,5 @@ """Utility functions for matrix operations.""" + from __future__ import annotations import math @@ -7,6 +8,7 @@ from tabulate import tabulate Matrix = list[list[int | float]] + def transpose(matrix: Matrix) -> Matrix: """Return the transpose of a matrix. @@ -84,6 +86,36 @@ def rot_2D(theta: float) -> Matrix: ] +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 die x-y-Ebene. + + x_2d = x * focal_length / (z + camera_distance) * scale + y_2d = y * focal_length / (z + camera_distance) * scale + + Args: + point: Der 3D-Punkt (x, y, z). + focal_length: Brennweite, steuert die Stärke der Perspektive. + scale: Skalierungsfaktor für den projizierten Punkt. + camera_distance: Offset für die Kameraposition auf der z-Achse. + + Returns: + Der 2D-Punkt (x_2d, y_2d). + + Raises: + ValueError: Wenn z + camera_distance gleich 0 ist. + """ + x, y, z = point + depth = z + camera_distance + if depth == 0: + raise ValueError("z + camera_distance darf nicht 0 sein") + return (x * focal_length / depth * scale, y * focal_length / depth * scale) + + if __name__ == "__main__": matrix_a = [ [3, 4, -1, 4], @@ -114,4 +146,4 @@ if __name__ == "__main__": assert math.isclose(rotated[1][0], 1.0, abs_tol=1e-9) assert rot_2D(0) == [[1.0, 0.0], [0.0, 1.0]] print(tabulate(rot_2D(1.0))) - print("Alle Tests bestanden.") \ No newline at end of file + print("Alle Tests bestanden.") diff --git a/pyproject.toml b/pyproject.toml index d9f90c0..21c4bf1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,4 +4,7 @@ version = "0.1.0" dependencies = ["tabulate", "ruff"] [project.optional-dependencies] -dev = ["pytest", "ruff", "pytest-cov"] \ No newline at end of file +dev = ["pytest", "ruff", "pytest-cov"] + +[tool.pytest.ini_options] +pythonpath = ["."] \ No newline at end of file