-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.py
45 lines (32 loc) · 1.42 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
import math
class Vec3:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __str__(self):
return f'Vec3({self.x},{self.y},{self.z})'
def __sub__(self, other):
return Vec3(self.x - other.x , self.y - other.y , self.z - other.z)
def __add__(self, other):
return Vec3(self.x + other.x , self.y + other.y , self.z + other.z)
def mag(self):
return math.sqrt(self.x**2 + self.y**2 + self.z**2)
def __div__(self, other):
return Vec3(self.x/other, self.y/other , self.z/other)
def __rmul__(self, other):
return Vec3(self.x*other, self.y*other , self.z*other)
def __mul__(self, other):
return Vec3(self.x*other, self.y*other , self.z*other)
def __neg__(self,other):
return Vec3(-1*self.x, -1*self.y, -1*self.z)
def __truediv__(self, other):
return Vec3(self.x/other, self.y/other , self.z/other)
def dot(self, other):
if isinstance(other, Vec3):
return self.x * other.x + self.y * other.y + self.z * other.z
raise Exception('this should recieve an arfument of type Vec3')
def cross(self, other):
if isinstance(other, Vec3):
return Vec3( self.y* other.z - self.x* other.y, self.z* other.x - self.x* other.z , self.x * self.y - self.y * self.x)
raise Exception('this should recieve an arfument of type Vec3')