from __future__ import print_function import sys import numpypy # if using stock python, use plain numpy ''' Todo: - fixmes - Make a System class - Provision to draw successive steps iteratively - Check physics (esp. mass) - Find reasonable timestep tradeoff - Colors - Different metrics ''' W = 1000 H = 500 img = numpypy.zeros((H, W, 3), dtype=numpypy.uint8) def clip(d, o, b): return min(max(d, o), b) def pk(c): chan = (1.0/c)*255 chan = clip(0, c, 255) chan = int(chan) return [chan, chan, chan] class Body(object): def __init__(self, m, x, v): self.m = m # mass self.x = x # position self.v = v # velocity self.a = 0+0j # acceleration self.lasta = self.a # acceleration at the last step def force_from(self, other): difference = other.x - self.x strength = other.m * (1.0/(abs(difference)**2)) force = complex(difference.real * strength, difference.imag * strength) return force def xstep(self): self.x += self.v + (self.a/2.0) def astep(self, s): self.lasta = self.a self.a = 0+0j for other in s: if other.x == self.x: continue # fixme self.a += self.force_from(other) self.v += (self.a + self.lasta)/2.0 for x in range(W): for y in range(H): system = [] system.append(Body(0.0, complex((x-W/2.0)/10.0, (y-H/2.0)/10.0), 0-0j)) system.append(Body(2.5, 20+0j, 0-2j)) system.append(Body(5.0, -20+0j, 0+1j)) for step in range(int(sys.argv[1])): for body in system: body.xstep() for body in system: body.astep(system) d = abs(system[0].x) #print d if d == 0: d = 1/1e300 # fixme setting = pk(d) #print setting img[y, x] = setting def array_to_p6(a): #print a w = a.shape[1] h = a.shape[0] header = "P6\n%i %i\n%i\n" % (w, h, 255) print(header, end='') for x in range(h): for y in range(w): c = a[x][y] #print ec o = "%s%s%s" % (chr(c[0]), chr(c[1]), chr(c[2])) print(o, end='') array_to_p6(img)