def get_fitting(self, free_rect): frx, fry = free_rect[DIM] a, b = self.x, self.y if a <= frx and b <= fry: yield make_rect((0, a), (0, b)) if b <= frx and a <= fry: yield make_rect((0, b), (0, a))
def __init__(self, width, height, free_rects=None): self.width = width self.height = height if free_rects is None: free_rects = [make_rect((0, width), (0, height))] self.free_rects = free_rects
def random_rects(area_w=DEFAULT_AREA_W, area_h=DEFAULT_AREA_H, max_width=DEFAULT_AREA_W, max_height=DEFAULT_AREA_H, num=DEFAULT_RANDOM_NUM): result = [] for _ in range(num): while 1: x1, x2 = sorted((random.randint(1, area_w), random.randint(1, area_w))) if 0 < x2 - x1 <= max_width: break while 1: y1, y2 = sorted((random.randint(1, area_h), random.randint(1, area_h))) if 0 < y2 - y1 <= max_height: break result.append(make_rect((x1, x2), (y1, y2))) return result
def random_rects(area_w=DEFAULT_AREA_W, area_h=DEFAULT_AREA_H, max_width=DEFAULT_AREA_W, max_height=DEFAULT_AREA_H, num=DEFAULT_RANDOM_NUM): result = [] for _ in range(num): while 1: x1, x2 = sorted( (random.randint(1, area_w), random.randint(1, area_w))) if 0 < x2 - x1 <= max_width: break while 1: y1, y2 = sorted( (random.randint(1, area_h), random.randint(1, area_h))) if 0 < y2 - y1 <= max_height: break result.append(make_rect((x1, x2), (y1, y2))) return result
import random 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)
import random 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),