How many circles of radius R with a D distance between them, fit in rectangle of B x L

215 Views Asked by At

i have a rectangle of B x L meters and i want to know how many circles with R radius can fit if there is a space of D between them. All the values wil be integers always.

I tried this.

With this example:

Initial grid with 10x9

i have: B = 10 L = 9

If radius of circles are R=1 and distance is 3 D=3. I can fit 4 circles (In the image they are the yellow zones)

Grid with circles put in it

So, for creating like a formula, i extended the radius of the circles to radius plus D between them, so the radius of the circles will be R + D/2. After that i calculate the area of a circle with that Radius

Grid with circles and extended radius with distance

With that area, i divided the area of the rectangle (10 x 9) between the area of the circle and cut the decimals, but the result is 2, not 4 :(

Results of calculation

I dont know how to get to a valid formula. I'm not a mathematician, i would appreciate any help. Thanks :)

1

There are 1 best solutions below

0
On

after reviewing the link that @hornypigeon54 provided me in the comment of the post, i saw the code that the page used to calculate the number of circles in the rectangle. After modyfing it a little, i came with this function in python to do that calculation. B and L are the dimensions of the rectangle, radius is the radius of the circles and the distance between them. So far the algorithm is working but i'm testing it.

def n_circles_in_rectangle(B: int, L: int, radius: int, distance:int) -> int:

    diameter = radius*2
    n_circles = 0

    if ((diameter + distance) <= B and (diameter + distance) <= L):
        posX = (diameter / 2)
        posY = (diameter / 2)

        while (posY + radius <= L):
            while (posX + radius<= B):
                n_circles += 1
                posX = posX + (diameter + distance)

            posX = (diameter / 2)
            posY = posY + diameter + distance

    return n_circles


print(n_circles_in_rectangle(B=10, L=9, radius=1, distance=4))