commit a2b388ed54a607456df8ebc66a38399301244f02 Author: Alina Bhatti Date: Thu Oct 30 20:05:43 2025 +0100 Initial commit: Moon project setup diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ac1444 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +__pycache__/ +.venv/ +*.pyc +*.pyo +*.log +.vscode/ +.idea/ + diff --git a/Matrixmultiplikation.py b/Matrixmultiplikation.py new file mode 100644 index 0000000..86ab318 --- /dev/null +++ b/Matrixmultiplikation.py @@ -0,0 +1,40 @@ +from typing import List +import math + +def matmul(mat_a: List[List[float]], mat_b: List[List[float]]) -> List[List[float]]: + m, n = len(mat_a), len(mat_a[0]) + n2, p = len(mat_b), len(mat_b[0]) + + # Dimension prüfen: Spaltenzahl von A == Zeilenzahl von B + if n != n2: + raise ValueError('Matrix dimensions do not match') + + result = [] + for i in range(m): # Zeilen von A + line = [] + for j in range(p): # Spalten von B + s = 0.0 + for k in range(n): # Spalten von A / Zeilen von B + s += mat_a[i][k] * mat_b[k][j] # <--- wichtig: b[k][j] + line.append(s) + result.append(line) + return result + + +if __name__ == "__main__": + matrix_a = [ + [3, 4, -1, 4], + [-2, 2, 5, 1] + ] + + matrix_b = [ + [1, 3, -2], + [2, 5, 1], + [-1, 4, -4], + [2, 3, 6] + ] + + matrix_c = matmul(matrix_a, matrix_b) + print("Ergebnis C = A * B:") + for row in matrix_c: + print(row) diff --git a/ProductÜA21.10.py b/ProductÜA21.10.py new file mode 100644 index 0000000..053c780 --- /dev/null +++ b/ProductÜA21.10.py @@ -0,0 +1,17 @@ +from typing import List +import math + +def product(numbers: list[float]) -> float: + + if not numbers: + raise ValueError('Die Liste darf nicht leer sein ') + result= 1.0 + for n in numbers: + if not isinstance(n, (float, int, str)): + raise ValueError('Ungültig') + result *= n + return result + + +if __name__ == "__main__": + print(product([2, 3, 4])) diff --git a/README.md b/README.md new file mode 100644 index 0000000..7eeafb0 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Moon – Animation der Mondumlaufbahn +Dieses Projekt zeigt eine einfache Pygame-Animation, +bei der der Mond die Erde umkreist. +Alle mathematischen Berechnungen erfolgen in compute.py. diff --git a/SubstringÜA21.10.py b/SubstringÜA21.10.py new file mode 100644 index 0000000..9c94931 --- /dev/null +++ b/SubstringÜA21.10.py @@ -0,0 +1,17 @@ +def subject(source:str, start_index:int, symbol_count:int=None)-> str: + if start_index > len(source): + raise ValueError('start_index must be smaller than the length of source') + if symbol_count is None: + symbol_count = len(source)-start_index + if start_index+symbol_count > len(source): + raise ValueError('start_index must be smaller than the length of source') + if start_index < 0: + raise ValueError('start_index must be positive') + + return source[start_index:start_index+symbol_count] + + +if __name__ == '__main__': + original = 'GEEKSFORGEEKS' + print(subject(original, 0, 5)) + print(subject(original, 5)) \ No newline at end of file diff --git a/Transponieren einer Matrix.py b/Transponieren einer Matrix.py new file mode 100644 index 0000000..7d686cb --- /dev/null +++ b/Transponieren einer Matrix.py @@ -0,0 +1 @@ +from typing import List diff --git a/compute.py b/compute.py new file mode 100644 index 0000000..59a59b0 --- /dev/null +++ b/compute.py @@ -0,0 +1,43 @@ +from typing import List, Tuple +import math + +Matrix = List[List[float]] +Vector = Tuple[float, float] + +def transpose(m: Matrix) -> Matrix: + if not m or not m[0]: + raise ValueError("Matrix must be non-empty") + return [list(row) for row in zip(*m)] + +def matmul(a: Matrix, b: Matrix) -> Matrix: #Matrixmultiplikation# + if not a or not b or not a[0] or not b[0]: + raise ValueError("Matrices must be non-empty") + m, n = len(a), len(a[0]) + n2, p = len(b), len(b[0]) + if n != n2: + raise ValueError("Incompatible shapes") + c: Matrix = [[0.0 for _ in range(p)] for _ in range(m)] + for i in range(m): + for k in range(n): + a_ik = float(a[i][k]) + for j in range(p): + c[i][j] += a_ik * float(b[k][j]) +def rotation_matrix(theta: float) -> Matrix: + """2D-Rotationsmatrix R(theta).""" + c, s = math.cos(theta), math.sin(theta) + return [[c, -s], + [s, c]] + +def apply_mat_to_vec(m: Matrix, v: Vector) -> Vector: + """Wendet eine 2×2-Matrix auf einen 2D-Vektor (als Spaltenvektor) an.""" + if len(m) != 2 or len(m[0]) != 2: + raise ValueError("Matrix must be 2x2 for 2D vector transform") + x, y = float(v[0]), float(v[1]) + x2 = m[0][0]*x + m[0][1]*y + y2 = m[1][0]*x + m[1][1]*y + return (x2, y2) + +def add(v1: Vector, v2: Vector) -> Vector: + """Vektor-Addition v1 + v2.""" + return (v1[0] + v2[0], v1[1] + v2[1]) + diff --git a/game.py b/game.py new file mode 100644 index 0000000..17be62b --- /dev/null +++ b/game.py @@ -0,0 +1,39 @@ +# game.py (nur als Notfall-Mock; bitte euer Original verwenden!) +import pygame, time + +class Game: + def __init__(self, width=800, height=600, fps=60, title="Game"): + pygame.init() + self.width, self.height, self.fps = width, height, fps + self.screen = pygame.display.set_mode((width, height)) + pygame.display.set_caption(title) + self.clock = pygame.time.Clock() + self.running = True + + def update(self, dt: float) -> None: + pass + + def draw(self, screen: pygame.Surface) -> None: + pass + + def run(self): + prev = time.perf_counter() + while self.running: + # Events + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.running = False + + # Delta-Zeit + now = time.perf_counter() + dt = now - prev + prev = now + + # Update + Draw + self.update(dt) + self.draw(self.screen) + + pygame.display.flip() + self.clock.tick(self.fps) + + pygame.quit() diff --git a/moon.py b/moon.py new file mode 100644 index 0000000..12f6424 --- /dev/null +++ b/moon.py @@ -0,0 +1,57 @@ +# moon.py +import math +import pygame +from compute import rotation_matrix, apply_mat_to_vec, add +from game import Game # <- euer Framework aus Semester 1 + +class Moon(Game): + """ + Animation: Blauer Kreis (Erde) im Ursprung, weißer Kreis (Mond) kreist darum. + Mathe (Rotation etc.) liegt komplett in compute.py. + """ + def __init__(self, width: int = 800, height: int = 600, fps: int = 60): + super().__init__(width, height, fps, title="Moon – Orbit Demo") + + # „Ursprung“ = Bildschirmmitte (damit Erde bei (0,0) in unserer Welt liegt) + self.center = (width // 2, height // 2) + + # Darstellung + self.earth_radius = 32 + self.moon_radius = 12 + self.orbit_radius = 160 # Abstand Erde–Mond in Pixeln (optisch ansprechend) + + # Bewegung + self.angular_speed = 0.8 # rad/s (Geschwindigkeit der Umlaufbahn) + self.angle = 0.0 # aktueller Winkel in rad + + # Farben + self.bg_color = (10, 10, 25) + self.earth_color = (60, 120, 255) # blau + self.moon_color = (245, 245, 245) # weiß + + def update(self, dt: float) -> None: + """Zeitentwicklung: Winkel erhöhen (dt in Sekunden).""" + self.angle = (self.angle + self.angular_speed * dt) % (2 * math.pi) + + def draw(self, screen: pygame.Surface) -> None: + """Szene rendern.""" + screen.fill(self.bg_color) + + # Erde im „Ursprung“ (hier: Bildschirmmitte) + pygame.draw.circle(screen, self.earth_color, self.center, self.earth_radius) + + # Startvektor des Mondes (auf x-Achse, Länge = orbit_radius) + v0 = (float(self.orbit_radius), 0.0) + + # Rotierter Vektor: v = R(angle) * v0 + R = rotation_matrix(self.angle) + v_rot = apply_mat_to_vec(R, v0) + + # In Bildschirmkoordinaten verschieben: (0,0) Welt -> center Screen + pos = add(self.center, v_rot) + + # Mond zeichnen + pygame.draw.circle(screen, self.moon_color, (int(pos[0]), int(pos[1])), self.moon_radius) + +if __name__ == "__main__": + Moon().run() diff --git a/pip b/pip new file mode 100644 index 0000000..e69de29