Shift a rectangle located on circle circumference so they do not overlap

circlestrigonometry

Suppose I have a rectangle with its center point located on the circumference of a disk or circle.

How can I move the rectangle along the radius just enough so that it does not overlap the disk anymore? That is, how to calculate the new center point of the rectangle or (x′,y′)?

The image illustrating the problem

I have these parameters available:

  • r: radius of the circle (maybe irrelevant)
  • θ: angle of radius
  • w, h: width and height of the rectangle
  • (x,y): coordinates of the current center point of the rectangle (starting yellow dot)

I tried the following but it does not result in what I want:

$$x' = x + \frac{w}{2} * \cos \theta$$
$$y' = y + \frac{h}{2} * \sin \theta$$

It should work for any angle. Another example:

Another example image

Best Answer

You actually have two constraints:

  1. The center of the rectangle (yellow dot) must remain on the ray from the circle's center that makes an angle $\theta$ with the positive $x$-axis.
  2. The point(s) on the boundary of the rectangle nearest to the center of the circle must lie on the circle's circumference.

As a result, you cannot first locate the yellow dot and then translate it outward by the half-width and half-height of the rectangle (as suggested by another answer), because for a rectangle of general dimensions, this will change the position of the yellow dot in such a way that it no longer necessarily meets criterion (1) above.

To simplify the problem, let us consider without loss of generality the case $0 \le \theta \le \pi/2$ and $r = 1$, since the other quadrants behave similarly via symmetry about the coordinate axes, and $w, h$ can be rescaled for $r \ne 1$. Also, let us assume that $h^2 + w^2 < 4$ as suggested by your diagrams (the case where $h^2 + w^2 > 4$ will need separate treatment).

Then, the first thing to observe is that there is a critical interval for which the nearest point as described in constraint (2) is the lower left vertex of the rectangle, and there are two other intervals such that the nearest point is actually located on a side of the rectangle. You show one of these cases in your second diagram, where $\theta = 0$ and the nearest point is located on the left side of the rectangle.

Indeed, it is not difficult to see that it is when the bottom edge of the rectangle coincides with the $x$-axis that the nearest point on the rectangle to the circle's center will transition from a point on the left edge, to the lower left vertex. That is to say, when $$\sin \theta = \frac{h}{2},$$ this is where the transition occurs. Similarly, another transition occurs when $$\cos \theta = \frac{w}{2},$$ where the nearest point changes from the lower left vertex to the bottom edge of the rectangle. So we must consider these three cases separately: $$\theta \in \left[0, \arcsin \frac{h}{2} \right), \\ \theta \in \left[\arcsin \frac{h}{2}, \arccos \frac{w}{2} \right], \\ \theta \in \left(\arccos \frac{w}{2}, \frac{\pi}{2}\right].$$

In all three cases, we first let the yellow point be $(x,y) = (\cos \theta, \sin \theta)$. Then in the first case, the point nearest to the center is $$(p,q) = (\cos \theta - w/2, 0),$$ because among all points on the left edge of the rectangle, the one that minimizes the distance to the center is the one with zero $y$-coordinate. We must then find a translation $$(x',y') = (x,y) + (a,b), \\ (p', q') = (p,q) + (a,b)$$ such that $(x',y')$ has angle $\theta$ (i.e., $\frac{y'}{x'} = \tan \theta$), and $(p')^2 + (q'^2) = 1$--that is, the translation puts $(p,q)$ on the circumference. This results in the system $$\frac{b + \sin \theta }{a + \cos \theta} = \tan \theta, \\ (\cos \theta - w/2 + a)^2 + b^2 = 1.$$ Although it is only quadratic, the result is lengthy so we will show it without proof: $$(a,b) = \left(\frac{1}{2} \cos^2 \theta \left(\sqrt{\tan^2 \theta \left(4 w \cos \theta - w^2 + 4\right) + 4 \cos ^2 \theta } - 2 \cos \theta + w \right), \\ \frac{1}{2} \sin \theta \cos \theta \left(\sqrt{\tan^2 \theta \left(4 w \cos \theta - w^2 + 4 \right) + 4 \cos^2 \theta } - 2 \cos \theta + w \right)\right).$$ Then the new center of the rectangle is $(x',y') = (x,y) + (a,b)$.

For the second case, the vertex $(\cos \theta - w/2, \sin \theta - h/2)$ is the nearest point, and the system we need to solve is $$\frac{b + \sin \theta }{a + \cos \theta} = \tan \theta, \\ (\cos \theta - w/2 + a)^2 + (\sin \theta - h/2 + b)^2 = 1.$$ This yields

$$(a,b) = K(h,w,\theta) (\cos \theta, \sin \theta)$$ where $$K(h,w,\theta) = \frac{1}{2} \left( -2 + h \sin \theta + \cos \theta \left(w + \sqrt{4 - h^2 + 2 h w \tan \theta - (w^2 - 4) \tan^2 \theta } \right) \right). \tag{1}$$

The remaining case is just more of the same kind of calculation, so I have omitted it here. As you can see, the result is not trivial. We could have saved some effort by solving the second case first to obtain the general solution for $K$, then recognized that the (literally!) edge cases correspond to the choices $h = 2 \sin \theta$ or $w = 2 \cos \theta$. We can put it all together in an animation. This is for $r = 1$, $w = 1/2$, $h = 1/4$:

enter image description here