from context import * from random import gauss import psyco psyco.full() size(600, 600) translate(width()/2.0, height()/2.0) system = [] def gr(x): return gauss(0, x) def setup(): background(0, 0.15, 0.3) class Body: def __init__(self, x, y, vx, vy, mass): self.x = x self.y = y self.vx = vx self.vy = vy self.mass = 100 self.r = self.mass**0.5 # self.history = [Point(x, y)] def distance(self, other): return (abs(self.x - other.x)**2 + abs(self.y - other.y)**2)**0.5 def update(self): self.x += self.vx self.y += self.vy for b in system: if b != self: df = - 1/(self.distance(b)**2)*10 self.x += df * (self.x - b.x) self.y += df * (self.y - b.y) # self.history.append(Point(self.x, self.y)) stroke(0) fill(1) oval(self.x, self.y, self.r, self.r) # for pt in self.history: # oval(pt.x, pt.y, 0.1, 0.1) # if self.x > width() or self.x < 0 or self.y > height() or self.y < 0: # system.remove(self) for x in range(100): system.append(Body(random(width()), random(height()), gr(0.1), gr(0.1), random(1000))) def draw(): gx = -1 gy = 0 canvas.clear() for b in system: b.update() v = 0 for ball in system: v += abs(ball.vx) + abs(ball.vy) # print len(system), log(v, 2) l = layer() canvas.fps = 6 canvas.setup = setup canvas.draw = draw run() #from context import profile #profile()