-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.py
104 lines (76 loc) · 3.03 KB
/
point.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import numpy as np
import random
gravity_constant = 0.1
class Point(object):
def __init__(self, pos=None, velocity=None, acceleration=None, radius=None, color=None, bounds=None):
if pos is None:
self.pos = np.array([0, 0], dtype='float64')
else:
self.pos = np.array(pos, dtype='float64')
if velocity is None:
self.velocity = np.array([0, 0], dtype='float64')
else:
self.velocity = np.array(velocity, dtype='float64')
if acceleration is None:
self.acceleration = np.array([0, 0], dtype='float64')
else:
self.acceleration = np.array(acceleration, dtype='float64')
self.radius = radius
self.mass = (4*np.pi/3*(self.radius)**3)
self.bounds = bounds
if color is None:
self.color = tuple(random.randint(0, 255) for i in range(3))
else:
self.color = color
self.id = random.randint(0, 2**32 - 1)
def __eq__(self, other):
if self.id == other.id:
return True
else:
return False
def __hash__(self):
return hash(self.id)
def get_pos(self):
return self.pos.astype('int')
def get_color(self):
return self.color
def get_mass(self):
return self.mass
def get_force(self, other_point):
self_pos = self.get_pos()
other_pos = other_point.get_pos()
distance = np.linalg.norm(other_pos - self_pos)
if distance < (self.radius + other_point.radius):
force = np.array([0, 0], dtype='float64')
else:
force_direction = (self_pos - other_pos)/distance
force = force_direction * gravity_constant * (self.get_mass() * other_point.get_mass()) / (distance ** 2)
return -1 * force
def update_velocity(self, points, dt):
total_force = np.array([0, 0], 'float64')
for i in range(len(points)):
if self.__eq__(points[i]):
continue
force = self.get_force(points[i])
total_force += force
self.acceleration = total_force / self.mass
self.velocity += self.acceleration * dt
def update_position(self, dt):
self.pos += self.velocity * dt
if self.bounds is not None:
if self.pos[0] < 0:
self.pos[0] = 0
self.velocity[0] *= -.8
self.acceleration[0] = 0
elif self.pos[0] > self.bounds[0]:
self.pos[0] = self.bounds[0]
self.velocity[0] *= -.8
self.acceleration[0] = 0
if self.pos[1] < 0:
self.pos[1] = 0
self.velocity[1] *= -.8
self.acceleration[1] = 0
elif self.pos[1] > self.bounds[1]:
self.pos[1] = self.bounds[1]
self.velocity[1] *= -.8
self.acceleration[1] = 0