-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.py
More file actions
executable file
·45 lines (31 loc) · 1.12 KB
/
vec3.py
File metadata and controls
executable file
·45 lines (31 loc) · 1.12 KB
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
# contains vec3 class for vector math
import math
class vec3:
def __init__(self, x = 0, y = 0, z = 0):
self.x = x
self.y = y
self.z = z
def __add__(self, other): # vector addition
return vec3(self.x + other.x, self.y + other.y, self.z + other.z)
def __radd__(self, other): # vector addition
return vec3(self.x + other.x, self.y + other.y, self.z + other.z)
def __sub__(self, other): # vector subtraction
return vec3(self.x - other.x, self.y - other.y, self.z - other.z)
def __rsub__(self, other): # vector subtraction
return vec3(other.x - self.x, other.y - self.y, other.z - self.z)
def __iadd__(self, other):
self.x += other.x
self.y += other.y
self.z += other.z
def __mul__(self, sc): # scalar multiplication
return vec3(self.x * sc, self.y * sc, self.z * sc)
def __rmul__(self, sc): # scalar multiplication
return self.__mul__(sc)
def __str__(self):
return str(self.x) + ", " + str(self.y) + ", " + str(self.z)
def magnitude(self):
return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
def normalize(self):
mag = self.magnitude()
return self * (1/mag)
# make plus equals