-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.py
187 lines (137 loc) · 5.49 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from math import sqrt
import random
from generallibrary import clamp, inrange, typeChecker
from generalvector.general import _GeneralVector
class Vec(_GeneralVector):
"""
Immutable vector.
"""
def __init__(self, x=None, y=None, z=None):
_GeneralVector.__init__(self, x, y, z, length=3)
self.x = self.axis[0]
self.y = self.axis[1]
self.z = self.axis[2]
def __str__(self):
return f"Vec({self.x}, {self.y}, {self.z})"
def __repr__(self):
return self.__str__()
def __eq__(self, other):
if typeChecker(other, _GeneralVector, error=False):
return self.x == other.x and self.y == other.y and self.z == other.z
else:
return self.x == other and self.y == other and self.z == other
def __add__(self, other):
other = Vec(other)
return Vec(self.x + other.x, self.y + other.y, self.z + other.z)
def __neg__(self):
return Vec(-self.x, -self.y, -self.z)
def __sub__(self, other):
return self + -other
def __mul__(self, other):
other = Vec(other)
return Vec(self.x * other.x, self.y * other.y, self.z * other.z)
def __truediv__(self, other):
other = Vec(other)
return Vec(self.x / other.x, self.y / other.y, self.z / other.z)
def __lt__(self, other):
other = Vec(other)
return self.x < other.x and self.y < other.y and self.z < other.z
def __gt__(self, other):
other = Vec(other)
return self.x > other.x and self.y > other.y and self.z > other.z
def __le__(self, other):
other = Vec(other)
return self.x <= other.x and self.y <= other.y and self.z <= other.z
def __ge__(self, other):
other = Vec(other)
return self.x >= other.x and self.y >= other.y and self.z >= other.z
def length(self):
"""
Get the length of this vector using pythagorean theorem.
"""
return sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
def normalized(self):
"""
Get this vector normalized by dividing each value by it's length.
"""
length = self.length()
if length == 0:
return Vec(0)
return self / length
def round(self):
"""
Get this vector with each value rounded.
"""
return Vec(round(self.x), round(self.y), round(self.z))
@staticmethod
def random(value1, value2=None):
"""
Get a vector with random values.
:param float or Vec value1: Minimum value if value2 is specified too. Otherwise it's the maximum value and minimum becomes 0
:param float or Vec value2: Optional maximum value
"""
if value2 is None:
value2 = Vec(value1)
value1 = Vec(0)
else:
value2 = Vec(value2)
value1 = Vec(value1)
return Vec(random.uniform(value1.x, value2.x), random.uniform(value1.y, value2.y), random.uniform(value1.z, value2.z))
def min(self, minimum):
"""
Get a new vector containing the minimum value for each value in the two vectors.
:param float or Vec minimum: Minimum value, floats are converted to Vec
"""
minimum = Vec(minimum)
return Vec(min(self.x, minimum.x), min(self.y, minimum.y), min(self.z, minimum.z))
def max(self, maximum):
"""
Get a new vector containing the maximum value for each value in the two vectors.
:param float or Vec maximum: Minimum value, floats are converted to Vec
"""
maximum = Vec(maximum)
return Vec(max(self.x, maximum.x), max(self.y, maximum.y), max(self.z, maximum.z))
def clamp(self, minimum, maximum):
"""
Get this vector clamped between two values as a new vector.
:param float or Vec minimum: Minimum value, floats are converted to Vec
:param float or Vec maximum: Maximum value, floats are converted to Vec
"""
minimum = Vec(minimum)
maximum = Vec(maximum)
return Vec(clamp(self.x, minimum.x, maximum.x), clamp(self.y, minimum.y, maximum.y), clamp(self.z, minimum.z, maximum.z))
def inrange(self, minimum, maximum):
"""
Return whether this vector is between two other vectors.
:param float or Vec minimum: Minimum value, floats are converted to Vec
:param float or Vec maximum: Maximum value, floats are converted to Vec
"""
return inrange(self.x, minimum.x, maximum.x) and inrange(self.y, minimum.y, maximum.y) and inrange(self.z, minimum.z, maximum.z)
def hex(self):
"""
Get a hex based on each value. Rounded and clamped between 0 and 255.
"""
rounded = self.round().clamp(0, 255)
return f"#{'%02x' % rounded.x}{'%02x' % rounded.y}{'%02x' % rounded.z}"
def range(self, size):
"""
Get a range from two vectors.
Self is upper left corner.
A size of Vec(1, 1) will return 1 Vec.
:param Vec size: Size of range.
:rtype: list[Vec]
"""
self.sanitize(ints=True)
size = Vec(size).sanitize(ints=True, positiveOrZero=True)
rangeList = []
for z in range(size.z):
for y in range(size.y):
for x in range(size.x):
rangeList.append(self + Vec(x, y, z))
return rangeList
def distance(self, other):
"""
Return distance between two Vectors.
:param other: Another Vec
"""
return (self - other).length()