Formula for radius of circles on vertices of regular polygon TLDR: Given a square, a number of circles, a rotation angle, and some spacing factor, how can I determine the largest possible radius of circles arranged on the vertices in a regular polygon such that the circles do not exceed the bounds of the square?

I am trying to determine a formula or algorithm for calculating the radius of circles situated on the vertices of regular polygons with 1 to n sides.
I have tried the following with inconsistent results.
2 * a * tan(π / n) / x

Where x is a sizing factor and a is the apothem. For example say I wanted the spacing between circles to be such that you could fit another circle on the midpoint of the side, x could be 4. Or if I wanted the circles to be touching x could be 1. But for n = 3 the results look quite different than say for 6 in that the spacing between the circles is too small/big respectively.
As for the apothem, it's not clear how I would calculate that.
For n = 1 or 2 I would have to handle it differently. For 1 I'd just want a circle in the centre of the available space, and for 2 they would be arranged in a line. Is there a more general formula I could use?
Something like the circles on the vertices of the hexagon in the image below, where I could adjust the radius of the circles as needed.

Further complication is that I calculate the coordinates of the vertices based on a given square and an angle to offset the resulting vertices, but I reduce the size of the resulting polygon based on the radius of the circles to make sure the resulting circles do not exceed the bounds of the square.
Here's a few imperfect drawings I made for 1–3:
  
The circles don't have to fill the square necessarily; I want to be able to adjust the spacing between them by reducing the radius.
I'm a bit stuck on a circular dependency (no pun intended) since I think I need the positions of the vertices to calculate the circle radius, but I need the circle radius to determine the positions of the vertices so that the circles do not exceed the size of the square.
 A: I believe this becomes easier to think about if we bound the circles inside of a larger circle.
Labeled diagram of an example setup with four circles:

With five circles:

Below is some trigonometry that relates the size of the square and the number of circles to the radius of the circles, their distance from the center of the square, and the angle of displacement between the circles. Let $n$ be the number of circles. The other labels are described in the diagram(s).
\begin{align*}
    e &= \frac{2\pi}{2n} \ \text{rad}=\frac{\pi}{n} \ \text{rad} \\
    d &= \pi-\frac{\pi}{2}-e=\frac{\pi}{2}-e \\
    b &= a \cdot \sec{d} \\
    c &= b+a \\
    f &= 2c
  \end{align*}
Now, to solve for $a$ in terms of $f$ and $n$:
\begin{align*}
  f &= 2(b+a) \\
  f &= 2(a \cdot \sec(d) + a) \\
  f &= 2a(\sec(d) + 1) \\
  a &= \frac{f}{2(\sec(d) + 1)} \\
  a &= \frac{f}{2(\sec(\frac{\pi}{2}-e) + 1)} \\
  a &= \frac{f}{2(\sec(\frac{\pi}{2}-\frac{\pi}{n}) + 1)} \\
  \end{align*}
And for $b$ in terms of $f$ and $n$:
\begin{align*}
    b &= a \cdot \sec(d) \\
    b &= \frac{f \cdot \sec(d)}{2(\sec(\frac{\pi}{2}-\frac{\pi}{n}) + 1)} \\
    b &= \frac{f \cdot \sec(\frac{\pi}{2}-\frac{\pi}{n})}{2(\sec(\frac{\pi}{2}-\frac{\pi}{n}) + 1)} \\
  \end{align*}
This works for any number of circles greater than $n=1$.
Bonus eight circle example

A: Seems like you can always grow the circles on the vertices to have radii equal to half the length of a side of the polygon.
A: I'm not sure how mathematical this is but instead of placing the circles so their centres are on the vertices of the polygon (which creates a cyclical dependency between the radius of the circles and the polygon vertices) I placed the circles so that the polygon vertex lies on the circumference of the circle and I used linear interpolation relative to the centre of the polygon to calculate the coordinates of the centres of the circles. For the case of n == 1 I still need to handle it a bit differently but other than that it works nicely.
I also used the distance from the vertices to the centre to adjust the radius of the circles to adjust the spacing.
