Algorithm for fitting circles into a square

I have the following question. I want to write some code that solves the following problem. Suppose we have a square of 1x1 meters and I want to fill them with circles of 0.22m diameter in a pyramid pattern, kind of like this.

enter image description here

How would I go about doing that? The horizontal row is easy to calculate. Just 1 / 0.22 rounded down but how can I calculate the vertical amount of circles?


Solution 1:

In this particular problem we can use the properties of the arrangement that you described as pyramid pattern: a circle is put on top of two adjacent circles such that their centers form an equilateral triangle. Let $d$ be the diameter of circles. Then the vertical distance between centers of two tangent circles that are not on the same horizontal line is $\frac{\sqrt3}{2}d$. This is the distance between rows of circles in this problem's arrangement. Now, how many rows can we fit within a square of side $a > d$? Note there is a half-circle under the lowest circle's center, and a half-circle above the highest circle's center. Try and use this hint before checking the answer below:

$$1 + \lfloor \frac{a-d}{\frac{\sqrt3}{2}d} \rfloor$$