diff --git a/compute.py b/compute.py index 24f7d15..5ff4e82 100644 --- a/compute.py +++ b/compute.py @@ -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]]