-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec2d.py
72 lines (57 loc) · 1.91 KB
/
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
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
import math
import random
class Vec2d:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return '({0}, {1})'.format(self.x, self.y)
def mag(self):
return math.sqrt(self.x**2 + self.y**2)
def serialize(self, name):
return '''"{name}": {{
"x": {x},
"y": {y}
}}'''.format(name=name, x=self.x, y=self.y)
@classmethod
def rand(self):
return Vec2d(random.random(), random.random())
def dist(self, other):
return (self - other).mag()
def mag(self):
return math.sqrt(pow(self.x, 2) + pow(self.y, 2))
def unit(self):
mag = self.mag()
if mag == 0:
return Vec2d(0,0)
return Vec2d(self.x / mag, self.y/ mag)
# Element by element multiplication
def elm_mult(self, other):
return Vec2d(self.x * other.x, self.y * other.y)
# Element by element division
def elm_div(self, other):
return Vec2d(self.x / other.x, self.y / other.y)
def __add__(self, other):
return Vec2d(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vec2d(self.x - other.x, self.y - other.y)
def __mul__(self, scalar):
return Vec2d(self.x * scalar, self.y * scalar)
def __truediv__(self, scalar):
assert scalar != 0
return Vec2d(self.x / scalar, self.y / scalar)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.x == other.x and self.y == other.y
else:
return False
def __ne__(self, other):
return not self.__eq__(other)
def __gt__(self, other):
return self.mag() > other.mag() and self != other
def create_vec2d_array(x_vec, y_vec):
assert len(x_vec) == len(y_vec), "Lists not same length"
points = []
for (x, y) in zip(x_vec, y_vec):
points.append(Vec2d(x, y))
return points