project_perspective ohne Tests

This commit is contained in:
Lena committed 2026-10-28 22:07:49 +01:00
1 parent beb61664ff
commit 0a4c0f7da3
1 file changed
+35
+35
View File
@@ -138,6 +138,7 @@ def rotate(matrix: Matrix, degrees: float) -> Matrix:
-------
Matrix
"""
if not matrix:
return []
n_rows, n_cols = len(matrix), len(matrix[0])
@@ -187,6 +188,7 @@ def rot_3D(angle: float, axis: str) -> list[list[float]]:
ValueError
If *axis* is not one of "x", "y" or "z".
"""
import math
c = math.cos(angle)
@@ -217,10 +219,43 @@ def project_ortho(
tuple[float, float]
The projected (x, y) after scaling.
"""
x, y, _ = point
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]:
"""Lässt entfernte Punkte kleiner erscheinen.
Parameters
----------
point : tuple[float, float, float]
Coordinates (x, y, z) of the point.
focal_length: float
Wie stark die Perspektive sichtbar ist (wie Brennweite)
scale : float, optional
Scaling factor applied to the projected x and y.
camera_distance: float
Entfernung der Kamera
Returns
-------
tuple[float, float]
The projected (x, y) after sizing.
"""
x2d = ((point[0] * focal_length) / (point[2] + camera_distance)) * scale
y2d = ((point[1] * focal_length) / (point[2] + camera_distance)) * scale
return (x2d, y2d)
if __name__ == "__main__":
# matmul tests
a1 = [[1, 2], [3, 4]]