One other method that ought to provide you with principally the identical outcomes as DavidT’s reply, however maybe with easier code:
Step 1: Divide your sq. formation into 4 rows. Every row begins at a nook place and extends from there in a constant route (for example clockwise, it would not actually matter a lot) across the sq..
Adapting DavidT’s ASCII artwork notation, listed below are the 4 traces of three items every in formations of N = 4, 8 and 12 items. I’ve labeled the items in every line as F(entrance), L(left), R(proper) and B(ack):
N = 4 N = 8 N = 12
F R F-F R F-F-F R
| |
L B L R L R
| | |
L B-B L R
|
L B-B-B
Step 2: Organize all of the items in every line in order that the areas between them and between the final unit and the subsequent nook are equal.
(This successfully hides the uneven alternative of whether or not to increase the traces clockwise or counterclockwise from the corners. Both method, the nook spots will all the time be crammed so long as N ≥ 4 and the remainder of the items on either side of the sq. shall be evenly spaced between the corners.)
Step 3: Resolve what number of items to place in every line.
If the variety of items is divisible by 4, you possibly can (and doubtless ought to) simply put the identical variety of items in every line. If not, you’ll have to add one additional unit to some traces.
The images in your query successfully match a filling order of F(ront) -> R(ight) -> B(ack) -> L(eft), and if that is what you want, it ought to work completely advantageous.
DavidT, nonetheless, suggests what’s successfully another filling order the place you begin by placing the primary additional unit within the F(ront) row and the second within the B(ack) row, but when there is a third additional unit, you take away the second unit from the again row and transfer it and the third unit into the L(eft) and R(ight) rows. This has the debatable benefit of all the time preserving the identical variety of items on the left and proper sides of the sq. and thus preserving the formation all the time symmetrical. (Effectively, at the very least for N ≠3.)
In fact there are additionally different choices, like F(ront) -> R(ight) -> L(eft) -> B(ack), which does not fairly all the time keep symmetry however avoids shifting items between rows as new items are added to the formation.
Or you would even let the participant explicitly select what number of items they need on either side of the formation. If most threats are coming from the entrance, possibly they’d reasonably have just one or two items within the again and a bunch of additional items within the entrance, for instance. In fact whether or not it is sensible to supply the participant such flexibility (and the related UI complexity) is determined by the kind of recreation you make.
Step 4: Resolve how large the sq. ought to be.
It most likely is sensible to make the scale of the sq. proportional to the variety of items within the longest line. That method, the minimal distance between adjoining items within the sq. shall be fixed.
The space between the items and the PC in the course of the sq. can even all the time be at the very least 1/sqrt(2) ≈ 0.7 occasions the minimal distance between two items. If this feels too crowded, you would make a particular case for N ≤ 4 and multiply the scale of the sq. by some scaling issue between sqrt(2) ≈ 1.4 and a couple of in that case. To me, an element of 1.5 suggests itself as a fairly easy and pure approximation of sqrt(2), however you possibly can determine for your self what seems to be and feels greatest to you.
