def cut_off(self, rect): """make cut adjustment and return edits """ new_free_rects = [] removed_rects = [] new_to_merge_dict = defaultdict(list) old_to_merge_dict = defaultdict(list) for rf in self.free_rects: if rectangle.overlap(rf, rect): active, *cuts = rectangle.get_cuts(rf, rect) if active: removed_rects.append(rf) for cut_type in cuts: chopped_rect = rectangle.cut_off(rf, rect, cut_type) new_to_merge_dict[cut_type].append(chopped_rect) else: if len(cuts) == 1: # len>2 only when corners touching old_to_merge_dict[cuts[0]].append(rf) new_free_rects.append(rf) else: new_free_rects.append(rf) new_merged_rects = [] for cut_type in CUT_TYPES: new_merged_rects.extend(self._merge_wrapped_rect(new_to_merge_dict[cut_type], old_to_merge_dict[cut_type])) new_free_rects.extend(new_merged_rects) self.free_rects = new_free_rects return new_merged_rects, removed_rects
from maxrect_pack.plotrect import plotrects from maxrect_pack.rect import make_rect import maxrect_pack.rect as rectangle from maxrect_pack.recthelper import random_rects random.seed(3) WIDTH = 20 HEIGHT = 20 if __name__ == '__main__': rects_free = make_rect((1, WIDTH), (1, HEIGHT)) rects_cut = random_rects(WIDTH, HEIGHT, 4, 4, 3) for rc in rects_cut: new_rects_free = [] for rf in rects_free: if rectangle.overlap(rc, rf): new_rects = rf.cut_off(rc) if new_rects: new_rects_free.extend(new_rects) else: new_rects_free.append(rf) rects_free = new_rects_free plotrects(*(rects_cut + rects_free), random_color=[0] * len(rects_cut) + [1] * len(rects_free), area_w=WIDTH, area_h=HEIGHT) # plotrects(*(rects_cut),random_color=[0]*len(rects_cut), area_w=WIDTH, area_h=HEIGHT)
random.seed(3) WIDTH = 20 HEIGHT = 20 if __name__ == "__main__": rects_free = make_rect((1, WIDTH), (1, HEIGHT)) rects_cut = random_rects(WIDTH, HEIGHT, 4, 4, 3) for rc in rects_cut: new_rects_free = [] for rf in rects_free: if rectangle.overlap(rc, rf): new_rects = rf.cut_off(rc) if new_rects: new_rects_free.extend(new_rects) else: new_rects_free.append(rf) rects_free = new_rects_free plotrects( *(rects_cut + rects_free), random_color=[0] * len(rects_cut) + [1] * len(rects_free), area_w=WIDTH, area_h=HEIGHT ) # plotrects(*(rects_cut),random_color=[0]*len(rects_cut), area_w=WIDTH, area_h=HEIGHT)