# Resizing a rectangle to always fit into its unrotated space

(For those coming here looking for answers to rectangle problems it may help to see the related (and solved) question: Given a width, height and angle of a rectangle, and an allowed final size, determine how large or small it must be to fit into the area)

Suppose I had a to-be-rotated rectangle that I wanted to fill a space of 100x20.

But upon rotation, instead of the ratio of the sides being maintained, I wanted the rectangle to scale its with and height to always fit inside of the original space. Here is an example: I have the rectangle in red and its original space in light blue, with the top-left corner marked as a blue circle. The numbers to the right represent degrees.

As the red rectangle rotates it changes its dimensions to always remain inside of the original size.

Such that what was once a 100x20 rectangle is now at 45 degrees a 14x14 (or thereabouts) rectangle.

How could the size of the red rectangle be determined for a given angle?

• If the ratio of the side lengths can be anything, the problem as stated is underconstrained. Do you want to maximize area or something like that? – Henning Makholm Aug 31 '11 at 18:30
• Your red rectangle is rotating with respect to which point? – J. M. is a poor mathematician Aug 31 '11 at 18:30
• Yes, maximizing area and rotating about the center. – Simon Sarris Aug 31 '11 at 18:31

First of all, there may be multiple rectangles that satisfy your conditions, e.g.

if you want specific angle of rectangle, or

if you want to specify a point on the boundary.

However, there is a special case where there is at most only once rectangle, i.e. if you assume that it has to touch both of your boundaries. In such case it is easy to compute it. Place the origin (point $(0,0)$) in the center of the boundary (the center of the circle), and let $(C_x,C_y)$ be the top right boundary corner (the boundary rectangle has size $2C_x \times 2C_y$ ). Let $(x,y)$ be the top right vertex of small rectangle, then it satisfies conditions ($\alpha > 0$ means counterclockwise rotation):

\begin{align*} \left[\begin{matrix}x'\\\ C_y\end{matrix}\right] &= \left[\begin{matrix}\cos\alpha&-\sin\alpha\\\sin\alpha&\cos\alpha\end{matrix}\right]\left[\begin{matrix}x\\\y\end{matrix}\right] \\\ \left[\begin{matrix}C_x\\\ y'\end{matrix}\right] &= \left[\begin{matrix}\cos\alpha&-\sin\alpha\\\sin\alpha&\cos\alpha\end{matrix}\right]\left[\begin{matrix}x\\\ -y\end{matrix}\right] \end{align*}

where $x'$ and $y'$ are just placeholders. Extracting appropriate rows from those formulae, we can transform that into one equation (notice the lack of minus sign in the matrix):

\begin{align*} \left[\begin{matrix}C_x\\\ C_y\end{matrix}\right] &= \left[\begin{matrix}\cos\alpha&\sin\alpha\\\sin\alpha&\cos\alpha\end{matrix}\right]\left[\begin{matrix}x\\\ y\end{matrix}\right] \end{align*}

with solution being:

\begin{align*} \left[\begin{matrix}\cos\alpha&\sin\alpha\\\sin\alpha&\cos\alpha\end{matrix}\right]^{-1} \left[\begin{matrix}C_x\\\ C_y\end{matrix}\right] &= \left[\begin{matrix}x\\\ y\end{matrix}\right] \\\ \sec{2\alpha}\left[\begin{matrix}\cos\alpha&-\sin\alpha\\\ -\sin\alpha&\cos\alpha\end{matrix}\right] \left[\begin{matrix}C_x\\\ C_y\end{matrix}\right] &= \left[\begin{matrix}x\\\ y\end{matrix}\right] \end{align*}

Please note, that this may not have a proper solution if $\alpha$ is to big!

Edit: Ok, I missed the comment about maximizing the area. Then again, consider this example:

The gray figure is a rhombus (it was created by rotating the black rectangle by $2\alpha$ ). To get the inscribed rectangle with the greatest area, consider the case when the rhombus would be a square--the greatest area would be when each rectangle vertex splits the rhombus edge in half (because then it is also a square and that is the rectangle with greatest area and given perimeter). But we can scale our rhombus (that may not be a square) so that is a square, obtain the solution there, and then scale back (the area will scale accordingly)! In conclusion the rectangle of greatest area will split the rhombus edges in half.

How to compute it? You could do it using the same approach:

\begin{align*} \left[\begin{matrix}x'\\\ C_y\end{matrix}\right] &= \left[\begin{matrix}\cos\alpha&-\sin\alpha\\\sin\alpha&\cos\alpha\end{matrix}\right]\left[\begin{matrix}x\\\y\end{matrix}\right] \\\ \left[\begin{matrix}x''\\\ -C_y\end{matrix}\right] &= \left[\begin{matrix}\cos(-\alpha)&-\sin(-\alpha)\\\sin(-\alpha)&\cos(-\alpha)\end{matrix}\right] \left[\begin{matrix}x\\\ -y\end{matrix}\right] \end{align*}

However, one can do it simpler: the vertex of the inscribed rectangle splits the gray edge in half, so $$2x\sin\alpha = C_y = 2y\cos\alpha\,.$$ This works if $C_x > C_y$, otherwise you need to do the same for $C_x$ instead. Moreover, even if $C_x > C_y$, you still need to check if the rotated rectangle fits into the boundary (because it may be that $C_x = C_y + \varepsilon$ ), if not, the solution from previous part will do.

Hope that helps ;-)

• This is only true when the black rectangle is very wide: otherwise the figure will be an octagon instead of a rhombus. – Generic Human Mar 14 '12 at 16:48
• @GenericHuman Yes, but then the first part of the post applies, i.e. there are two cases: the rectangle is constrained by the other edge (then the first part applies) or not (and then the second works, independent of rhombuses and octagons). – dtldarek Mar 14 '12 at 18:18

Consider the following special case, of which all other cases are reflections, I think, and which is the left-right reflection of your example image.

• The rectangle is wider than it is high.
• The rotation angle, call it $θ_r$, is counterclockwise and between 0 and π/2 (90°).

Since we are rotating about a center, let's throw in some polar coordinates, with the center of the rectangles being the origin.

Consider the point which is the upper-right corner of the rectangle, and call it $(x_p,y_p)$. Its polar coordinates are $$(r_p,θ_p) = (\sqrt{x_p^2+y_p^2}, \operatorname{atan2}(y_p, x_p)).$$ Note that $θ_r$ is not $θ_p$, but $θ_r + θ_p$ is the angle of the upper-right point of the rotated rectangle.

We now need to find the polar radius of that point. Note that, for rotations less than π/2, that point is somewhere on the line $y = y_p$. The polar equation of that line is $r = y_p \csc θ$, so the point is $$(y_p \csc(θ_r+θ_p), θ_r+θ_p).$$

(If what you want is not the corner points of the new rectangle, but merely a scaling factor, then stop here and take the ratio of the half-diagonals of the original and rotated rectangles, that is, $\frac{y_p \csc(θ_r+θ_p)}{r_p}$.)

We then find the Cartesian form of that point, and algebraically simplify:

\begin{align*}&(y_p \csc(θ_r+θ_p) \cos(θ_r+θ_p), y_p \csc(θ_r+θ_p) \sin(θ_r+θ_p)) \\ = &(y_p \cot(θ_r+θ_p), y_p) \\ = &(y_p \cot(θ_r+\operatorname{atan2}(y_p, x_p)), y_p) \end{align*}

This gives us the upper right corner. The lower left is of course its mirror image; we now need the lower right corner (and its mirror the upper left). This corner has the same radius, but the angle $θ_r-θ_p$, so the formula

\begin{align*} &(y_p \csc(θ_r+θ_p) \cos(θ_r-θ_p), y_p \csc(θ_r+θ_p) \sin(θ_r-θ_p)) \\ \end{align*}

Trigonometrically simplifying this expression has exceeded my enthusiasm, but according to my plots it works. I also note that as the angle changes, all of the points follow straight lines, which suggests that there might be some reparameterization of the problem such that it is mostly linear formulas and one trigonometric formula of the rotation angle and aspect ratio.

I would leave this as a comment as I don't have time to work it out to completion, but it's a little too long for that.

Two observations should simplify the route to the solution:

1. Instead of rotating the red rectangle, let its orientation be fixed and rotate the blue containing rectangle in the opposite direction instead. Now let two corners of the red rectangle be $(0,0)$ and $(x,y)$. All you have to do is maximize $xy$.

2. I think one can show that because of symmetry, only the shorter side of the blue rectangle matters. If that length is $w$, you have the constraint that $(x,y)$ lies on the line at distance $w$ from the origin, oriented at angle $\theta$. Now you could go ahead and directly apply calculus here, but it's better to consider Lagrange multipliers and note that the gradient of the objective $xy$ must be normal to the constraint line. Since the gradient of $xy$ has a particularly simple form, this should directly lead to a neat solution.

Of course, after this you still have to transform the rectangle back to the original orientation and center it, but that's fairly routine stuff.

Assume your outer rectangle $R$ is of size $(2w,2h)$ with $w\geq h$, and $0<\theta<\pi/2$. In the coordinate system centered around the center of $R$ and rotated by angle $\theta$, the centered rectangle with upper right corner $(x,y)$ and with axes parallel to the coordinate system's axes is contained within $R$ if and only if \begin{aligned} x\sin\theta + y\cos\theta&\leq h\\ x\cos\theta + y\sin\theta&\leq w\\ x,y&\geq 0 \end{aligned} This is a convex polygon $P$ containing 0 and the objective function $f(x,y)=xy$ is non-negative and 2-homogeneous, so the maximum will be reached on the boundary. Since $f$ is minimal when $x=0$ or $y=0$ we only need to consider points where at least one of \begin{aligned} x\sin\theta + y\cos\theta = h &\qquad(E_h)\\ x\sin\theta + y\cos\theta = w &\qquad(E_w)\\ \end{aligned} is true, that is $$\arg\max_P f \subseteq P\cap(E_h\cup E_w)$$

In the general case $\theta\neq\pi/4$, $E_h$ and $E_w$ intersect at a unique point $(x_0,y_0) = 1/\cos 2\theta \cdot \left(w\cos\theta - h\sin\theta, h\cos\theta - w\sin\theta\right)$.

$f$ is concave on $E_h$ and $E_w$, so $$\arg\max_P f \subseteq (P\cap\arg\max_{E_h} f) \cup (P\cap\arg\max_{E_w} f) \cup \{(x_0,y_0)\}$$

On $E_h$, a quick calculation shows that the maximum of $f$ is reached at $(x,y)=h/2\cdot\left(1/\sin\theta,1/\cos\theta\right)$ which lies inside $P$ iff $w/h\geq 1/\sin 2\theta$.

On $E_w$, $P\cap\arg\max_{E_w} f$ is empty, otherwise we would have $h/w\geq 1/\sin 2\theta>1$ which would contradict $w\geq h$.

Comparing $f(x,y)$ in the two cases, we find that the maximum is:

• $(x,y)=h/2\cdot\left(1/\sin\theta,1/\cos\theta\right)$ when $w/h\geq 1/\sin 2\theta$
• $(x,y) = 1/\cos 2\theta \cdot \left(w\cos\theta - h\sin\theta, h\cos\theta - w\sin\theta\right)$ when $w/h\leq 1/\sin 2\theta$

When $\theta=\pi/4$, the maximum is $x=y=h/2$.