-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector.py
57 lines (46 loc) · 1.74 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
from math import sqrt, pow
class Vector3:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def magnitude(self):
return sqrt(pow(self.x, 2) + pow(self.y, 2) + pow(self.z, 2) )
def normalize(vec):
mag = vec.magnitude()
if mag != 0:
return Vector3(vec.x/mag, vec.y/mag, vec.z/mag)
else:
return Vector3(0, 0, 0)
def __add__(self, b):
if type(b) is Vector3:
return Vector3(self.x + b.x, self.y + b.y, self.z + b.z)
elif type(b) is Vector2:
return Vector3(self.x + b.x, self.y + b.x, self.z)
else:
return Vector3(self.x + b, self.y + b, self.z + b)
def __sub__(self, b):
if type(b) is Vector3:
return Vector3(self.x - b.x, self.y - b.y, self.z - b.z)
elif type(b) is Vector2:
return Vector3(self.x - b.x, self.y - b.y, self.z)
else:
return Vector3(self.x - b, self.y - b, self.z - b)
def __mul__(self, b):
if type(b) is Vector3:
return Vector3(self.x * b.x, self.y * b.y, self.z * b.z)
return Vector3(self.x * b, self.y * b, self.z * b)
def __truediv__(self, b):
if type(b) is Vector3:
return Vector3(self.x / b.x, self.y / b.y, self.z / b.z)
return Vector3(self.x / b, self.y / b, self.z / b)
def cross(a, b):
return Vector3((a.y * b.z) - (a.z * b.y),
(a.z * b.x) - (a.x * b.z),
(a.x * b.y) - (a.y * b.x))
def toMatrix(self):
return [[self.x], [self.y], [self.z]]
def toArray4(self):
return [self.x, self.y, self.z, 0.0]
def __repr__(self):
return f'{self.x} , {self.y}, {self.z}'