lol
This commit is contained in:
1 parent
3696afaeec
commit
d633de0293
2 files changed
+37
-2
No files matched your search
+33
-1
@@ -1,4 +1,5 @@
|
|||||||
"""Utility functions for matrix operations."""
|
"""Utility functions for matrix operations."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import math
|
import math
|
||||||
@@ -7,6 +8,7 @@ from tabulate import tabulate
|
|||||||
|
|
||||||
Matrix = list[list[int | float]]
|
Matrix = list[list[int | float]]
|
||||||
|
|
||||||
|
|
||||||
def transpose(matrix: Matrix) -> Matrix:
|
def transpose(matrix: Matrix) -> Matrix:
|
||||||
"""Return the transpose of a 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__":
|
if __name__ == "__main__":
|
||||||
matrix_a = [
|
matrix_a = [
|
||||||
[3, 4, -1, 4],
|
[3, 4, -1, 4],
|
||||||
@@ -114,4 +146,4 @@ if __name__ == "__main__":
|
|||||||
assert math.isclose(rotated[1][0], 1.0, abs_tol=1e-9)
|
assert math.isclose(rotated[1][0], 1.0, abs_tol=1e-9)
|
||||||
assert rot_2D(0) == [[1.0, 0.0], [0.0, 1.0]]
|
assert rot_2D(0) == [[1.0, 0.0], [0.0, 1.0]]
|
||||||
print(tabulate(rot_2D(1.0)))
|
print(tabulate(rot_2D(1.0)))
|
||||||
print("Alle Tests bestanden.")
|
print("Alle Tests bestanden.")
|
||||||
+4
-1
@@ -4,4 +4,7 @@ version = "0.1.0"
|
|||||||
dependencies = ["tabulate", "ruff"]
|
dependencies = ["tabulate", "ruff"]
|
||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
dev = ["pytest", "ruff", "pytest-cov"]
|
dev = ["pytest", "ruff", "pytest-cov"]
|
||||||
|
|
||||||
|
[tool.pytest.ini_options]
|
||||||
|
pythonpath = ["."]
|
||||||
Reference in new issue
Block a user