【问题标题】:How can I find what is the table layout that will contain an certain number of cells whose ratio of dimensions is closest to a given aspect ratio如何找到包含一定数量的单元格的表格布局,这些单元格的尺寸比最接近给定的纵横比
【发布时间】:2017-11-01 15:30:06
【问题描述】:

我怎样才能找到包含一定数量的单元格 (N) 的表格布局,其尺寸比 (x, y) 最接近给定纵横比 (r)?

我有许多项目需要放入表格布局中,并且需要知道我的表格布局必须是什么才能至少包含所有项目,同时尽可能接近给定的纵横比。

例如假设我们有 N=5 个要放入表格布局的项目,并且我们的目标纵横比 r 是 4:3( 1.33),那么 3x2 会比 2x3 表更好。

3*2(比例 3:2 = 1.5 所以更接近 1.33)

[1][2][3]
[4][5][_]

2*3(比例 2:3 = 0.67)

[1][2]
[3][4]
[5][_]

【问题讨论】:

    标签: layout aspect-ratio closest


    【解决方案1】:

    我在 Python 中找到了这个解决方案。

    def closest_layout_to_ratio(n, ratio=(4, 3)):
        if n == 1: return (1, 1)
    
        th_r = max(ratio)/min(ratio)
        th_x = th_r*math.sqrt(n/th_r)
        th_y = n / th_x
    
        best = (None, None, math.inf)
        for rounder in (math.floor, math.ceil):
            y = max(1, rounder(th_y))
            x = math.ceil(n/y)
            r = x / y
            if abs(r - th_r) < abs(best[2] - th_r):
                best = (int(x), int(y), r)
    
        return best[:2] if ratio[0] == max(ratio) else best[:2][::-1]
    

    【讨论】:

      猜你喜欢
      • 2016-08-08
      • 2016-02-11
      • 2022-06-22
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 2022-08-24
      • 2020-03-29
      相关资源
      最近更新 更多