47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
class Particle:
|
|
def __init__(self, position, velocity=(0, 0), acceleration=(0, 0)):
|
|
self.position = position
|
|
self.velocity = velocity
|
|
self.acceleration = acceleration
|
|
|
|
def update(self):
|
|
"""Erstellt ein neues Particle-Objekt mit aktualisiertem Zustand."""
|
|
new_velocity = (
|
|
self.velocity[0] + self.acceleration[0],
|
|
self.velocity[1] + self.acceleration[1]
|
|
)
|
|
new_position = (
|
|
self.position[0] + new_velocity[0],
|
|
self.position[1] + new_velocity[1]
|
|
)
|
|
return Particle(new_position, new_velocity, self.acceleration)
|
|
|
|
|
|
# Simulation mit Historie (Immutable State erlaubt Undo)
|
|
history = []
|
|
|
|
# Initialzustand (Start bei (0, 0) mit Beschleunigung (1, 2))
|
|
p0 = Particle(position=(0, 0), velocity=(0, 0), acceleration=(1, 2))
|
|
history.append(p0)
|
|
|
|
# 1. Update
|
|
p1 = history[-1].update()
|
|
history.append(p1)
|
|
|
|
# 2. Update
|
|
p2 = history[-1].update()
|
|
history.append(p2)
|
|
|
|
# Überprüfen der Zwischenzustände
|
|
assert history[0].position == (0, 0)
|
|
assert history[1].position == (1, 2)
|
|
assert history[2].position == (3, 6)
|
|
|
|
# UNDO-Schritte ausführen (Einfach das jeweils letzte Element entfernen)
|
|
history.pop() # Undo von Schritt 2
|
|
assert history[-1].position == (1, 2)
|
|
|
|
history.pop() # Undo von Schritt 1
|
|
assert history[-1].position == (0, 0)
|
|
|
|
print("Undo-Tests für Particle erfolgreich bestanden!") |