def splitNode(self, node, w, h): node.used = True node.down = BlockNode(self, node.x, node.y + h, node.w, node.h - h) node.right = BlockNode(self, node.x + w, node.y, node.w - w, h) return node
def growRight(self, w, h): root = BlockNode(self, 0, 0, self.root.w + w, self.root.h) root.used = True root.down = self.root root.right = BlockNode(self, self.root.w, 0, w, self.root.h) self.root = root node = self.findNode(self.root, w, h) if node: return self.splitNode(node, w, h) else: return None
def growDown(self, w, h): root = BlockNode(self, 0, 0, self.root.w, self.root.h + h) root.used = True root.down = BlockNode(self, 0, self.root.h, self.root.w, h) root.right = self.root self.root = root node = self.findNode(self.root, w, h) if node: return self.splitNode(node, w, h) else: return None
def __init__(self, w=0, h=0): self.nodes = [] self.autogrow = False if w > 0 and h > 0: self.root = BlockNode(self, 0, 0, w, h) else: self.autogrow = True self.root = None
def fit(self, blocks): length = len(blocks) w = blocks[0].w if length > 0 else 0 h = blocks[0].h if length > 0 else 0 if self.autogrow: self.root = BlockNode(self, 0, 0, w, h) for block in blocks: node = self.findNode(self.root, block.w, block.h) if node: block.fit = self.splitNode(node, block.w, block.h) elif self.autogrow: block.fit = self.growNode(block.w, block.h)