Search code examples
formulagdl

formula for number of blocks with mortar in a distance


I want to work out a formula to use in basic code for the following scenarios.

d = distance
bw = block width
mw = mortar width
b = number of blocks

how many blocks b (or fraction thereof) is there in d if there is mortar between every block but there might or might not be mortar at the end of the last block.

scenarios using the formula b = d / (bw + mw)

d = 480
bw = 230
mw = 10

b would equal 2, this is correct.

d = 470
bw = 230
mw = 10

b would equal 1.958, this is incorrect, it should still equal 2 as i only need to know the number of blocks.

d = 595
bw = 230
mw = 10

b would equal 2.479, this is incorrect, it should equal 2.5 as i only need to know the number of blocks not including the fractional part of the mortar.


Solution

  • # integer division to calculate whole blocks
    whole_blocks = d // (bw + mw)
    
    # deduct whole blocks from distance
    remaining_space = d - whole_blocks * (bw + mw)
    
    # divide remaining space by block width to get partial blocks
    # if greater than 1, reset to 1 (the rest will be mortar)
    # then add to whole blocks
    final_blocks = whole_blocks + min(remaining_space / bw, 1)