-
Notifications
You must be signed in to change notification settings - Fork 2
/
Vec2d.py
38 lines (26 loc) · 914 Bytes
/
Vec2d.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
import math
class Vec2d:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self, new):
self.x += new.x
self.y += new.y
def add_vect(self, new):
return Vec2d(self.x + new.x, self.y + new.y)
def sub(self, new):
self.x -= new.x
self.y -= new.y
def sub_vect(self, new):
return Vec2d(self.x - new.x, self.y - new.y)
def angle(self):
return math.atan2(-self.y,self.x)
def limit(self, max_):
if self.mag() > max_:
self.set_mag(max_)
def mag(self):
return math.sqrt(self.x * self.x + self.y * self.y)
def set_mag(self, value):
self.x, self.y = value*self.x/self.mag(), value*self.y/self.mag()
def distance(self, new):
return math.sqrt((self.x - new.x) * (self.x - new.x) + (self.y - new.y) * (self.y - new.y))