-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector3.py
More file actions
106 lines (81 loc) · 3.05 KB
/
vector3.py
File metadata and controls
106 lines (81 loc) · 3.05 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import math
class Vector3:
'''Recreation of Unity's Vector3.
\nSource: https://docs.unity3d.com/ScriptReference/Vector3.html
'''
normalized: 'Vector3'
x: float
y: float
z: float
def __init__(self, x: float, y: float, z: float):
self.x = x
self.y = y
self.z = z
def __repr__(self) -> str:
return f"Vector3(\nx: {self.x}\ny: {self.y}\nx: {self.x}\nmagnitude: {self.magnitude})"
def __sub__(self, other: 'Vector3'):
return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
def __add__(self, other: 'Vector3'):
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
def __mul__(self, other):
if isinstance(other, Vector3):
return Vector3.Dot(self, other)
elif isinstance(other, int | float | bytes):
return Vector3(self.x*other, self.y*other, self.z - other)
else:
raise ValueError("Only Vector3, int, float and bytes are accepted")
def __truediv__(self, other):
if isinstance(other, int | float | bytes):
return Vector3(self.x/other, self.y/other, self.z/other)
else:
raise ValueError("Only int, float and bytes are accepted")
def __eq__(self, other: 'Vector3') -> bool:
return isinstance(other, Vector3) & self.x == other.x & self.y == other.y & self.z == other.z
def __ne__(self, other: 'Vector3') -> bool:
return isinstance(other, Vector3) & (self.x != other.x or self.y != other.y or self.z != other.z)
@staticmethod
def Dot(va: 'Vector3', vb: 'Vector3'):
return va.x*vb.x + va.y*vb.y + va.z*vb.z
@staticmethod
def Distance(va: 'Vector3', vb: 'Vector3'):
return math.sqrt((va.x-vb.x)**2 + (va.y-vb.y)**2 + (va.z-vb.z)**2)
@property
def magnitude(self):
'''norm of the vector'''
return math.sqrt(self.x**2 + self.y**2 + self.z**2)
@staticmethod
def Angle(va: 'Vector3', vb: 'Vector3'):
'''Returns the minimum angle between the 2 vectors in degrees'''
return math.acos(Vector3.Dot(va, vb) / (va.magnitude * vb.magnitude))*180/math.pi
@staticmethod
def Lerp(va: 'Vector3', vb: 'Vector3', t: float) -> 'Vector3':
'''Lerp between vector A and vector B
\nt represents the percentage clamped between 0 and 1'''
return va + (vb - va) * max(min(t, 1), 0)
@staticmethod
def MoveTowards(va: 'Vector3', vb: 'Vector3', maxDistanceDelta: float) -> 'Vector3':
return va + (vb - va) * maxDistanceDelta
@staticmethod
def zero():
return Vector3(0, 0, 0)
@staticmethod
def left():
return Vector3(-1, 0, 0)
@staticmethod
def right():
return Vector3(1, 0, 0)
@staticmethod
def down():
return Vector3(0, -1, 0)
@staticmethod
def up():
return Vector3(0, 1, 0)
@staticmethod
def forward():
return Vector3(0, 0, 1)
@staticmethod
def back():
return Vector3(0, 0, -1)
@staticmethod
def one():
return Vector3(1, 1, 1)