Rotating boxes of variable width around a circle.

140 Views Asked by At

first time posting a question. I'll try and be as descriptive as possible.

Let's say I have a circle with a Radius of r, and a variable amount of boxes with variable widths, but equal height, that I'd like to rotate around the circle. Each box should not overlap, but they should touch (I'm sure there is a math term for this). Given a starting point of 90 degrees, how would one go about doing this? Below is a link to a picture of what I'd like to accomplish. Also, here is a link to what I have managed to come up with: Link to project.

I hope that is a thorough enough explanation. Any help would be greatly appreciated, as I've hit a wall. I've stumbled across some great help, but haven't found anything that exactly fits my need. Thanks in advance.

enter image description here

Additional image to explain

1

There are 1 best solutions below

0
On BEST ANSWER

It suffices to determine the angular offset between neighboring box centers. Let $r$ be the radius of the circle, $w_1$ the half-width of the box already in place and $w_2$ the half-width of the next box to be placed. Using half-widths seems natural: we really only care about the side of a box that faces its neighbor and this declutters the math by hiding otherwise extraneous factors of two in the variables. Polar coordinates seems best suited for this problem.

W.l.o.g. place the first box so that it touches the circle at $r\angle0$. Since we’re assuming that the boxes are tall enough that no outer corners will fit under a neighboring box, there are three cases to consider:

  • $w_1=w_2$: The corners touch, so the angular displacement is simply twice the angle between the box corner and center. $$\Delta\theta = 2\arctan{w_2\over r}. \tag1$$

  • $w_2\gt w_1$: The corner of the second box hits the side of the first. That side is parallel to the polar axis, so its equation is $\rho = {w_1\over\sin\theta}$. The inner corners of the second box are at distance of $\sqrt{r^2+w_2^2}$ from the origin, so the corner will touch the box side at an angle of $$\arcsin{w_1\over\sqrt{r^2+w_2^2}}.$$ To this we add the angular displacement of the edge midpoint from the corner, so the total angular displacement of the second box from the first is $$\Delta\theta = \arcsin{w_1\over\sqrt{r^2+w_2^2}} + \arctan{w_2\over r}. \tag 2$$ Inverse trig functions can be quite expensive to compute, so it might be worthwhile in terms of overall efficiency to combine the two terms into a single tangent value by converting the sine to a tangent via the pythagorean theorem and using the formula for the tangent of a sum of angles.

  • $w_2\le w_1$: The corner of the first box hits the edge of the second. The roles of the boxes are reversed, so swap $w_1$ and $w_2$ and apply formula (2).

There is a further complication when the first box is wider than the circle and $w_2\gt w_1$: if we place the box corner on the side of its neighbor per the second case above, they’ll overlap. Looking at what should happen as the second box’s width is increased, once the corners of the two boxes meet, increasing the box width shouldn’t change the angle any more. The box should simply keep extending in place. So, if $w_1\gt r$, clamp $w_2$ to $\min(w_1,w_2)$ and proceed as above.