Axis aligned rectangle inscribed in rotated rectangle
$begingroup$
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:
- The center of $P$ should be at the center of $R'$ (and $R$)
- All points inside $P$ should also be inside $R'$
- $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: https://stackoverflow.com/questions/5789239/calculate-largest-rectangle-in-a-rotated-rectangle
geometry
$endgroup$
add a comment |
$begingroup$
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:
- The center of $P$ should be at the center of $R'$ (and $R$)
- All points inside $P$ should also be inside $R'$
- $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: https://stackoverflow.com/questions/5789239/calculate-largest-rectangle-in-a-rotated-rectangle
geometry
$endgroup$
$begingroup$
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...)
$endgroup$
– Rahul
Feb 24 '13 at 17:45
$begingroup$
If you are still interested I'll write up a solution.
$endgroup$
– Christian Blatter
Sep 16 '17 at 12:19
add a comment |
$begingroup$
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:
- The center of $P$ should be at the center of $R'$ (and $R$)
- All points inside $P$ should also be inside $R'$
- $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: https://stackoverflow.com/questions/5789239/calculate-largest-rectangle-in-a-rotated-rectangle
geometry
$endgroup$
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:
- The center of $P$ should be at the center of $R'$ (and $R$)
- All points inside $P$ should also be inside $R'$
- $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: https://stackoverflow.com/questions/5789239/calculate-largest-rectangle-in-a-rotated-rectangle
geometry
geometry
edited May 23 '17 at 12:39
Community♦
1
1
asked Feb 24 '13 at 17:27
Magnus HoffMagnus Hoff
1212
1212
$begingroup$
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...)
$endgroup$
– Rahul
Feb 24 '13 at 17:45
$begingroup$
If you are still interested I'll write up a solution.
$endgroup$
– Christian Blatter
Sep 16 '17 at 12:19
add a comment |
$begingroup$
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...)
$endgroup$
– Rahul
Feb 24 '13 at 17:45
$begingroup$
If you are still interested I'll write up a solution.
$endgroup$
– Christian Blatter
Sep 16 '17 at 12:19
$begingroup$
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...)
$endgroup$
– Rahul
Feb 24 '13 at 17:45
$begingroup$
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...)
$endgroup$
– Rahul
Feb 24 '13 at 17:45
$begingroup$
If you are still interested I'll write up a solution.
$endgroup$
– Christian Blatter
Sep 16 '17 at 12:19
$begingroup$
If you are still interested I'll write up a solution.
$endgroup$
– Christian Blatter
Sep 16 '17 at 12:19
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
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'$.
$endgroup$
add a comment |
$begingroup$
I ended up using this implementation in JavaScript:
function getCropCoordinates(angleInRadians, imageDimensions) {
var ang = angleInRadians;
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 :)
$endgroup$
$begingroup$
Also, it turns out, this fails for extremely narrow $R$ :( Will post updates as I debug...
$endgroup$
– Magnus Hoff
Feb 25 '13 at 16:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f313094%2faxis-aligned-rectangle-inscribed-in-rotated-rectangle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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'$.
$endgroup$
add a comment |
$begingroup$
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'$.
$endgroup$
add a comment |
$begingroup$
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'$.
$endgroup$
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'$.
answered Feb 24 '13 at 17:54
Emanuele PaoliniEmanuele Paolini
17.9k22052
17.9k22052
add a comment |
add a comment |
$begingroup$
I ended up using this implementation in JavaScript:
function getCropCoordinates(angleInRadians, imageDimensions) {
var ang = angleInRadians;
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 :)
$endgroup$
$begingroup$
Also, it turns out, this fails for extremely narrow $R$ :( Will post updates as I debug...
$endgroup$
– Magnus Hoff
Feb 25 '13 at 16:28
add a comment |
$begingroup$
I ended up using this implementation in JavaScript:
function getCropCoordinates(angleInRadians, imageDimensions) {
var ang = angleInRadians;
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 :)
$endgroup$
$begingroup$
Also, it turns out, this fails for extremely narrow $R$ :( Will post updates as I debug...
$endgroup$
– Magnus Hoff
Feb 25 '13 at 16:28
add a comment |
$begingroup$
I ended up using this implementation in JavaScript:
function getCropCoordinates(angleInRadians, imageDimensions) {
var ang = angleInRadians;
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 :)
$endgroup$
I ended up using this implementation in JavaScript:
function getCropCoordinates(angleInRadians, imageDimensions) {
var ang = angleInRadians;
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 :)
edited May 23 '17 at 12:39
Community♦
1
1
answered Feb 25 '13 at 16:10
Magnus HoffMagnus Hoff
1212
1212
$begingroup$
Also, it turns out, this fails for extremely narrow $R$ :( Will post updates as I debug...
$endgroup$
– Magnus Hoff
Feb 25 '13 at 16:28
add a comment |
$begingroup$
Also, it turns out, this fails for extremely narrow $R$ :( Will post updates as I debug...
$endgroup$
– Magnus Hoff
Feb 25 '13 at 16:28
$begingroup$
Also, it turns out, this fails for extremely narrow $R$ :( Will post updates as I debug...
$endgroup$
– Magnus Hoff
Feb 25 '13 at 16:28
$begingroup$
Also, it turns out, this fails for extremely narrow $R$ :( Will post updates as I debug...
$endgroup$
– Magnus Hoff
Feb 25 '13 at 16:28
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f313094%2faxis-aligned-rectangle-inscribed-in-rotated-rectangle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
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...)
$endgroup$
– Rahul
Feb 24 '13 at 17:45
$begingroup$
If you are still interested I'll write up a solution.
$endgroup$
– Christian Blatter
Sep 16 '17 at 12:19