# Axis aligned rectangle inscribed in rotated rectangle

I start with an axis aligned rectangle, $R$, that I rotate by the angle $\theta$ to get $R'$.

Afterwards I'd like to identify another axis aligned rectangle, $P$ with the following additional constraints:

1. The center of $P$ should be at the center of $R'$ (and $R$)
2. All points inside $P$ should also be inside $R'$
3. $P$ should be as big as possible, area wise

What is the width and height of $P$, in terms of the width and height of $R$ and $\theta$?

I'm not sure if these criteria uniquely identify a rectangle. If they do not, please enlighten me :)

I've attempted applying my brain to the problem, but it appears I am enough out of practice that this is too hard. Hence this cry for help ;)

I've found a related question that seems to be the same question, but the answer is for another question: Rectangle in rotated bounding rectangle

I think I've also found the same question on stack overflow, but the answers are messy, and the ones I've managed to read and put into practice turn out to be wrong: http://stackoverflow.com/questions/5789239/calculate-largest-rectangle-in-a-rotated-rectangle

-
This previous question is very related: math.stackexchange.com/questions/61005/…. (I ought to work out the details of my answer there at some point...) –  Rahul Feb 24 '13 at 17:45

Let $(\pm x,\pm y)$ be the coordinate of the vertices of $P$. The area of $P$ is $4xy$. At least one of the vertices of $P$ must lie on an edge of $R'$ (otherwise you can increase $P$ by a scaling) and hence also the opposite vertex is on the opposide edge of $R'$. Suppose $(x,y)$ is on one edge of $R'$ and suppose that the edge of $R'$ is contained in the line $ax+by=c$. Then, if $(x,y)$ is internal to the edge, you can apply the Lagrange multipliers to find that $(y,x)=\lambda(a,b)$ which together with the condition $ax+by=c$ gives the coordinates of the vertex: $$\begin{cases} x = \frac{2c}{a}\\\\ y = \frac{2c}{b}\\ \end{cases}$$

You must try this solution with both edges of $R'$ and discard the solution where the other vertices are outside $R'$.

-

I ended up using this implementation in JavaScript:

function getCropCoordinates(angleInRadians, imageDimensions) {
var img = imageDimensions;

var quadrant = Math.floor(ang / (Math.PI / 2)) & 3;
var sign_alpha = (quadrant & 1) === 0 ? ang : Math.PI - ang;
var alpha = (sign_alpha % Math.PI + Math.PI) % Math.PI;

var bb = {
w: img.w * Math.cos(alpha) + img.h * Math.sin(alpha),
h: img.w * Math.sin(alpha) + img.h * Math.cos(alpha)
};

var gamma = img.w < img.h ? Math.atan2(bb.w, bb.h) : Math.atan2(bb.h, bb.w);

var delta = Math.PI - alpha - gamma;

var length = img.w < img.h ? img.h : img.w;
var d = length * Math.cos(alpha);
var a = d * Math.sin(alpha) / Math.sin(delta);

var y = a * Math.cos(gamma);
var x = y * Math.tan(gamma);

return {
x: x,
y: y,
w: bb.w - 2 * x,
h: bb.h - 2 * y
};
}


I'd write down the deduction in LaTeX form, but I didn't deduce it :)

-
Also, it turns out, this fails for extremely narrow $R$ :( Will post updates as I debug... –  Magnus Hoff Feb 25 '13 at 16:28