-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
82 lines (66 loc) · 2.94 KB
/
Copy pathvector.py
File metadata and controls
82 lines (66 loc) · 2.94 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import builtins
import math
from numbers import Number
import numpy as np
class Vec3:
"""Holds 3-elements tuple that represents a 3-dimensional vector"""
def __init__(self, x=0.0, y=0.0, z=0.0):
if isinstance(x, Vec3):
(self.x, self.y, self.z) = (float(x.x), float(x.y), float(x.z))
elif isinstance(x, tuple):
(self.x, self.y, self.z) = (float(x[0]), float(x[1]), float(x[2]))
elif isinstance(x, list):
(self.x, self.y, self.z) = (float(x[0]), float(x[1]), float(x[2]))
elif isinstance(x, np.ndarray):
(self.x, self.y, self.z) = (float(x[0]), float(x[1]), float(x[2]))
else:
assert isinstance(x, Number)
(self.x, self.y, self.z) = (float(x), float(y), float(z))
def __str__(self):
rounded = self.__round__(3)
return f"<{rounded.x}, {rounded.y}, {rounded.z}>"
def __repr__(self):
rounded = self.__round__(3)
return f"Vec3 Object: <{rounded.x}, {rounded.y}, {rounded.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, scalar):
assert not isinstance(scalar, Vec3)
return Vec3(self.x * scalar, self.y * scalar, self.z * scalar)
def __rmul__(self, scalar):
return self.__mul__(scalar)
def __truediv__(self, other):
if isinstance(other, Vec3):
return Vec3((self.x / other.x), (self.y / other.y), (self.z / other.z))
else:
assert isinstance(other, int) or isinstance(other, float)
return Vec3((self.x / other), (self.y / other), (self.z / other))
def dot(self, other):
# assert isinstance(other, Vec3) # Caused issues with recursion?
return self.x * other.x + self.y * other.y + self.z * other.z
def cross(self, other):
assert isinstance(other, Vec3)
if abs(self) != 0 and abs(other) != 0:
i = self.y * other.z - self.z * other.y
j = self.z * other.x - self.x * other.z
k = self.x * other.y - self.y * other.x
return Vec3(i, j, k)
else:
print(f"Issues with vectors {self} and {other}")
print("Cannot take cross product of zero vector")
def __abs__(self):
return math.sqrt(self.dot(self))
def __round__(self, num_dec=2):
rounded_vec = Vec3(x=builtins.round(self.x, num_dec),
y=builtins.round(self.y, num_dec),
z=builtins.round(self.z, num_dec))
return rounded_vec
def normalize(self):
magnitude = abs(self)
if magnitude == 0.0:
print("Warning: Attempted to normalize zero vector")
return 0
else:
return self / magnitude