-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.py
156 lines (105 loc) · 3.63 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import math
from formation_package import math_ as rvo_math
class Vector2:
# Defines a 2-D vector.
def __init__(self, x=0.0, y=0.0):
'''
Constructs and initializes a 2-D vector from the specified xy-coordinates.
Args:
x (float): The x-coordinate of the 2-D vector.
y (float): The y-coordinate of the 2-D vector.
'''
self.x_ = x
self.y_ = y
def __str__(self):
return "Vector2(x={}, y={})".format(self.x_, self.y_)
@property
def x(self):
return self.x_
@property
def y(self):
return self.y_
def __matmul__(self, other):
assert isinstance(other, Vector2), '__matmul__ argument should be a Vector2'
return self.x_*other.x_ + self.y_*other.y_
def __mul__(self, other):
assert not isinstance(other, Vector2), '__mul__ argument should be a float'
return Vector2(self.x_*other, self.y_*other)
def __rmul__(self, other):
assert not isinstance(other, Vector2), '__rmul__ argumetn should be a float'
return Vector2(other*self.x_, other*self.y_)
def __truediv__(self, scalar):
return Vector2(self.x_/scalar, self.y_/scalar)
def __add__(self, other):
return Vector2(self.x_ + other.x_, self.y_ + other.y_)
def __radd__(self, other):
return Vector2(other.x_ + self.x_, other.y_ + self.y_)
def __sub__(self, other):
return Vector2(self.x_ - other.x_, self.y_ - other.y_)
def __rsub__(self, other):
return Vector2(other.x_ - self.x_, other.y_ - self.y_)
def __neg__(self):
return Vector2(-self.x_, -self.y_)
def __abs__(self):
'''
Computes the length of a specified 2-dimensional vector.
Args:
vector (Vector2): The 2-dimensional vector whose length is to be computed.
Returns:
float: the length of the 2-dimensional vector.
'''
return math.sqrt(rvo_math.abs_sq(self))
class Vector3:
# Defines a 3-D vector.
def __init__(self, x=0.0, y=0.0, z=0.0):
'''
Constructs and initializes a 3-D vector from the specified xy-coordinates.
Args:
x (float): The x-coordinate of the 3-D vector.
y (float): The y-coordinate of the 3-D vector.
z (float): The z-coordinate of the 3-D vector.
'''
self.x_ = x
self.y_ = y
self.z_ = z
def __str__(self):
return "Vector3(x={}, y={}, z={})".format(self.x_, self.y_, self.z_)
@property
def x(self):
return self.x_
@property
def y(self):
return self.y_
@property
def z(self):
return self.z_
def __matmul__(self, other):
assert isinstance(other, Vector3), '__matmul__ argument should be a Vector3'
return self.x_*other.x_ + self.y_*other.y_ + self.z_*other.z_
def __mul__(self, other):
assert not isinstance(other, Vector3), '__mul__ argument should be a float'
return Vector3(self.x_*other, self.y_*other, self.z_*other)
def __rmul__(self, other):
assert not isinstance(other, Vector3), '__rmul__ argument should be a float'
return Vector3(other*self.x_, other*self.y_, other*self.z_)
def __truediv__(self, scalar):
return Vector3(self.x_/scalar, self.y_/scalar, self.z_/scalar)
def __add__(self, other):
return Vector3(self.x_ + other.x_, self.y_ + other.y_, self.z_ + other.z_)
def __radd__(self, other):
return Vector3(other.x_ + self.x_, other.y_ + self.y_, other.z_ + self.z_)
def __sub__(self, other):
return Vector3(self.x_ - other.x_, self.y_ - other.y_, self.z_ - other.z_)
def __rsub__(self, other):
return Vector3(other.x_ - self.x_, other.y_ - self.y_, other.z_ - self.z_)
def __neg__(self):
return Vector3(-self.x_, -self.y_, -self.z_)
def __abs__(self):
'''
Computes the length of a specified 3-D vector.
Args:
vector (Vector3): The 3-D vector whose length is to be computed.
Returns:
float: the length of the 3-D vector.
'''
return math.sqrt(rvo_math.abs_sq(self))