Files
PROG2A/aufgabenblatt_3/wuerfel/spiel/compute.py
T
2026-10-28 21:15:33 +01:00

137 lines
4.4 KiB
Python

"""Funktionen zur linearen Algebra auf Matrizen als verschachtelte Listen."""
import math
def matmul(
matrix_a: list[list[float]], matrix_b: list[list[float]]
) -> list[list[float]]:
"""
Multipliziert zwei Matrizen miteinander (A * B).
:param matrix_a: Linke Matrix mit m Zeilen und n Spalten
:param matrix_b: Rechte Matrix mit n Zeilen und p Spalten
:return: Ergebnismatrix mit m Zeilen und p Spalten
:raises ValueError: Wenn die Spaltenzahl von A nicht der Zeilenzahl von B entspricht
"""
if len(matrix_a[0]) != len(matrix_b):
raise ValueError("Spaltenzahl von A muss der Zeilenzahl von B entsprechen.")
result = []
for row in matrix_a:
new_row = []
for col_index in range(len(matrix_b[0])):
value = 0
for k in range(len(row)):
value += row[k] * matrix_b[k][col_index]
new_row.append(value)
result.append(new_row)
return result
def transpose(matrix: list[list[float]]) -> list[list[float]]:
"""
Transponiert eine Matrix: Zeilen werden zu Spalten und Spalten zu Zeilen.
:param matrix: Matrix mit m Zeilen und n Spalten
:return: Transponierte Matrix mit n Zeilen und m Spalten
:raises ValueError: Bei leerer Matrix oder unterschiedlich langen Zeilen
"""
if not matrix or not matrix[0]:
raise ValueError("Die Matrix darf nicht leer sein.")
column_count = len(matrix[0])
for row in matrix:
if len(row) != column_count:
raise ValueError("Alle Zeilen müssen gleich viele Spalten haben.")
result = []
for col_index in range(column_count):
new_row = []
for row in matrix:
new_row.append(row[col_index])
result.append(new_row)
return result
def rot_2D(angle: float) -> list[list[float]]:
"""
Liefert die 2D-Rotationsmatrix für eine Drehung um den Ursprung.
:param angle: Drehwinkel im Bogenmaß (gegen den Uhrzeigersinn)
:return: 2x2-Rotationsmatrix [[cos, -sin], [sin, cos]]
"""
return [
[math.cos(angle), -math.sin(angle)],
[math.sin(angle), math.cos(angle)],
]
def rot_3D(angle: float, axis: str) -> list[list[float]]:
"""
Liefert die 3D-Rotationsmatrix für eine Drehung um eine Koordinatenachse.
Formeln (θ = angle):
- x-Achse: [[1, 0, 0], [0, cos θ, -sin θ], [0, sin θ, cos θ]]
- y-Achse: [[cos θ, 0, sin θ], [0, 1, 0], [-sin θ, 0, cos θ]]
- z-Achse: [[cos θ, -sin θ, 0], [sin θ, cos θ, 0], [0, 0, 1]]
:param angle: Drehwinkel im Bogenmaß
:param axis: Drehachse, "x", "y" oder "z"
:return: 3x3-Rotationsmatrix
:raises ValueError: Wenn axis nicht "x", "y" oder "z" ist
"""
cos = math.cos(angle)
sin = math.sin(angle)
if axis == "x":
return [[1, 0, 0], [0, cos, -sin], [0, sin, cos]]
if axis == "y":
return [[cos, 0, sin], [0, 1, 0], [-sin, 0, cos]]
if axis == "z":
return [[cos, -sin, 0], [sin, cos, 0], [0, 0, 1]]
raise ValueError(f"Ungültige Achse '{axis}', erlaubt sind 'x', 'y' und 'z'.")
def project_ortho(
point: tuple[float, float, float], scale: float = 1
) -> tuple[float, float]:
"""
Projiziert einen 3D-Punkt orthogonal auf die xy-Ebene.
Die z-Koordinate wird ignoriert, x und y werden mit scale multipliziert.
:param point: 3D-Punkt (x, y, z)
:param scale: Skalierungsfaktor für das Ergebnis
:return: 2D-Punkt (x * scale, y * scale)
"""
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]:
"""
Projiziert einen 3D-Punkt perspektivisch auf die Bildschirmebene.
Formel: x' = x * focal_length / (z + camera_distance) * scale,
y' entsprechend.
:param point: 3D-Punkt (x, y, z)
:param focal_length: Brennweite, steuert die Stärke der Perspektive
:param scale: Skalierungsfaktor für das Ergebnis
:param camera_distance: Abstand der Kamera zum Ursprung
:return: 2D-Punkt (x', y')
:raises ValueError: Wenn der Punkt auf Höhe oder hinter der Kamera liegt
"""
x, y, z = point
depth = z + camera_distance
if depth <= 0:
raise ValueError("Der Punkt liegt auf Höhe oder hinter der Kamera.")
factor = focal_length / depth * scale
return (x * factor, y * factor)