Determine if a Rectangle is Inside, Overlaps, Doesn't Overlaps Another Rectangle












1












$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










share|cite|improve this question









$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
















1












$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










share|cite|improve this question









$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














1












1








1





$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










share|cite|improve this question









$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






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










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


















  • $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










1 Answer
1






active

oldest

votes


















1












$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}$$






share|cite|improve this answer











$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











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
});


}
});














draft saved

draft discarded


















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









1












$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}$$






share|cite|improve this answer











$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
















1












$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}$$






share|cite|improve this answer











$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














1












1








1





$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}$$






share|cite|improve this answer











$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}$$







share|cite|improve this answer














share|cite|improve this answer



share|cite|improve this answer








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


















  • $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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory