# Using some basic Newtonian physics (G = gm1m2/r^2, F = MA) to create a still # drawing. There are two "gravs" -- particles with gravity pulls that affect # the flow of the other particles. # grav-42-50.pdf displays the weird effect that occurs when the ratio of the # masses is 42:50. import sys sys.path.append('/Users/apollotiger/lib') from euclid import * from math import tan class Particle: def __init__(self,locus=Vector2(0,750),mass=5,radius=2,color=(255,255,255),mother=False): self.radius = radius self.mass = mass self.locus = locus self.velox = Vector2(0,0) def distance(self,locus): return ((self.locus.x - locus.x) ** 2 + (self.locus.y - locus.y) ** 2) ** 0.5 def movement(): global lastpart,gravs newpart = lastpart for grav in gravs: distance = newpart.distance(grav.locus) force = 0.6 * newpart.mass * grav.mass / distance ** 2 acceleration = force / newpart.mass newpart.velox += acceleration * (newpart.locus - grav.locus) / distance if 0 > newpart.locus.x - newpart.velox.x: newpart.velox.x *= -1 if 0 > newpart.locus.y - newpart.velox.y: newpart.velox.y *= -1 if WIDTH < newpart.locus.x - newpart.velox.x: newpart.velox.x *= -1 if HEIGHT < newpart.locus.y - newpart.velox.y: newpart.velox.y *= -1 newpart.locus -= newpart.velox lastpart = newpart fill(0) size(180,180) fill(0,0,0) rect(0,0,HEIGHT,WIDTH) lastpart = Particle(Vector2(80,80 - (140 ** 2 - 70 ** 2) ** 0.5 / 2.0),10,3) gravs = [Particle(Vector2(35,80),50,10), Particle(Vector2(130,80),42,10)] # Stable orbit between the two bodies occurs when the ratio is 50:42 fill(1,1,1) for grav in gravs: oval(grav.locus.x,grav.locus.y,grav.radius,grav.radius) for a in range(2000): movement() fill(0.6,0.6,1,0.15) oval(lastpart.locus.x,lastpart.locus.y,lastpart.radius,lastpart.radius)