Determine if a Rectangle is Inside, Overlaps, Doesn't Overlaps Another Rectangle
$begingroup$
Using the center's x- and y-coordinates, width and length of each rectangle, determine if the second rectangle is inside, overlaps or doesn't overlap the first rectangle.
I know that if I divide the width or height by 2, I will get the reach of both sides of rectangle, horizontally and vertically. I can't seem to come up with a formula to solve for each condition.
Here's the list of variables I'm using:
Rectangle 1: x1, y1, width1, height1
Rectangle 2: x2, y2, width2, height2
Here's the formulas I'm coming up with:
The absolute value of the horizontal distance = x1 - x2
The absolute value of the vertical distance = y1 - y2
Or is it better to subtract the smaller number from the larger one?
Any ideas? Thank you
geometry rectangles
$endgroup$
add a comment |
$begingroup$
Using the center's x- and y-coordinates, width and length of each rectangle, determine if the second rectangle is inside, overlaps or doesn't overlap the first rectangle.
I know that if I divide the width or height by 2, I will get the reach of both sides of rectangle, horizontally and vertically. I can't seem to come up with a formula to solve for each condition.
Here's the list of variables I'm using:
Rectangle 1: x1, y1, width1, height1
Rectangle 2: x2, y2, width2, height2
Here's the formulas I'm coming up with:
The absolute value of the horizontal distance = x1 - x2
The absolute value of the vertical distance = y1 - y2
Or is it better to subtract the smaller number from the larger one?
Any ideas? Thank you
geometry rectangles
$endgroup$
$begingroup$
You may possibly need more variables , as in your case , the rectangles can be rotated , which can consequently yield more than $1$ answer . Or are you assuming that the sides are parallel to the axes ?
$endgroup$
– Sinπ
Jan 25 at 1:23
$begingroup$
Yes, the sides will be parallel to the axis. Rotation won't be necessary.
$endgroup$
– donfontaine12
Jan 25 at 1:33
add a comment |
$begingroup$
Using the center's x- and y-coordinates, width and length of each rectangle, determine if the second rectangle is inside, overlaps or doesn't overlap the first rectangle.
I know that if I divide the width or height by 2, I will get the reach of both sides of rectangle, horizontally and vertically. I can't seem to come up with a formula to solve for each condition.
Here's the list of variables I'm using:
Rectangle 1: x1, y1, width1, height1
Rectangle 2: x2, y2, width2, height2
Here's the formulas I'm coming up with:
The absolute value of the horizontal distance = x1 - x2
The absolute value of the vertical distance = y1 - y2
Or is it better to subtract the smaller number from the larger one?
Any ideas? Thank you
geometry rectangles
$endgroup$
Using the center's x- and y-coordinates, width and length of each rectangle, determine if the second rectangle is inside, overlaps or doesn't overlap the first rectangle.
I know that if I divide the width or height by 2, I will get the reach of both sides of rectangle, horizontally and vertically. I can't seem to come up with a formula to solve for each condition.
Here's the list of variables I'm using:
Rectangle 1: x1, y1, width1, height1
Rectangle 2: x2, y2, width2, height2
Here's the formulas I'm coming up with:
The absolute value of the horizontal distance = x1 - x2
The absolute value of the vertical distance = y1 - y2
Or is it better to subtract the smaller number from the larger one?
Any ideas? Thank you
geometry rectangles
geometry rectangles
asked Jan 25 at 1:18
donfontaine12donfontaine12
82
82
$begingroup$
You may possibly need more variables , as in your case , the rectangles can be rotated , which can consequently yield more than $1$ answer . Or are you assuming that the sides are parallel to the axes ?
$endgroup$
– Sinπ
Jan 25 at 1:23
$begingroup$
Yes, the sides will be parallel to the axis. Rotation won't be necessary.
$endgroup$
– donfontaine12
Jan 25 at 1:33
add a comment |
$begingroup$
You may possibly need more variables , as in your case , the rectangles can be rotated , which can consequently yield more than $1$ answer . Or are you assuming that the sides are parallel to the axes ?
$endgroup$
– Sinπ
Jan 25 at 1:23
$begingroup$
Yes, the sides will be parallel to the axis. Rotation won't be necessary.
$endgroup$
– donfontaine12
Jan 25 at 1:33
$begingroup$
You may possibly need more variables , as in your case , the rectangles can be rotated , which can consequently yield more than $1$ answer . Or are you assuming that the sides are parallel to the axes ?
$endgroup$
– Sinπ
Jan 25 at 1:23
$begingroup$
You may possibly need more variables , as in your case , the rectangles can be rotated , which can consequently yield more than $1$ answer . Or are you assuming that the sides are parallel to the axes ?
$endgroup$
– Sinπ
Jan 25 at 1:23
$begingroup$
Yes, the sides will be parallel to the axis. Rotation won't be necessary.
$endgroup$
– donfontaine12
Jan 25 at 1:33
$begingroup$
Yes, the sides will be parallel to the axis. Rotation won't be necessary.
$endgroup$
– donfontaine12
Jan 25 at 1:33
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
If any of the following are true, the rectangles don't intersect, otherwise they do:
$$x_1+frac{w_1}{2} < x_2-frac{w_2}{2}$$
$$x_1-frac{w_1}{2} > x_2+frac{w_2}{2}$$
$$y_1+frac{h_1}{2} < y_2-frac{h_2}{2}$$
$$y_1-frac{h_1}{2} > y_2+frac{h_2}{2}$$
Edit
For the second rectangle to be inside the first, all of the following must be true:
$$x_2+frac{w_2}{2} le x_1+frac{w_1}{2}$$
$$x_2-frac{w_2}{2} ge x_1-frac{w_1}{2}$$
$$y_2+frac{h_2}{2} le y_1+frac{h_1}{2}$$
$$y_2-frac{h_2}{2} ge y_1-frac{h_1}{2}$$
$endgroup$
$begingroup$
What conditions will occur when the rectangle is completely inside?
$endgroup$
– donfontaine12
Jan 25 at 10:53
$begingroup$
It works for each condition now. Thank you.
$endgroup$
– donfontaine12
Jan 25 at 18:30
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%2f3086589%2fdetermine-if-a-rectangle-is-inside-overlaps-doesnt-overlaps-another-rectangle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
If any of the following are true, the rectangles don't intersect, otherwise they do:
$$x_1+frac{w_1}{2} < x_2-frac{w_2}{2}$$
$$x_1-frac{w_1}{2} > x_2+frac{w_2}{2}$$
$$y_1+frac{h_1}{2} < y_2-frac{h_2}{2}$$
$$y_1-frac{h_1}{2} > y_2+frac{h_2}{2}$$
Edit
For the second rectangle to be inside the first, all of the following must be true:
$$x_2+frac{w_2}{2} le x_1+frac{w_1}{2}$$
$$x_2-frac{w_2}{2} ge x_1-frac{w_1}{2}$$
$$y_2+frac{h_2}{2} le y_1+frac{h_1}{2}$$
$$y_2-frac{h_2}{2} ge y_1-frac{h_1}{2}$$
$endgroup$
$begingroup$
What conditions will occur when the rectangle is completely inside?
$endgroup$
– donfontaine12
Jan 25 at 10:53
$begingroup$
It works for each condition now. Thank you.
$endgroup$
– donfontaine12
Jan 25 at 18:30
add a comment |
$begingroup$
If any of the following are true, the rectangles don't intersect, otherwise they do:
$$x_1+frac{w_1}{2} < x_2-frac{w_2}{2}$$
$$x_1-frac{w_1}{2} > x_2+frac{w_2}{2}$$
$$y_1+frac{h_1}{2} < y_2-frac{h_2}{2}$$
$$y_1-frac{h_1}{2} > y_2+frac{h_2}{2}$$
Edit
For the second rectangle to be inside the first, all of the following must be true:
$$x_2+frac{w_2}{2} le x_1+frac{w_1}{2}$$
$$x_2-frac{w_2}{2} ge x_1-frac{w_1}{2}$$
$$y_2+frac{h_2}{2} le y_1+frac{h_1}{2}$$
$$y_2-frac{h_2}{2} ge y_1-frac{h_1}{2}$$
$endgroup$
$begingroup$
What conditions will occur when the rectangle is completely inside?
$endgroup$
– donfontaine12
Jan 25 at 10:53
$begingroup$
It works for each condition now. Thank you.
$endgroup$
– donfontaine12
Jan 25 at 18:30
add a comment |
$begingroup$
If any of the following are true, the rectangles don't intersect, otherwise they do:
$$x_1+frac{w_1}{2} < x_2-frac{w_2}{2}$$
$$x_1-frac{w_1}{2} > x_2+frac{w_2}{2}$$
$$y_1+frac{h_1}{2} < y_2-frac{h_2}{2}$$
$$y_1-frac{h_1}{2} > y_2+frac{h_2}{2}$$
Edit
For the second rectangle to be inside the first, all of the following must be true:
$$x_2+frac{w_2}{2} le x_1+frac{w_1}{2}$$
$$x_2-frac{w_2}{2} ge x_1-frac{w_1}{2}$$
$$y_2+frac{h_2}{2} le y_1+frac{h_1}{2}$$
$$y_2-frac{h_2}{2} ge y_1-frac{h_1}{2}$$
$endgroup$
If any of the following are true, the rectangles don't intersect, otherwise they do:
$$x_1+frac{w_1}{2} < x_2-frac{w_2}{2}$$
$$x_1-frac{w_1}{2} > x_2+frac{w_2}{2}$$
$$y_1+frac{h_1}{2} < y_2-frac{h_2}{2}$$
$$y_1-frac{h_1}{2} > y_2+frac{h_2}{2}$$
Edit
For the second rectangle to be inside the first, all of the following must be true:
$$x_2+frac{w_2}{2} le x_1+frac{w_1}{2}$$
$$x_2-frac{w_2}{2} ge x_1-frac{w_1}{2}$$
$$y_2+frac{h_2}{2} le y_1+frac{h_1}{2}$$
$$y_2-frac{h_2}{2} ge y_1-frac{h_1}{2}$$
edited Jan 25 at 16:37
answered Jan 25 at 2:20
JensJens
3,94521031
3,94521031
$begingroup$
What conditions will occur when the rectangle is completely inside?
$endgroup$
– donfontaine12
Jan 25 at 10:53
$begingroup$
It works for each condition now. Thank you.
$endgroup$
– donfontaine12
Jan 25 at 18:30
add a comment |
$begingroup$
What conditions will occur when the rectangle is completely inside?
$endgroup$
– donfontaine12
Jan 25 at 10:53
$begingroup$
It works for each condition now. Thank you.
$endgroup$
– donfontaine12
Jan 25 at 18:30
$begingroup$
What conditions will occur when the rectangle is completely inside?
$endgroup$
– donfontaine12
Jan 25 at 10:53
$begingroup$
What conditions will occur when the rectangle is completely inside?
$endgroup$
– donfontaine12
Jan 25 at 10:53
$begingroup$
It works for each condition now. Thank you.
$endgroup$
– donfontaine12
Jan 25 at 18:30
$begingroup$
It works for each condition now. Thank you.
$endgroup$
– donfontaine12
Jan 25 at 18:30
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%2f3086589%2fdetermine-if-a-rectangle-is-inside-overlaps-doesnt-overlaps-another-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$
You may possibly need more variables , as in your case , the rectangles can be rotated , which can consequently yield more than $1$ answer . Or are you assuming that the sides are parallel to the axes ?
$endgroup$
– Sinπ
Jan 25 at 1:23
$begingroup$
Yes, the sides will be parallel to the axis. Rotation won't be necessary.
$endgroup$
– donfontaine12
Jan 25 at 1:33