/
Vector3d.py
65 lines (49 loc) · 1.52 KB
/
Vector3d.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
import numpy as np
from geomutils import add_coords
from distance import euclidean_dist
class Vector3d:
def __init__(self, x, y, z):
self._coord = (x, y, z)
def __add__(self, other):
return add_coords(self, other)
def __mul__(self, other):
if isinstance(other, (int, float)):
return Vector3d(*tuple(x * other for x in self._coord))
if len(self) != len(other):
raise Exception('cannot multiply vectors of different dimensions')
return np.dot(self._coord, other._coord)
def cross_product(self, other):
a0, a1, a2 = self._coord
b0, b1, b2 = other._coord
return Vector3d(a1 * b2 - a2 * b1, a2 * b0 - a0 * b2, a0 * b1 - a1 * b0)
def __neg__(self):
return self * -1
def __sub__(self, other):
return self + (-other)
def __abs__(self):
return euclidean_dist(self._coord)
def __getitem__(self, i):
return self._coord[i]
def direction(self):
d = abs(self)
if d != 0:
return self * (1 / d)
def __len__(self):
return len(self._coord)
def __eq__(self, other):
return self._coord == other._coord
def __hash__(self):
return hash(self._coord)
def __str__(self):
return str(self._coord)
if __name__ == "__main__":
v = Vector3d(1,3,4)
print(v * v)
print(abs(v))
print(v.direction())
print(v[0], v[1])
print(len(v))
print(v - v)
print(hash(v))
u = Vector3d(1,3,5)
print(u.cross_product(v))