思想:跟家里铺瓷砖的想法是一样的
给出大矩形的宽高:width, height
切块的宽高:span(width, height)
def slice_rectangle(width, height, span): shape = [0, 0] # 高宽 blocks = [] # (lx,ly) 左上角。(bx,by)右下角 lx = 0 ly = 0 bx = 0 by = 0 while by < height: by = ly + span if by > height: by = height lx = bx = 0 shape[0] = shape[0] +1 shape[1] = 0 while bx < shape[1] = shape[1] + 1 bx = lx + span if bx > bx = width block = [lx, ly, bx, by] # 对应位置: 坐上x,y 和 右下x.y的坐标 blocks.append(block) lx = bx ly = by # 返回切割的形状和每个切割块的坐标 return shape,blocks