/
Centroid.py
48 lines (37 loc) · 1 KB
/
Centroid.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
from Vec2 import Vec2
from Bounds import Bounds
from mDist import mDist
class Centroid(Vec2):
ID=0
centroids=[] # a convenient place to keep them
@classmethod
def reset(cls):
for c in Centroid.centroids:
c.clear()
def clear(self):
self.verts=list()
self.neighbors=set()
self.neighborVerts=dict()
self.isClosed= True
@classmethod
def at(cls,p):
dists= [ (mDist(p,c),c) for c in Centroid.centroids ]
return [p for d,p in sorted(dists,key=lambda x: x[0])][0]
@classmethod
def region(cls,p):
return cls.at(p)
def __new__(cls, *args, **kwargs):
instance= Vec2.__new__(cls, *args, **kwargs )
instance.ID= Centroid.ID ; Centroid.ID+=1
instance.clear()
return instance
# called after border intersects
def addCornerVert(self): # because we can
for p in Bounds.corners:
if Centroid.region(p)==self:
self.verts.append(p)
def addNeighbor(self, n, v):
self.neighbors.add(n)
if not n in self.neighborVerts:
self.neighborVerts[n]=set()
self.neighborVerts[n].add(tuple(v))