34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
class Particle:
|
|
def __init__(self, mass=1.0, velocity=None, position=None):
|
|
self.mass = mass
|
|
self.velocity = list(velocity if velocity is not None else [0.0, 0.0, 0.0])
|
|
self.position = list(position if position is not None else [0.0, 0.0, 0.0])
|
|
|
|
def __repr__(self):
|
|
return f"Particle(mass={self.mass}, velocity={self.velocity}, position={self.position})"
|
|
|
|
|
|
class cloud:
|
|
def __init__(self, particles=None):
|
|
self.particles = list(particles or [])
|
|
|
|
def add(self, p):
|
|
self.particles.append(p)
|
|
|
|
def step(self, dt=1.0):
|
|
for p in self.particles:
|
|
p.position = [x + v * dt for x, v in zip(p.position, p.velocity)]
|
|
return self
|
|
|
|
def center(self):
|
|
total = sum(p.mass for p in self.particles)
|
|
if total == 0:
|
|
return [0.0, 0.0, 0.0]
|
|
answer = [0.0, 0.0, 0.0]
|
|
for p in self.particles:
|
|
answer = [a + p.mass * x for a, x in zip(answer, p.position)]
|
|
return [x / total for x in answer]
|
|
|
|
def __repr__(self):
|
|
return f"cloud({self.particles})"
|