在墙上均匀分布装饰元素

2024-09-20 22:53:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图编写一些小程序,帮助我计算墙上需要多少装饰元素,以及元素之间的距离

假设墙的宽度为128.3厘米,元素的宽度为4.3厘米

我需要一些公式,这将帮助我计算元素之间的差距(面板),这样,我就不必削减元素的宽度

也请参考附件中的图片

figure of a sample distribution

以下是我目前的代码:

wall_length = float(128.3)
panel_width = float(4.3)
print('\n','\n','\n','\n')

num1 = (float(wall_length) / float(panel_width))
num2 = num1 / 2
num3 = num2 + 1
num4 = num3 * float(panel_width)
num5 = wall_length - num4
num6 = num5 / num2
num7 = num6 + panel_width

print('###########################', '\n', 'num1 - result! Divide wall with Panel', '\n', '=', num1, '\n') 
print('###########################', '\n', 'num2 - result!', '\n', num1, '/', '2', '=', num2, '\n')
print('###########################', '\n', 'num3 - result!', '\n', num2, '+', '1', '=', num3, '\n')
print('###########################', '\n', 'num4 - result!', '\n', num3, '*', panel_width, '=', num4, '\n')
print('###########################', '\n', 'num5 - result!', '\n', wall_length, '-', num4, '=', num5, '\n')
print('###########################', '\n', 'num6 - result!', '\n', num5, '/', num2, '=', num6, '\n')
print('\n', '\n', '\n','\n', '\n', '\n')

print('###########################','\n','YOUR GAP SIZE IS','\n',num6,'\n' )
print('###########################','\n','YOUR PANEL SIZE + GAP SIZE','\n',num7,'\n')
print('###########################')
print('\n', '\n', '\n','\n', '\n', '\n')

Tags: 元素宽度resultfloatwidthlengthprintpanel
2条回答

或者是另一种解决方案,获得与面板宽度几乎相同的间隙宽度

def get_panel_number(wall_length: float, panel_width: float) -> int:
    """
    1. Substract width of the first pannel to get wall length to
    distribute rest of panels
    2. Divide the output of above substraction by the width of panel to get 
    maximum
    number of panels and the fraction of the last panel
    3. Divide that number by 2 and get integer of it to get number of panels
    4. Add the first panel to that output to get overall number of panels
    """
    wall_length_to_distribute = wall_length - panel_width
    maximum_number_of_panels = wall_length_to_distribute / panel_width
    return int(maximum_number_of_panels/2) + 1

def get_gap_width(
    wall_length: float, panel_width: float, number_of_panels: int
) -> float:
    """
    1. From wall_length substract number of panels multiply by panel width to 
       get how many gaps in total you have
    2. Substract 1 from number of panels to get number of gaps
    3. Divide gaps width in total by number of spaces to get one gap width

    """
    gaps_width_in_total = wall_length - (number_of_panels * panel_width)
    number_of_gaps = number_of_panels - 1
    return gaps_width_in_total / number_of_gaps



wall_length = 128.3
panel_width = 4.3


number_of_panels = get_panel_number(wall_length, panel_width)
gap_width = get_gap_width(wall_length, panel_width, number_of_panels)

print(f"Wall length: {wall_length}")
print(f"Panel width: {panel_width}")
print(f"Number of panels: {number_of_panels}")
print(f"Gap between panels: {gap_width}")

输出:

Wall length: 128.3
Panel width: 4.3
Number of panels: 15
Gap between panels: 4.557142857142858

您使用哪个版本的python?该程序至少需要3.6,因为f-string

首先,我需要说几句话:

  1. 您不需要使用wall_length = float(128.3)wall_length = 128.3也会这样做
  2. 间隙和面板的总数不能是偶数(num1在代码中),因为墙应该以面板开始和结束
  3. 您可以尝试该总数的不同备选方案,并选择面板宽度和间隙宽度之间的最小差值。例如,128.3 / 43 = 29.837您可以尝试29、31,看看哪一个是最好的

以下是一种方法:

def print_possible_gap_widths(wall_length, panel_width):
    num_total = wall_length // panel_width 

    # num_total cannot be even since the wall should start and end with a panel
    if num_total % 2 == 0:
        num_total += 1

    for total in [num_total - 2, num_total, num_total + 2]:
        num_gaps = total // 2
        num_panels = total - num_gaps
        total_gap_lengths = wall_length - (num_panels * panel_width)
        gap_width = total_gap_lengths / num_gaps
        print("Gap width:", gap_width,  "Diff:", abs(panel_width - gap_width)) 

对于128.34.3,调用print_possible_gap_widths(128.3, 4.3),结果将是:

Gap width: 5.23846153846154 Diff: 0.9384615384615405
Gap width: 4.557142857142858 Diff: 0.257142857142858
Gap width: 3.9666666666666677 Diff: 0.33333333333333215

因此,最好的方法是4.557142857142858

对于128.38.9,调用print_possible_gap_widths(128.3, 8.9),结果将是:

Gap width: 11.0 Diff: 2.0999999999999996
Gap width: 8.157142857142858 Diff: 0.742857142857142
Gap width: 6.025 Diff: 2.875

因此,最好的方法是8.157142857142858

最后,您可以比较结果并作出决定

相关问题 更多 >

    热门问题