-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.py
63 lines (49 loc) · 1.64 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
from math import sqrt
class Vector():
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z
# for use in matrix ops
self.rowop = [x,y,z]
def __str__(self):
return f"[{self.x}, {self.y}, {self.z}]"
def __repr__(self):
return self.__str__()
# norm of vector
def norm(self):
ab = abs(self)
return Vector(self.x/ab, self.y/ab, self.z/ab)
# rounds vector, returns list of coords
def rnd(self):
return [round(a,4) for a in self.rowop]
# cross product
def cross(self, other):
x = self.y*other.z - self.z*other.y
y = self.z*other.x - self.x*other.z
z = self.x*other.y - self.y*other.x
return Vector(x,y,z)
def __abs__(self):
return sqrt(self.x**2 + self.y**2 + self.z**2)
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
z = self.z + other.z
return Vector(x,y,z)
def __sub__(self, other):
x = self.x - other.x
y = self.y - other.y
z = self.z - other.z
return Vector(x,y,z)
def __mul__(self, other):
if isinstance(other, int) or isinstance(other, float):
return self.__rmul__(other)
if not isinstance(other, Vector):
return other.__rmul__(self)
return self.x*other.x+self.y*other.y+self.z*other.z
def __rmul__(self, other):
return Vector(other*self.x, other*self.y, other*self.z)
if __name__ == "__main__":
v = Vector(2,2)
v2 = Vector(-2.22,-4.3333333333,2.32)
print(abs(v2))