How do I rotate a square around a circle? I have


*

*a circle of radius r

*a square of length l


The centre of the square is currently rotating around the circle in a path described by a circle of radius $(r + \frac{l}{2})$

However, the square overlaps the circle at e.g. 45°. I do not want the square to overlap with the circle at all, and still smoothly move around the circle. The square must not rotate, and should remain in contact with the circle i.e. the distance of the square from the circle should fluctuate as the square moves around the circle.
Is there a formula or algorithm for the path of the square?
[edit]
Thanks for all your help! I've implemented the movement based on Yves solution with help from Izaak's source code:

I drew a diagram to help me visualise the movement as well (the square moves along the red track):

 A: At first it  appears that the inner vertex of the square is a circle of radius $r$ and outer vertex goes on circle radius $ r+ \sqrt2 l $.
However the constraint of parallel displacement of squares forces the squares to move horizontally or vertically maintaining sliding contact at four points $$(r,0), (0,r),(-r,0), (0,-r) $$  with square side sliding on circle radius $a$ at the  above points. It also defines an outer square envelope of side $(r+l)$ serving as outer boundary.
A rough sketch (sorry about the hand drawn Paint diagram) can indicate what is happening at outer boundary. 
If $\tan β=\dfrac{l}{l+r}$ then the eight demarcation of change over polar angles to flat square boundary are given by:
$$ θ= β, π/2-β,π/2+β, π-β, π+β, 3π/2-β, 3π/2+β,-β$$

Mathematica has RegionPlot command defining swept area:
r=a; a = 2; l = 1; RegionPlot[{x^2 + y^2 > a^2, x^2 + y^2 < (a + Sqrt[2] l)^2, Abs[x] 
< a + l, Abs[y] < a + l}, {x, -4, 4}, {y, -4, 4} ]

A: Consider what happens when the square is directly to the right of the circle with the circle tangent to the midpoint of a side of the square. 
In order for the square to move up while remaining in contact without crossing into the square, it has to go straight up. 
This continues until the lower left vertex is on the circle, after which that vertex moves along the circle from the rightmost point to the topmost point. 
Then the square has to move straight left until the lower right vertex is on the circle, and then that vertex moves along the circle. 
Each time a vertex moves along the circle, the center of the square moves along a circular arc whose center is displaced from the center of the displayed circle. 
As a result, the center of the square moves straight up, then through a quarter circle arc, the straight left for the width of the square, then through another quarter circle, then down for the height of the square, etc. 
The total length of the path of the square's center until it returns to the start is
$2\pi r + 4s,$ where $r$ is the radius of the circle and $s$ is the side of the square. 
If you can use the distance along the path, not the direction between the centers, as your parameter for the motion of the square, it should make this easier to draw. 
A: 
The path can be expressed in terms of the distance $d$ = AB as a function of the angle $θ$ that the distance line forms with the vertical axis.
Consider $θ$ in the first quadrant first. In the case where the sides of the square are in contact with the circle,  B moves either horizontally or vertically, and the distance function is given simply by 
$$d\left( θ \right) =\left( r+\frac{l}{2} \right) \sec θ ,θ \in \left( 0,\tan ^{-1}\frac{l}{2r+l} \right) 
$$
$$d\left( θ \right) =\left( r+\frac{l}{2} \right) \csc θ ,θ \in \left( \cot ^{-1}\frac{l}{2r+l},\frac{π}{2} \right) 
$$
On the other hand, if the vertex C is in contact with the circle, where $θ\in(\tan^{-1}\frac l{2r+l},\cot^{-1}\frac l{2r+l})$, apply the sine rule to the triangle ABC,
$$\frac{\sin x}r = \frac{\sin y}{\frac l{\sqrt2}}=\frac{\sin(x+y)}d$$
which leads to $d=r(\cos y + \cot x\sin y)$ and $\sin y = \frac l{\sqrt2 r}\sin x$. Recognize that $x=45-θ$ and eliminate $y$ to obtain the path function
$$d\left( θ \right) =\frac{1}{\sqrt{2}}\left( \sqrt{2r^2-l^2\sin ^2\left( 45-θ \right)}+l\cos \left( 45-θ \right) \right) 
$$
Similar expressions can be obtained for $θ$ in the other quadrants.
A: As said elsewhere, the trajectory of the center is made of four lines segments and four circular arcs obtained by shifting the quarters of the original circle. 

But it is not enough to known the shape of the trajectory, we also need to know the distance as a function of time, assuming that the center rotates at constant angular speed (not the contact point).
For convenience, the square is of side $2l$. For a contact on the left side of the square, we intersect the line
$$d\,(\cos\theta,\sin\theta)$$ where $\theta=\omega t$, with the straight part of the trajectory,
$$x=r+l$$
and we obtain the point
$$(r+l,\tan\theta(r+l))$$ and the distance $$\color{green}{d=\frac{r+l}{\cos\theta}}.$$
For a contact on the bottom left corner, we intersect the same line with the shifted circle
$$\left(x-l\right)^2+\left(y-l\right)^2=r^2$$
and this gives us the quadratic equation in $d$
$$\color{green}{d^2-2ld(\cos\theta+\sin\theta)+2l^2-r^2=0}.$$
You need to repeat the reasoning for the other sides and corners of the square.
Finally, the switch from contact by the side to contact by the corner occurs at an angle $\theta$ such that
$$\begin{cases}d\cos\theta=r+l,\\d\sin\theta=l,\end{cases}$$
i.e.
$$\color{green}{\tan\theta=\frac l{r+l}}.$$
