Layout 如何找到包含一定数量的单元格的表格布局,这些单元格的尺寸比最接近给定的纵横比

Layout 如何找到包含一定数量的单元格的表格布局,这些单元格的尺寸比最接近给定的纵横比,layout,aspect-ratio,closest,Layout,Aspect Ratio,Closest,如何找到包含一定数量的单元格(N)的表格布局,这些单元格的尺寸比(x,y)最接近给定的纵横比(r) 我有许多项需要放在表布局中,并且需要知道我的表布局必须是什么,以便在尽可能接近给定纵横比的情况下至少包含所有项 例如假设我们在表格布局中有N=5项,目标纵横比r为4:3(1.33),那么3x2比2x3表格更好 3*2(比率3:2=1.5,因此更接近1.33) 2*3(比率2:3=0.67) 我在Python中找到了这个解决方案 def closest_layout_to_ratio(n, rati

如何找到包含一定数量的单元格(N)的表格布局,这些单元格的尺寸比(x,y)最接近给定的纵横比(r)

我有许多项需要放在表布局中,并且需要知道我的表布局必须是什么,以便在尽可能接近给定纵横比的情况下至少包含所有项

例如假设我们在表格布局中有N=5项,目标纵横比r为4:3(1.33),那么3x2比2x3表格更好

3*2(比率3:2=1.5,因此更接近1.33)

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


我在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]
def最近的_布局_对_比率(n,比率=(4,3)):
如果n==1:返回(1,1)
th_r=最大(比率)/最小(比率)
th_x=th_r*math.sqrt(n/th_r)
th_y=n/th_x
最佳=(无,无,math.inf)
对于rounder in(math.floor,math.ceil):
y=最大值(1,圆度(THU y))
x=数学单元(n/y)
r=x/y
如果abs(r-thu-r)
我在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]
def最近的_布局_对_比率(n,比率=(4,3)):
如果n==1:返回(1,1)
th_r=最大(比率)/最小(比率)
th_x=th_r*math.sqrt(n/th_r)
th_y=n/th_x
最佳=(无,无,math.inf)
对于rounder in(math.floor,math.ceil):
y=最大值(1,圆度(THU y))
x=数学单元(n/y)
r=x/y
如果abs(r-thu-r)
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]