-
Notifications
You must be signed in to change notification settings - Fork 3
/
vector.py
78 lines (61 loc) · 1.71 KB
/
vector.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
__author__ = 'wybe'
# Python 3.4.3
import math
class Vector:
"""
Vector class.
"""
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
"""
Adds two vectors together.
"""
return Vector(self.x + other.x, self.y + other.y)
def __sub__(self, other):
"""
Subtracts two vectors from each other.
"""
return Vector(self.x - other.x, self.y - other.y)
def __str__(self):
"""
Converts the vector into a human readable string.
"""
return "<" + str(self.x) + ", " + str(self.y) + ">"
def get_head(self):
"""
Returns the heading of the vector.
"""
return math.atan2(self.y, self.x)
def get_mag(self):
"""
Returns the magnitude of the vector.
"""
return math.sqrt((self.x ** 2) + (self.y ** 2))
def set_head(self, heading):
"""
Sets the heading of the vector and keeps the magnitude the same.
"""
mag = self.get_mag()
self.x = math.cos(heading) * mag
self.y = math.sin(heading) * mag
def set_mag(self, magnitude):
"""
Sets the magnitude of the vector and keeps the heading the same.
"""
head = self.get_head()
self.x = math.cos(head) * magnitude
self.y = math.sin(head) * magnitude
def mult(self, factor):
"""
Multiplies the vector with the factor.
"""
mag = self.get_mag()
self.set_mag(mag * factor)
def rotate(self, angle):
"""
Rotates the vector by the angle.
"""
head = self.get_head()
self.set_head(head + angle)