#!/usr/bin/env python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2022/03/24 22:52:01 +0900> u""" Divide rectangle. * Windows10 x64 21H2 + Python 2.7.18 32bit * Windows10 x64 21H2 + Python 3.9.11 64bit """ import random import math import time def get_div_rects(x0, y0, x1, y1, d, v, sftv): new_rect = [] x0 = math.floor(x0) y0 = math.floor(y0) x1 = math.floor(x1) y1 = math.floor(y1) w0 = x1 - x0 h0 = y1 - y0 if w0 <= 0 or h0 <= 0: return new_rect rw = w0 rh = h0 tw = rw th = rh ydivide = True if tw <= th else False if v == 0: ydivide = False elif v == 1: ydivide = True if ydivide: h = float(h0) / d s = h * sftv / 2.0 lst = [] lst.append(y0) i = 1 while i < d: y = math.floor(y0 + h * i + random.uniform(-s, s)) if y <= y1: lst.append(y) i += 1 lst.append(y1) lst = list(set(lst)) lst.sort() for i in range(len(lst) - 1): new_rect.append([int(x0), int(lst[i]), int(x1), int(lst[i + 1])]) else: w = float(w0) / d s = w * sftv / 2 lst = [] lst.append(x0) i = 1 while i < d: x = math.floor(x0 + w * i + random.uniform(-s, s)) if x <= x1: lst.append(x) i += 1 lst.append(x1) lst = list(set(lst)) lst.sort() for i in range(len(lst) - 1): new_rect.append([int(lst[i]), int(y0), int(lst[i + 1]), int(y1)]) return new_rect def div_rect(rects, x0, y0, x1, y1, v, cnt, cntmax, dmin, dmax): if cnt > cntmax: return rects if dmin >= dmax: d = dmin elif cnt == 0: d = dmax else: d = random.randint(dmin, dmax) new_rects = get_div_rects(x0, y0, x1, y1, d, v, 0.6) if cnt == cntmax: rects.extend(new_rects) else: for r in new_rects: x0, y0, x1, y1 = r rects = div_rect(rects, x0, y0, x1, y1, 2, cnt + 1, cntmax, dmin, dmax) return rects def get_divide_rectangles(w, h, dmin, dmax, cntmax): """Divide rectangle.""" if dmin > dmax: dmax = dmin x0 = 0 y0 = 0 x1 = w - 1 y1 = h - 1 rects = [] rects = div_rect(rects, x0, y0, x1, y1, 1, 0, cntmax, dmin, dmax) return rects def init_random_seed(seed, randomize): if randomize == True or randomize == 1: seed = math.floor(time.time()) random.seed(seed) return seed def fill_rects(imgw, imgh, rects, spc, borderradius): # bg cfill color = 0.25 bg_col = 0.25 for rect in rects: bx0, by0, bx1, by1 = rect x0 = math.floor(bx0 + spc) y0 = math.floor(by0 + spc) x1 = math.floor(bx1 - spc) y1 = math.floor(by1 - spc) w0 = x1 - x0 h0 = y1 - y0 print("(x0, y0, x1, y1, w0, h0) = (%d, %d, %d, %d, %d, %d)" % (x0, y0, x1, y1, w0, h0)) if w0 <= 0 or h0 <= 0 or x0 >= imgw or y0 >= imgh: print("not fill") continue # box fill color fillcol = bg_col + random.uniform((24.0 / 256.0), (80.0 / 256.0)) print("fill color = %f" % fillcol) if borderradius <= 0: # rectangle pass else: # round rectangle pass def main(): """Main func.""" w, h = 1024, 1024 dmin = 1 # 1 - 10 dmax = 3 # 1 - 10 cntmax = 4 # 1 - 10 spc = 2 # 0 - 16 borderradius = 2 # 0 - 16 seed = 42 # static randomize seed randomize = 1 # 1 or True : randomize / 0 or False : use static seed randseed = init_random_seed(seed, randomize) rects = get_divide_rectangles(w, h, dmin, dmax, cntmax) # fill_rects(w, h, rects, spc, borderradius) print("# image w,h = %d, %d" % (w, h)) print("# divide min, max = %d, %d" % (dmin, dmax)) print("# repeat count = %d" % cntmax) print("# random seed = %d" % randseed) print(rects) if __name__ == "__main__": main()