-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.py
More file actions
58 lines (44 loc) · 1.67 KB
/
vec3.py
File metadata and controls
58 lines (44 loc) · 1.67 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
46
47
48
49
50
51
52
53
54
55
56
57
58
from math import sqrt
import numbers
class Vec3:
def __init__(self, x=0, y=0, z=0):
(self.x, self.y, self.z) = (x, y, z)
def __repr__(self):
return '{}({}, {}, {})'.format(__class__.__name__, self.x, self.y, self.z)
# +u
def __pos__(self):
return self
# -u
def __neg__(self):
return Vec3(-self.x, -self.y, -self.z)
# u + v
def __add__(self, other):
return Vec3(self.x + other.x, self.y + other.y, self.z + other.z)
# u - v
def __sub__(self, other):
return Vec3(self.x - other.x, self.y - other.y, self.z - other.z)
# u * v
def __mul__(self, other):
return Vec3(self.x * other.x, self.y * other.y, self.z * other.z)
# k * u
def __rmul__(self, other):
return Vec3(other * self.x, other * self.y, other * self.z)
# u / k or u / v
def __truediv__(self, other):
if isinstance(other, numbers.Real):
return Vec3(self.x / other, self.y / other, self.z / other)
return Vec3(self.x / other.x, self.y / other.y, self.z / other.z)
def length(self):
return sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
def squared_length(self):
return self.x * self.x + self.y * self.y + self.z * self.z
def dot(self, other):
return self.x * other.x + self.y * other.y + self.z * other.z
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 Vec3(x, y, z)
def unit(self):
l = self.length()
return self / l