-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.py
112 lines (77 loc) · 3.6 KB
/
general.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
from generallibrary import typeChecker, confineTo
class GeneralVector:
"""
Not to be used directly.
GeneralVector class that Vec2 and Vec inherits for shared functions, end-goal is that all methods are moved here to make package dry
"""
def __init__(self, *axis, length):
axis = tuple([float(n) if typeChecker(n, str, error=False) else n for n in axis if n is not None])
axisLen = len(axis)
if axisLen == 0:
self.axis = tuple([0] * length)
elif axisLen == 1:
if typeChecker(axis[0], GeneralVector, error=False):
vector = axis[0]
if len(vector.axis) != length:
raise AttributeError(f"{vector} was supplied as first argument but it's axis length is not {length}")
self.axis = vector.axis + tuple()
else:
typeChecker(axis[0], float)
self.axis = tuple([axis[0]] * length)
elif axisLen == length:
self.axis = axis
else:
raise AttributeError(f"Could not handle supplied axis {axis} for {self.__class__.__name__}")
for n in self.axis:
typeChecker(n, float)
def __getitem__(self, item):
return self.axis[item]
def sanitize(self, ints=False, positive=False, positiveOrZero=False, negative=False, negativeOrZero=False, minimum=None, maximum=None):
"""
Sanitize this vector with a bunch of optional flags.
:param generalvector.Vec or generalvector.Vec2 self:
:param ints:
:param positive:
:param positiveOrZero:
:param negative:
:param negativeOrZero:
:param generalvector.Vec or generalvector.Vec2 minimum:
:param generalvector.Vec or generalvector.Vec2 maximum:
:return: self
"""
if ints and self != self.round():
raise ValueError(f"{self} failed 'ints' sanitizing")
if positive and not self > 0:
raise ValueError(f"{self} failed 'positive' sanitizing")
if positiveOrZero and not self >= 0:
raise ValueError(f"{self} failed 'positiveOrZero' sanitizing")
if negative and not self < 0:
raise ValueError(f"{self} failed 'negative' sanitizing")
if negativeOrZero and not self <= 0:
raise ValueError(f"{self} failed 'negativeOrZero' sanitizing")
if minimum and not self >= minimum:
raise ValueError(f"{self} failed 'minimum' sanitizing")
if maximum and not self <= maximum:
raise ValueError(f"{self} failed 'maximum' sanitizing")
if ints:
return self.round()
else:
return self
def confineTo(self, pos, size, margin=0):
"""
Confine this vector to an area, but unlike clamp it subtracts axis * n to create an 'infinite' area effect.
:param generalvector.Vec or generalvector.Vec2 self:
:param generalvector.Vec or generalvector.Vec2 pos: Lowest point of area
:param generalvector.Vec or generalvector.Vec2 size: Size of area, has to be positive or zero
:param float margin: Margin of confinement
"""
pos = self.__class__(pos)
size = self.__class__(size).sanitize(positiveOrZero=True)
maximum = pos + size
return self.__class__(*[confineTo(axis, pos[i], maximum[i], margin) for i, axis in enumerate(self.axis)])
def absolute(self):
"""
Return this vector with absolute values.
:param generalvector.Vec or generalvector.Vec2 self:
"""
return self.__class__(*[abs(axis) for axis in self.axis])