2017-05-31 40 views
1

我如何才能找到什麼是表的佈局,將包含一定數量的細胞(ñ),其尺寸(x的比率,Y)最接近於一個給定的縱橫比(- [R)?我如何才能找到什麼是表的佈局,將包含一定數量的細胞的尺寸,其比例是最接近給定的寬高比

我有一些項目,我需要放在一個表格佈局,並需要知道我的表格佈局必須至少包括所有的項目,儘可能接近給定的長寬比。

例如假設我們有Ñ = 5項放入一個表的佈局和我們的目標縱橫比- [R爲4:3(1.33),然後是3×2會更好然後2×3表。

3 * 2(比3:2 = 1.5,從而更接近1.33)

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

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

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

回答

0

我發現在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] 
相關問題