首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将一个大长方体切割成任意尺寸在一定范围内的小长方体。

将一个大长方体切割成任意尺寸在一定范围内的小长方体。
EN

Stack Overflow用户
提问于 2022-04-26 21:19:46
回答 2查看 125关注 0票数 2

我试图把一个大的长方体(L,W,H)切成N (N可以是任意数,不需要指定)小长方体,其中长方体的长度和宽度是大长方体的20%到50%,并且小长方体的高度小于H的30%。小立方体的大小不应该都一样,理想情况下,每个长方体都应该是不同的。算法的输入应该是大长方体的L、W、H,算法的返回应该是一组小方框尺寸,它们可以按顺序叠加回大长方体中。

一个相关的问题,我可以想到的是三维股票切割问题,虽然在这个问题上,你指定每个小长方体,但在这里,小盒子的尺寸可以是随机的,只要他们在一个范围内。任何帮助都将不胜感激!

EN

Stack Overflow用户

回答已采纳

发布于 2022-05-05 09:58:07

代码语言:javascript
复制
rng = np.random.default_rng(0)
def split_dimension(length, low, high):
    '''
    Attempt to partition a segment of the given length in segments
    of length between low and high
    '''
    acc = 0
    while length - acc > high:
        assert length - acc >= low, \
          'Failed to get a solution, maybe it was bad luck, maybe there is no solution'
        # of course this choice could be made in a more clever way
        v = rng.uniform(low, high)
        yield acc, v
        acc += v
    yield acc, length - acc

def split_hypercuboid(dimension, dim_ranges):
    '''
    Attempt to split a given cuboid in sub cuboids with dimensions
    
    '''
    low, high = dim_ranges[-1]
    length = dimension[-1] 
    if len(dimension) == 1:
        for c,v in split_dimension(length, low, high):
            yield (c,), (v,)
    else:
        for c,v in split_dimension(length, low, high):
            for corner, size in split_hypercuboid(dimension[:-1], dim_ranges[:-1]):
                yield corner + (c,), size + (v,)
def solve(L, W, H):
    ''' Apply it to the original question '''
    return list(split_hypercuboid((L, W, H), ((0.2*L, 0.5*L), (0.2*W, 0.5*W), (0.0, 0.3*H))))

绘制第一层

代码语言:javascript
复制
plt.figure(figsize=(5,5))
for (z, x, y), (l, w, h) in solve(100, 100, 100):
    if z == 0:
        plt.plot([x, x, x+w, x+w, x], [y, y+h, y+h, y, y], '-k')

票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72020655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档