# 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.

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.

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 • This is very impressive, I've got to give it a try still but I'm just going to mark it as accepted anyway.
– shim
Jan 19, 2021 at 16:37
• Yea, that works nicely. But if n = 1, then for $a$ you have $sec(\frac{𝜋}{2} - 𝜋)$ in the denominator, which is undefined?
– shim
Jan 20, 2021 at 20:53
• Oops, yes you're right. It doesn't actually work for n=1 Jan 20, 2021 at 22:14

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.

• So what's the length of the side?
– shim
Jan 13, 2021 at 23:01
• That depends? Related to what? Are you keeping the distance from the center of the polygon to a vertex constant? Or the distance to the side (apothem) constant? Or perhaps you are keeping the length of a side constant? Then the radius would be half of that!
– mjw
Jan 13, 2021 at 23:02
• The input is a given box size and a number of circles.
– shim
Jan 13, 2021 at 23:04
• Once you formulate the problem precisely, it is straightforward to calculate the length of a side from some other parameter.
– mjw
Jan 13, 2021 at 23:04
• Oh, they have to fit into a box? The bottom is square?
– mjw
Jan 13, 2021 at 23:05

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.