-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasstypes.py
212 lines (166 loc) · 5.92 KB
/
classtypes.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
class vec2():
def __init__(self,x,y=None):
if y==None:
if type(x) in [list,tuple,vec2]:
self.pos=[x[0],x[1]]
else:
self.pos=[x,x]
else:
self.pos=[x,y]
self.dist=0
self.reload()
def reload(self):
self.dist=(self.x*self.x+self.y*self.y)**.5
@property
def x(self):
return self[0]
@x.setter
def x(self, value):
self[0] = value
self.reload()
@property
def y(self):
return self[1]
@y.setter
def y(self, value):
self[1] = value
self.reload()
@property
def normal(self):
return self/self.dist
def normalize(self):
self.pos[0]/=self.dist
self.pos[1]/=self.dist
self.dist=1
def copy(self):
return vec2(self.pos[0],self.pos[1])
def __str__(self):
return "vec2({},{})".format(self.pos[0],self.pos[1])
def __mul__(self, value):
assert isinstance(value, (int, float, complex, vec2,list,tuple))
if isinstance(value, (int, float, complex)):
return vec2(*(e*value for e in self))
return vec2(self.x*value[0], self.y*value[1])
def __truediv__(self, value):
assert isinstance(value, (int, float, complex, vec2,list,tuple))
if isinstance(value, (int, float, complex)):
return vec2(*(e/value for e in self))
return vec2(self.x/value[0], self.y/value[1])
def __add__(self,value):
assert isinstance(value, (int, float, complex, vec2,list,tuple))
if isinstance(value, (int, float, complex)):
return vec2(*(e+value for e in self))
return vec2(self.x+value[0], self.y+value[1])
def __sub__(self,value):
assert isinstance(value, (int, float, complex, vec2,list,tuple))
if isinstance(value, (int, float, complex)):
return vec2(*(e-value for e in self))
return vec2(self.x-value[0], self.y-value[1])
def __neg__(self):
return vec2(-1*self.x, -1*self.y)
def __getitem__(self,index):
return self.pos[index]
def __setitem__(self,index,value):
self.pos[index]=value
self.reload()
def __eq__(self,value):
if isinstance(value, (vec2,list,tuple)):
return self[0]==value[0] and self[1]==value[1]
return False
def __round__(self):
return vec2(round(self.x), round(self.y))
class polydict:
def __init__(self,axii=["key","value"],data=[]):#data = [{"key": 5,"value": 10}] and use non-axii for non-access ones
self.stored = {hash(str(d)):d for d in data}
self.refs = {axis:{str(data[i][axis]):hash(str(data[i])) for i in range(len(data))} for axis in axii}
#speed var
self.len = len(self.stored)
def __len__(self):
return self.len
def __str__(self):
return "axii: "+str([i for i in self.refs.keys()])+"\n"+"\n".join(["poly ID "+str(i[0])+": "+str(i[1]) for i in self.stored.items()])
def criteriaID(self,criteria):
for id,data in self.stored.items():
keys = [i for i in data.keys()]
i=0
for s in range(len(keys)):#clean out non-criteria checks
if not keys[i] in [i for i in criteria.keys()]:
del keys[i]
i-=1
i+=1
i=0
if len(keys)!=0:
continue
for s in range(len(keys)):
if not data[keys[i]]==criteria[keys[i]]:#make sure each check is true, if yes return it
break
i+=1
if i==len(criteria)-1:
return id
return 0.1#hashes don't return decimals
def set(self, set, criteria={}):#turns into add element by default
if criteria!={}:
id = self.criteriaID(criteria)
if id!=0.1:
for k,v in set.items():
self.stored[id][k] = v
return
for axis in set.keys():
if axis in self.refs.keys():
self.refs[axis][str(set[axis])]=len(self.stored)
self.stored[hash(str(set))]=set
self.len = len(self.stored)
def get(self, criteria):
assert type(criteria)==dict
id = self.criteriaID(criteria)
if id==.1:
raise KeyError("Could not find poly with criteria "+str(criteria))
return self.stored[id]
def rem(self,criteria):
id = self.criteriaID(criteria)
if id!=.1:
del self.stored[id]
for axis in self.refs.keys():
if id in self.refs[axis].values() and axis in criteria.keys():
del self.refs[axis][str(criteria[axis])]
self.len = len(self.stored)
class inf:
def __init__(self,amount=1):
self.amount=amount
def __str__(self):
return str(self.amount)+"inf"
def __mul__(self,other):
self.amount*=other#works for inf*inf too!
return self
def __truediv__(self,other):
self.amount/=other
return self
def __add__(self,other):
self.amount+=other
return self
def __sub__(self,other):
self.amount-=other
return self
def __neg__(self):
self.amount=0-self.amount
return self
def __eq__(self,other):
if type(other)==type(self):
return self.amount==other.amount
return False
def __lt__(self,other):#less
if type(other)==type(self):
return self.amount<other.amount
return False
def __le__(self,other):#less or equal
if type(other)==type(self):
return self.amount<=other.amount
return False
def __gt__(self,other):#greater
if type(other)==type(self):
return self.amount>other.amount
return True
def __ge__(self,other):#greater or equal
if type(other)==type(self):
return self.amount>=other.amount
return True