""" Some utility functions for sets of grid cells. """ import debug from direction import direction from vector3d import vector3d def increment_set(curset, direct): """ Return the cells incremented in given direction """ if direct==direction.NORTH: offset = vector3d(0,1,0) elif direct==direction.SOUTH: offset = vector3d(0,-1,0) elif direct==direction.EAST: offset = vector3d(1,0,0) elif direct==direction.WEST: offset = vector3d(-1,0,0) elif direct==direction.UP: offset = vector3d(0,0,1) elif direct==direction.DOWN: offset = vector3d(0,0,-1) else: debug.error("Invalid direction {}".format(dirct)) newset = set() for c in curset: newc = c+offset newset.add(newc) return newset def remove_border(curset, direct): """ Remove the cells on a given border. """ border = get_border(curset, direct) curset.difference_update(border) def get_upper_right(curset): ur = None for p in curset: if ur == None or (p.x>=ur.x and p.y>=ur.y): ur = p return ur def get_lower_left(curset): ll = None for p in curset: if ll == None or (p.x<=ll.x and p.y<=ll.y): ll = p return ll def get_border( curset, direct): """ Return the furthest cell(s) in a given direction. """ # find direction-most cell(s) maxc = [] if direct==direction.NORTH: for c in curset: if len(maxc)==0 or c.y>maxc[0].y: maxc = [c] elif c.y==maxc[0].y: maxc.append(c) elif direct==direct.SOUTH: for c in curset: if len(maxc)==0 or c.ymaxc[0].x: maxc = [c] elif c.x==maxc[0].x: maxc.append(c) elif direct==direct.WEST: for c in curset: if len(maxc)==0 or c.x