-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharge.py
30 lines (23 loc) · 1.09 KB
/
charge.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
import math
class Charge:
def __init__(self, **kwargs):
self.magnitude = kwargs.get('magnitude')
self.position = kwargs.get('position')
def get_sign(self):
return self.magnitude/math.fabs(self.magnitude)
def get_sq_distance_from(self, position: tuple):
return (self.position[0] - position[0]) ** 2 + (self.position[1] - position[1]) ** 2
def get_component_factors(self, position: tuple):
if self.position[0] == position[0]:
return 0, 1
slope = (self.position[1] - position[1]) / (self.position[0] - position[0])
angle = math.fabs(math.atan(slope))
if self.position[0] > position[0] and self.position[1] > position[1]:
angle = math.pi - angle
elif self.position[0] > position[0] and self.position[1] < position[1]:
angle = math.pi + angle
elif self.position[0] < position[0] and position[1] > self.position[1]:
angle = -angle
return math.cos(angle), math.sin(angle)
def get_shifted_position(self):
return self.position[0] - 10, self.position[1] - 10