-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
126 lines (95 loc) · 3.38 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import math
# The Vector class
class Vector:
# Initialiser
def __init__(self, x=0, y=0):
self.x = x
self.y = y
# Returns a string representation of the vector
def __str__(self):
return "(" + str(self.x) + "," + str(self.y) + ")"
# Tests the equality of this vector and another
def __eq__(self, other):
return self.x == other.x and self.y == other.y
# Tests the inequality of this vector and another
def __ne__(self, other):
return not self.__eq__(other)
# Returns a tuple with the point corresponding to the vector
def get_p(self):
return (self.x, self.y)
# Returns a copy of the vector
def copy(self):
return Vector(self.x, self.y)
# Adds another vector to this vector
def add(self, other):
self.x += other.x
self.y += other.y
return self
def __add__(self, other):
return self.copy().add(other)
# Negates the vector (makes it point in the opposite direction)
def negate(self):
return self.multiply(-1)
def __neg__(self):
return self.copy().negate()
# Subtracts another vector from this vector
def subtract(self, other):
return self.add(-other)
def __sub__(self, other):
return self.copy().subtract(other)
# Multiplies the vector by a scalar
def multiply(self, k):
self.x *= k
self.y *= k
return self
def __mul__(self, k):
return self.copy().multiply(k)
def __rmul__(self, k):
return self.copy().multiply(k)
# Divides the vector by a scalar
def divide(self, k):
return self.multiply(1/k)
def __truediv__(self, k):
return self.copy().divide(k)
# Normalizes the vector
def normalize(self):
return self.divide(self.length())
# Returns a normalized version of the vector
def get_normalized(self):
return self.copy().normalize()
# Returns the dot product of this vector with another one
def dot(self, other):
return self.x * other.x + self.y * other.y
# Returns the length of the vector
def length(self):
return math.sqrt(self.x**2 + self.y**2)
# Returns the squared length of the vector
def length_squared(self):
return self.x**2 + self.y**2
# Reflect this vector on a normal
def reflect(self, normal):
n = normal.copy()
n.multiply(2*self.dot(normal))
self.subtract(n)
return self
# Returns the angle between this vector and another one
def angle(self, other):
return math.acos(self.dot(other) / (self.length() * other.length()))
# Rotates the vector 90 degrees anticlockwise
def rotate_anti(self):
self.x, self.y = -self.y, self.x
return self
# Rotates the vector according to an angle theta given in radians
def rotate_rad(self, theta):
rx = self.x * math.cos(theta) - self.y * math.sin(theta)
ry = self.x * math.sin(theta) + self.y * math.cos(theta)
self.x, self.y = rx, ry
return self
# Rotates the vector according to an angle theta given in degrees
def rotate(self, theta):
theta_rad = theta / 180 * math.pi
return self.rotate_rad(theta_rad)
# project the vector onto a given vector
def get_proj(self, vec):
unit = vec.get_normalized()
return unit.multiply(self.dot(unit))