1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python

import numpy as n
import scipy.constants as c

class Body:

    def __init__(self,xx,xy,vx,vy,m):
        self.coord = n.array([xx,xy],dtype='float')
        self.speed = n.array([vx,vy],dtype='float')
        self.mass = m
        self.color = n.random.rand(3)

    def __str__(self):
        return """Planet:
Coordinates:\t%05.2f\t%05.2f
Velocity:\t%05.2f\t%05.2f
Mass:\t\t%05.2e""" % (self.coord[0],self.coord[1],self.speed[0],self.speed[1],self.mass)

    def gravitate(self,other):
        if self == other:
            return 0

        if other.__class__ == self.__class__:
            e_r = self.coord-other.coord
            a = -6e-3*other.mass/n.inner(e_r,e_r)*e_r
            self.speed += a
        else:
            raise TypeError, 'This is no planet'

    def move(self):
        self.coord += self.speed

if __name__ == '__main__':
    print 'This is a test body'
    b = Body(1.1,1.2,.1,1,1e3)
    print b