from numarray import * class Ball: def __init__(self): self.pos = array([0.0, 1.0]) self.vel = array([1.0, 0.0]) self.acc = array([0.0, 0.0]) self.forc = array([0.0, 0.0]) self.mass = 1.0; class Simulation: gravity = array([0.0, -9.8]) drag = 0.03; elastic = 0.8 # ~letter size paper scale = array([ 72 * 8.25, 72 * 10]) def __init__(self): self.ball = Ball() pass def step(self, dt): ball = self.ball #print ball.pos[1] # calculate forces ball.forc = (-self.drag * ball.vel) + (ball.mass * self.gravity); #print ball.forc # calculate acceleration ball.acc = ball.forc / ball.mass; # integrate vel = ball.vel + ball.acc * dt; pos = ball.pos + ball.vel * dt; # check walls if (pos[1] < 0) | (pos[1] > 1): vel[1] = -ball.vel[1] * self.elastic pos[1] = ball.pos[1] + 0 if (pos[0] < 0) | (pos[0] > 1): vel[0] = -ball.vel[0] * self.elastic pos[0] = ball.pos[0] + 0 ball.vel = vel ball.pos = pos # scale to paper pos = pos * self.scale # draw circle print "%0.2f %0.2f %0.2f 0 360 arc" % (pos[0], pos[1], 6*abs(ball.vel[1])+8) print "stroke" def start(self, final, dt): pos = self.ball.pos * self.scale print "%d %d moveto" % (pos[0], pos[1]) for i in range(0,int(final/dt)): self.step(dt*i) print 'stroke' savedout = sys.stdout; def main(): sys.stdout = open('ball.ps', 'w') init() sim = Simulation() sim.start(0.0115,0.00001) fin() def init(): pass print """ %!PS-2.0 /inch {72 mul} def 0.3 setlinewidth 0.85 0.5 0.2 setrgbcolor """ def fin(): print """ showpage """ sys.stdout = savedout if __name__ == "__main__": main()