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):



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.
