-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector3.py
35 lines (27 loc) · 1.03 KB
/
vector3.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
class vec3:
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z
def __add__(self, other):
return vec3(self.x + other.x, self.y + other.y, self.z + other.z)
def __sub__(self, other):
return vec3(self.x - other.x, self.y - other.y, self.z - other.z)
def __mul__(self, val):
return vec3(self.x * val, self.y * val, self.z * val)
def __truediv__(self, val):
return vec3(self.x / val, self.y / val, self.z / val)
def mag(self):
return (self.x**2 + self.y**2 + self.z**2)**(0.5)
def cross(self, other):
return vec3(self.y * other.z - self.z * other.y,
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x)
def dot(self, other):
return self.x * other.x + self.y * other.y + self.z * other.z
def normalized(self):
m = self.mag()
if not m == 0:
return vec3(self.x/m, self.y/m, self.z/m)
else:
return vec3(0,0,0)