I found an algorithm for determining the maximum size that squares can be packed in a given rectangle. This is working well, but actually it's equally-sized circles that I have to pack, and since the highest-density lattice packing of circles is the hexagonal packing arrangement, I'd like to figure out an algorithm to calculate the best arrangement that will maximize the size of the equally-sized circles within the given x ⨉ y rectangle.
In trying to figure out this for myself, I started with Neptilo's Algorithm to get the maximum size of n squares that fit into a rectangle with a given width and height, but since Neptilo's answer diverges from the accepted answer's detailed explanation, I got a little lost in the algorithm.
I've also noticed that if you are packing by laying rows with every other row's circles offset and nudged up against the previous row, the height of each subsequent row is sin(60°) times the diameter d of the circles. So the total height of r rows would be d ⨉ (1 + (r-1) ⨉ sin(60)). If we were to look at packing by laying columns with every other column's circles offset and nudged in, the width would use a similar formula. Anyhow this is a useful insight but not enough for me to figure out how to find the optimal layout!
Actually, Misha Lavrov had some insight in Maximum number of circle packing into a rectangle, with similar geometric insights, but I'm still not sure how to approach this as an algorithm.
What I'm hoping for is an algorithm to determine the maximum diameter circle where all will fit within a given x ⨉ y sized rectangle, and whether the best approach would be hexagonal packing along rows, hexagonal columns around circles, or — using the square-packing algorithm I cited already is fine — instead packing as squares. (I can envision, for instance, the square packing being better in such as when you have 8 circles to pack in a 400 x 200 rectangle; you are better off with a 4x2 square grid of d=100 rather than hex-packing with d=88.8 since the second row would be offset from the first.