How much do two rectangles overlap?
I have two rectangles a and b with their sides parallel to the axes of the coordinate system. I have their co-ordinates as x1,y1,x2,y2.
I'm trying to determine, not only do they overlap, but HOW MUCH do they overlap? I'm trying to figure out if they're really the same rectangle give or take a bit of wiggle room. So is their area 95% the same?
Any help in calculating the % of overlap?
algorithm math geometry bounding-box
|
show 5 more comments
I have two rectangles a and b with their sides parallel to the axes of the coordinate system. I have their co-ordinates as x1,y1,x2,y2.
I'm trying to determine, not only do they overlap, but HOW MUCH do they overlap? I'm trying to figure out if they're really the same rectangle give or take a bit of wiggle room. So is their area 95% the same?
Any help in calculating the % of overlap?
algorithm math geometry bounding-box
2
What have you come up with so far?
– Gumbo
Feb 17 '12 at 7:20
you mean to say, 2 rectangles are in a box with given co-ordinates?
– sgowd
Feb 17 '12 at 7:20
@sans481 Yes I corrected the question thanks.
– Patrick Collins
Feb 17 '12 at 7:22
Did you give a try on that? If you have, what did you do?
– sgowd
Feb 17 '12 at 7:26
1
The answer from user3025064 is the right answer.
– prometeu
Sep 29 '17 at 18:18
|
show 5 more comments
I have two rectangles a and b with their sides parallel to the axes of the coordinate system. I have their co-ordinates as x1,y1,x2,y2.
I'm trying to determine, not only do they overlap, but HOW MUCH do they overlap? I'm trying to figure out if they're really the same rectangle give or take a bit of wiggle room. So is their area 95% the same?
Any help in calculating the % of overlap?
algorithm math geometry bounding-box
I have two rectangles a and b with their sides parallel to the axes of the coordinate system. I have their co-ordinates as x1,y1,x2,y2.
I'm trying to determine, not only do they overlap, but HOW MUCH do they overlap? I'm trying to figure out if they're really the same rectangle give or take a bit of wiggle room. So is their area 95% the same?
Any help in calculating the % of overlap?
algorithm math geometry bounding-box
algorithm math geometry bounding-box
edited Jun 12 '15 at 16:55
Patrick Collins
asked Feb 17 '12 at 7:17
Patrick CollinsPatrick Collins
2,12521828
2,12521828
2
What have you come up with so far?
– Gumbo
Feb 17 '12 at 7:20
you mean to say, 2 rectangles are in a box with given co-ordinates?
– sgowd
Feb 17 '12 at 7:20
@sans481 Yes I corrected the question thanks.
– Patrick Collins
Feb 17 '12 at 7:22
Did you give a try on that? If you have, what did you do?
– sgowd
Feb 17 '12 at 7:26
1
The answer from user3025064 is the right answer.
– prometeu
Sep 29 '17 at 18:18
|
show 5 more comments
2
What have you come up with so far?
– Gumbo
Feb 17 '12 at 7:20
you mean to say, 2 rectangles are in a box with given co-ordinates?
– sgowd
Feb 17 '12 at 7:20
@sans481 Yes I corrected the question thanks.
– Patrick Collins
Feb 17 '12 at 7:22
Did you give a try on that? If you have, what did you do?
– sgowd
Feb 17 '12 at 7:26
1
The answer from user3025064 is the right answer.
– prometeu
Sep 29 '17 at 18:18
2
2
What have you come up with so far?
– Gumbo
Feb 17 '12 at 7:20
What have you come up with so far?
– Gumbo
Feb 17 '12 at 7:20
you mean to say, 2 rectangles are in a box with given co-ordinates?
– sgowd
Feb 17 '12 at 7:20
you mean to say, 2 rectangles are in a box with given co-ordinates?
– sgowd
Feb 17 '12 at 7:20
@sans481 Yes I corrected the question thanks.
– Patrick Collins
Feb 17 '12 at 7:22
@sans481 Yes I corrected the question thanks.
– Patrick Collins
Feb 17 '12 at 7:22
Did you give a try on that? If you have, what did you do?
– sgowd
Feb 17 '12 at 7:26
Did you give a try on that? If you have, what did you do?
– sgowd
Feb 17 '12 at 7:26
1
1
The answer from user3025064 is the right answer.
– prometeu
Sep 29 '17 at 18:18
The answer from user3025064 is the right answer.
– prometeu
Sep 29 '17 at 18:18
|
show 5 more comments
9 Answers
9
active
oldest
votes
Compute the area of the intersection, which is a rectangle too:
SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
From there you compute the area of the union:
SU = SA + SB - SI
And you can consider the ratio
SI / SU
(100% in case of a perfect overlap, down to 0%).
wow. That is exactly what I was after thank you! I wasn't thinking about it correctly. The introduction of the union concept is what I was missing. Thanks!.
– Patrick Collins
Feb 19 '12 at 4:59
Thanks man! Works like a charm.
– skrat
Oct 10 '12 at 12:44
Is SA and SB the area of A and B? And for SI, the more i move my rectangle to the right bottom the higher the values get.
– clankill3r
Apr 28 '14 at 16:33
1
Give a numerical example.
– Yves Daoust
Apr 28 '14 at 19:08
7
It'd be so much better if he labeled what the variables are.
– Louis Hong
Jun 1 '15 at 0:24
|
show 2 more comments
The formula for intersection will be
SI= Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
then the union will be S=SA+SB-SI
And finally, the ratio will be SI / S.
ratio should beSI/S
– warmspringwinds
Jul 7 '15 at 22:20
@warmspringwinds: I agree
– Joko
Sep 7 '15 at 14:05
14
Hmmm. This answer looks more or less identical to the highest-rated answer, which predates this one by nearly two years . . .
– imallett
Jan 8 '16 at 1:59
2
This answer does not address the case of one rectangle inside another.
– user1084113
Jun 6 '16 at 0:21
3
@PatrickCollins My point was that this answer seems plagiarized from Yves Daoust's--crude copy+paste plagiarism at that. However, while it's unclear to me what commentary you're referring to (tommy.qichang's single comment?), I've just noticed that the first formula is subtly different. If this fixes an error in Yves's answer, I suggest someone edit this answer to both acknowledge Yves's, and to explain the error it contained. Ideally, user3025064 would have edit-fixed Yves's answer instead, but next best would be at least to explain why there are two nearly identical answers now.
– imallett
Feb 12 '17 at 5:46
|
show 2 more comments
I recently ran into this problem as well and applied Yves' answer, but somehow that led to the wrong area size, so I rewrote it.
Assuming two rectangles A and B, find out how much they overlap and if so, return the area size:
IF A.right < B.left OR A.left > B.right
OR A.bottom < B.top OR A.top > B.bottom THEN RETURN 0
width := IF A.right > B.right THEN B.right - A.left ELSE A.right - B.left
height := IF A.bottom > B.bottom THEN B.bottom - A.top ELSE A.bottom - B.top
RETURN width * height
add a comment |
Just fixing previous answers so that the ratio is between 0 and 1 (using Python):
# (x1,y1) top-left coord, (x2,y2) bottom-right coord, (w,h) size
A = {'x1': 0, 'y1': 0, 'x2': 99, 'y2': 99, 'w': 100, 'h': 100}
B = {'x1': 0, 'y1': 0, 'x2': 49, 'y2': 49, 'w': 50, 'h': 50}
# overlap between A and B
SA = A['w']*A['h']
SB = B['w']*B['h']
SI = np.max([ 0, 1 + np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, 1 + np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ])
SU = SA + SB - SI
overlap_AB = float(SI) / float(SU)
print 'overlap between A and B: %f' % overlap_AB
# overlap between A and A
B = A
SB = B['w']*B['h']
SI = np.max([ 0, 1 + np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, 1 + np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ])
SU = SA + SB - SI
overlap_AA = float(SI) / float(SU)
print 'overlap between A and A: %f' % overlap_AA
The output will be:
overlap between A and B: 0.250000
overlap between A and A: 1.000000
1
Note. This answer uses NumPy.
– rayryeng
May 24 '17 at 6:05
@Alessio B how about a condition where one rectangle is inside other?
– prb
Nov 27 '18 at 7:57
add a comment |
While the accepted answer given is correct, I think it's worth exploring this answer in a way that will make the rationale for the answer completely obvious. This is too common an algorithm to have an incomplete (or worse, controversial) answer. Furthermore, with only a passing glance at the given formula, you may miss the beauty and extensibility of the algorithm, and the implicit decisions that are being made.
First, consider one way to define a two dimensional box is with:
- (x, y) for the top left point
- (x, y) for the bottom right point
This might look like:

I indicate the top left with a triangle and the bottom right with a circle. This is to avoid opaque syntax like x1, x2 for this example.
Two overlapping rectangles might look like this:

Notice that to find the overlap you're looking for the place where the orange and the blue collide:

Once you recognize this, it becomes obvious that overlap is the result of finding and multiplying these two darkened lines:

The length of each line is the minimum value of the circle point between the lines we wish to compare, minus the maximum value of the triangle points.

Here, I'm using the two-toned shape to show that both the orange and the blue are compared. The small letter after the shape indicates that the triangles are compared along that axis.
For example, in the top equation in this previous image you can see the orange and blue triangles are compared to look for the maximum value between the two. The attribute that is compared is the x attribute. The maximum x value between the orange and blue triangles is 210.
Another way to say the same thing is: the length of the line that can fit onto both of the lines that we are comparing is found by subtracting the closest point on the longest side of the line from the furthest point on the closest side of the line.

Finding those lines gives complete information of the overlapping areas.

Once you have this, finding the percentage of overlap is trivial:

But wait, if the orange rectangle does not overlap with the blue one then you're going to have a problem:

With this example, you get a -850 for our overlapping area, that can't be right. Even worse, if a detection doesn't overlap with either dimension (neither on the x or y axis) then you will still get a positive number because both dimensions are negative. This is why you see the Max(0, ...) * Max(0, ...) as part of the solution; it ensures that if any of the overlaps are negative you'll get a 0 back from your function.
The final formula in keeping with our symbology:

It's worth noting that using the max(0, ...) function may not be necessary. You may want to know if something overlaps along one of its dimensions rather than all of them; if you use max then you will obliterate that information. For that reason, consider how you want to deal with non-overlapping images. Normally, the max function is fine to use, but it's worth being aware what it's doing.
Finally, notice that since this comparison is only concerned with linear measurements it can be scaled to arbitrary dimensions or arbitrary overlapping quadrilaterals.
To summarize:
intersecting_area =
max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y))
percent_coverage = intersecting_area / (orange_area + blue_area - intersecting_area)
thanks for the nice explanation. What if the bounding box is inside another bounding box?
– prb
Nov 27 '18 at 7:50
1
@prb take this equation:max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y))and put in numbers so that all the orange triangles are bigger than the blue triangles (but less than the blue circles) and all the orange circles are less than the blue circles (but more than the blue triangles). Report your findings
– Connor
Nov 27 '18 at 16:51
Is there a way we can do it for multiple bounding boxes?
– prb
Nov 28 '18 at 9:08
@prb what did you find with the previous one? Other people could benefit from your comment here.
– Connor
Nov 28 '18 at 15:44
@prb To scale the number of rectangles increase the number of colors for each shape comparison. E.g. With a third, green rectangle you would look formax(0, min(orange.circle.x, blue.circle.x, green.circle.x) - max(....
– Connor
Nov 28 '18 at 15:58
add a comment |
Assuming that the rectangle must be parallel to x and y axis as that seems to be the situation from the previous comments and answers.
I cannot post comment yet, but I would like to point out that both previous answers seem to ignore the case when one side rectangle is totally within the side of the other rectangle. Please correct me if I am wrong.
Consider the case
a: (1,1), (4,4)
b: (2,2), (5,3)
In this case, we see that for the intersection, height must be bTop - bBottom because the vertical part of b is wholly contained in a.
We just need to add more cases as follows: (The code can be shorted if you treat top and bottom as the same thing as right and left, so that you do not need to duplicate the conditional chunk twice, but this should do.)
if aRight <= bLeft or bRight <= aLeft or aTop <= bBottom or bTop <= aBottom:
# There is no intersection in these cases
return 0
else:
# There is some intersection
if aRight >= bRight and aLeft <= bLeft:
# From x axis point of view, b is wholly contained in a
width = bRight - bLeft
elif bRight >= aRight and bLeft <= aLeft:
# From x axis point of view, a is wholly contained in b
width = aRight - aLeft
elif aRight >= bRight:
width = bRight - aLeft
else:
width = aRight - bLeft
if aTop >= bTop and aBottom <= bBottom:
# From y axis point of view, b is wholly contained in a
height = bTop - bBottom
elif bTop >= aTop and bBottom <= aBottom:
# From y axis point of view, a is wholly contained in b
height = aTop - aBottom
elif aTop >= bTop:
height = bTop - aBottom
else:
height = aTop - bBottom
return width * height
add a comment |
[ymin_a, xmin_a, ymax_a, xmax_a] = list(bbox_a)
[ymin_b, xmin_b, ymax_b, xmax_b] = list(bbox_b)
x_intersection = min(xmax_a, xmax_b) - max(xmin_a, xmin_b) + 1
y_intersection = min(ymax_a, ymax_b) - max(ymin_a, ymin_b) + 1
if x_intersection <= 0 or y_intersection <= 0:
return 0
else:
return x_intersection * y_intersection
add a comment |
@User3025064 is correct and is the simplest solution, though, exclusivity must be checked first for rectangles that do not intersect e.g., for rectangles A & B (in Visual Basic):
If A.Top =< B.Bottom or A.Bottom => B.Top or A.Right =< B.Left or A.Left => B.Right then
Exit sub 'No intersection
else
width = ABS(Min(XA2, XB2) - Max(XA1, XB1))
height = ABS(Min(YA2, YB2) - Max(YA1, YB1))
Area = width * height 'Total intersection area.
End if
add a comment |
The answer of @user3025064 is the right answer. The accepted answer inadvertently flips the inner MAX and MIN calls.
We also don't need to check first if they intersect or not if we use the presented formula, MAX(0,x) as opposed to ABS(x). If they do not intersect, MAX(0,x) returns zero which makes the intersection area 0 (i.e. disjoint).
I suggest that @Yves Daoust fixes his answer because it is the accepted one that pops up to anyone who searches for that problem. Once again, here is the right formula for intersection:
SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
The rest as usual. Union:
SU = SA + SB - SI
and ratio:
SI/SU
1
are you sure? I have updated the correct answer based on your advice, but 30 people have suggested Yves was the correct answer, so I'm hoping you can double check your assumption for me. thanks.
– Patrick Collins
Jun 12 '15 at 16:53
Try this counter example: Two rectangles, side by side that do not overlap, soXA1<XA2<XB1<XB2. The width of the intersection according to Yves is:w = Max(0, Max(XA2, XB2) - Min(XA1, XB1)) = XB2-XA1which is a big rectangle that contains the gap between the two rectangles. In the fixed formula,w = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) = Max(0, XA2-XB1) = 0because XA2<XB1 hence XA2-XB1<0.w=0means no intersection.
– Hazem
Jun 14 '15 at 5:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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
},
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%2fstackoverflow.com%2fquestions%2f9324339%2fhow-much-do-two-rectangles-overlap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
Compute the area of the intersection, which is a rectangle too:
SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
From there you compute the area of the union:
SU = SA + SB - SI
And you can consider the ratio
SI / SU
(100% in case of a perfect overlap, down to 0%).
wow. That is exactly what I was after thank you! I wasn't thinking about it correctly. The introduction of the union concept is what I was missing. Thanks!.
– Patrick Collins
Feb 19 '12 at 4:59
Thanks man! Works like a charm.
– skrat
Oct 10 '12 at 12:44
Is SA and SB the area of A and B? And for SI, the more i move my rectangle to the right bottom the higher the values get.
– clankill3r
Apr 28 '14 at 16:33
1
Give a numerical example.
– Yves Daoust
Apr 28 '14 at 19:08
7
It'd be so much better if he labeled what the variables are.
– Louis Hong
Jun 1 '15 at 0:24
|
show 2 more comments
Compute the area of the intersection, which is a rectangle too:
SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
From there you compute the area of the union:
SU = SA + SB - SI
And you can consider the ratio
SI / SU
(100% in case of a perfect overlap, down to 0%).
wow. That is exactly what I was after thank you! I wasn't thinking about it correctly. The introduction of the union concept is what I was missing. Thanks!.
– Patrick Collins
Feb 19 '12 at 4:59
Thanks man! Works like a charm.
– skrat
Oct 10 '12 at 12:44
Is SA and SB the area of A and B? And for SI, the more i move my rectangle to the right bottom the higher the values get.
– clankill3r
Apr 28 '14 at 16:33
1
Give a numerical example.
– Yves Daoust
Apr 28 '14 at 19:08
7
It'd be so much better if he labeled what the variables are.
– Louis Hong
Jun 1 '15 at 0:24
|
show 2 more comments
Compute the area of the intersection, which is a rectangle too:
SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
From there you compute the area of the union:
SU = SA + SB - SI
And you can consider the ratio
SI / SU
(100% in case of a perfect overlap, down to 0%).
Compute the area of the intersection, which is a rectangle too:
SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
From there you compute the area of the union:
SU = SA + SB - SI
And you can consider the ratio
SI / SU
(100% in case of a perfect overlap, down to 0%).
edited May 3 '18 at 6:39
Patrick Collins
2,12521828
2,12521828
answered Feb 17 '12 at 8:31
Yves DaoustYves Daoust
38.1k72759
38.1k72759
wow. That is exactly what I was after thank you! I wasn't thinking about it correctly. The introduction of the union concept is what I was missing. Thanks!.
– Patrick Collins
Feb 19 '12 at 4:59
Thanks man! Works like a charm.
– skrat
Oct 10 '12 at 12:44
Is SA and SB the area of A and B? And for SI, the more i move my rectangle to the right bottom the higher the values get.
– clankill3r
Apr 28 '14 at 16:33
1
Give a numerical example.
– Yves Daoust
Apr 28 '14 at 19:08
7
It'd be so much better if he labeled what the variables are.
– Louis Hong
Jun 1 '15 at 0:24
|
show 2 more comments
wow. That is exactly what I was after thank you! I wasn't thinking about it correctly. The introduction of the union concept is what I was missing. Thanks!.
– Patrick Collins
Feb 19 '12 at 4:59
Thanks man! Works like a charm.
– skrat
Oct 10 '12 at 12:44
Is SA and SB the area of A and B? And for SI, the more i move my rectangle to the right bottom the higher the values get.
– clankill3r
Apr 28 '14 at 16:33
1
Give a numerical example.
– Yves Daoust
Apr 28 '14 at 19:08
7
It'd be so much better if he labeled what the variables are.
– Louis Hong
Jun 1 '15 at 0:24
wow. That is exactly what I was after thank you! I wasn't thinking about it correctly. The introduction of the union concept is what I was missing. Thanks!.
– Patrick Collins
Feb 19 '12 at 4:59
wow. That is exactly what I was after thank you! I wasn't thinking about it correctly. The introduction of the union concept is what I was missing. Thanks!.
– Patrick Collins
Feb 19 '12 at 4:59
Thanks man! Works like a charm.
– skrat
Oct 10 '12 at 12:44
Thanks man! Works like a charm.
– skrat
Oct 10 '12 at 12:44
Is SA and SB the area of A and B? And for SI, the more i move my rectangle to the right bottom the higher the values get.
– clankill3r
Apr 28 '14 at 16:33
Is SA and SB the area of A and B? And for SI, the more i move my rectangle to the right bottom the higher the values get.
– clankill3r
Apr 28 '14 at 16:33
1
1
Give a numerical example.
– Yves Daoust
Apr 28 '14 at 19:08
Give a numerical example.
– Yves Daoust
Apr 28 '14 at 19:08
7
7
It'd be so much better if he labeled what the variables are.
– Louis Hong
Jun 1 '15 at 0:24
It'd be so much better if he labeled what the variables are.
– Louis Hong
Jun 1 '15 at 0:24
|
show 2 more comments
The formula for intersection will be
SI= Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
then the union will be S=SA+SB-SI
And finally, the ratio will be SI / S.
ratio should beSI/S
– warmspringwinds
Jul 7 '15 at 22:20
@warmspringwinds: I agree
– Joko
Sep 7 '15 at 14:05
14
Hmmm. This answer looks more or less identical to the highest-rated answer, which predates this one by nearly two years . . .
– imallett
Jan 8 '16 at 1:59
2
This answer does not address the case of one rectangle inside another.
– user1084113
Jun 6 '16 at 0:21
3
@PatrickCollins My point was that this answer seems plagiarized from Yves Daoust's--crude copy+paste plagiarism at that. However, while it's unclear to me what commentary you're referring to (tommy.qichang's single comment?), I've just noticed that the first formula is subtly different. If this fixes an error in Yves's answer, I suggest someone edit this answer to both acknowledge Yves's, and to explain the error it contained. Ideally, user3025064 would have edit-fixed Yves's answer instead, but next best would be at least to explain why there are two nearly identical answers now.
– imallett
Feb 12 '17 at 5:46
|
show 2 more comments
The formula for intersection will be
SI= Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
then the union will be S=SA+SB-SI
And finally, the ratio will be SI / S.
ratio should beSI/S
– warmspringwinds
Jul 7 '15 at 22:20
@warmspringwinds: I agree
– Joko
Sep 7 '15 at 14:05
14
Hmmm. This answer looks more or less identical to the highest-rated answer, which predates this one by nearly two years . . .
– imallett
Jan 8 '16 at 1:59
2
This answer does not address the case of one rectangle inside another.
– user1084113
Jun 6 '16 at 0:21
3
@PatrickCollins My point was that this answer seems plagiarized from Yves Daoust's--crude copy+paste plagiarism at that. However, while it's unclear to me what commentary you're referring to (tommy.qichang's single comment?), I've just noticed that the first formula is subtly different. If this fixes an error in Yves's answer, I suggest someone edit this answer to both acknowledge Yves's, and to explain the error it contained. Ideally, user3025064 would have edit-fixed Yves's answer instead, but next best would be at least to explain why there are two nearly identical answers now.
– imallett
Feb 12 '17 at 5:46
|
show 2 more comments
The formula for intersection will be
SI= Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
then the union will be S=SA+SB-SI
And finally, the ratio will be SI / S.
The formula for intersection will be
SI= Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
then the union will be S=SA+SB-SI
And finally, the ratio will be SI / S.
edited Sep 20 '15 at 5:16
Ganz7
119113
119113
answered Jan 19 '14 at 17:21
user3025064user3025064
22922
22922
ratio should beSI/S
– warmspringwinds
Jul 7 '15 at 22:20
@warmspringwinds: I agree
– Joko
Sep 7 '15 at 14:05
14
Hmmm. This answer looks more or less identical to the highest-rated answer, which predates this one by nearly two years . . .
– imallett
Jan 8 '16 at 1:59
2
This answer does not address the case of one rectangle inside another.
– user1084113
Jun 6 '16 at 0:21
3
@PatrickCollins My point was that this answer seems plagiarized from Yves Daoust's--crude copy+paste plagiarism at that. However, while it's unclear to me what commentary you're referring to (tommy.qichang's single comment?), I've just noticed that the first formula is subtly different. If this fixes an error in Yves's answer, I suggest someone edit this answer to both acknowledge Yves's, and to explain the error it contained. Ideally, user3025064 would have edit-fixed Yves's answer instead, but next best would be at least to explain why there are two nearly identical answers now.
– imallett
Feb 12 '17 at 5:46
|
show 2 more comments
ratio should beSI/S
– warmspringwinds
Jul 7 '15 at 22:20
@warmspringwinds: I agree
– Joko
Sep 7 '15 at 14:05
14
Hmmm. This answer looks more or less identical to the highest-rated answer, which predates this one by nearly two years . . .
– imallett
Jan 8 '16 at 1:59
2
This answer does not address the case of one rectangle inside another.
– user1084113
Jun 6 '16 at 0:21
3
@PatrickCollins My point was that this answer seems plagiarized from Yves Daoust's--crude copy+paste plagiarism at that. However, while it's unclear to me what commentary you're referring to (tommy.qichang's single comment?), I've just noticed that the first formula is subtly different. If this fixes an error in Yves's answer, I suggest someone edit this answer to both acknowledge Yves's, and to explain the error it contained. Ideally, user3025064 would have edit-fixed Yves's answer instead, but next best would be at least to explain why there are two nearly identical answers now.
– imallett
Feb 12 '17 at 5:46
ratio should be
SI/S– warmspringwinds
Jul 7 '15 at 22:20
ratio should be
SI/S– warmspringwinds
Jul 7 '15 at 22:20
@warmspringwinds: I agree
– Joko
Sep 7 '15 at 14:05
@warmspringwinds: I agree
– Joko
Sep 7 '15 at 14:05
14
14
Hmmm. This answer looks more or less identical to the highest-rated answer, which predates this one by nearly two years . . .
– imallett
Jan 8 '16 at 1:59
Hmmm. This answer looks more or less identical to the highest-rated answer, which predates this one by nearly two years . . .
– imallett
Jan 8 '16 at 1:59
2
2
This answer does not address the case of one rectangle inside another.
– user1084113
Jun 6 '16 at 0:21
This answer does not address the case of one rectangle inside another.
– user1084113
Jun 6 '16 at 0:21
3
3
@PatrickCollins My point was that this answer seems plagiarized from Yves Daoust's--crude copy+paste plagiarism at that. However, while it's unclear to me what commentary you're referring to (tommy.qichang's single comment?), I've just noticed that the first formula is subtly different. If this fixes an error in Yves's answer, I suggest someone edit this answer to both acknowledge Yves's, and to explain the error it contained. Ideally, user3025064 would have edit-fixed Yves's answer instead, but next best would be at least to explain why there are two nearly identical answers now.
– imallett
Feb 12 '17 at 5:46
@PatrickCollins My point was that this answer seems plagiarized from Yves Daoust's--crude copy+paste plagiarism at that. However, while it's unclear to me what commentary you're referring to (tommy.qichang's single comment?), I've just noticed that the first formula is subtly different. If this fixes an error in Yves's answer, I suggest someone edit this answer to both acknowledge Yves's, and to explain the error it contained. Ideally, user3025064 would have edit-fixed Yves's answer instead, but next best would be at least to explain why there are two nearly identical answers now.
– imallett
Feb 12 '17 at 5:46
|
show 2 more comments
I recently ran into this problem as well and applied Yves' answer, but somehow that led to the wrong area size, so I rewrote it.
Assuming two rectangles A and B, find out how much they overlap and if so, return the area size:
IF A.right < B.left OR A.left > B.right
OR A.bottom < B.top OR A.top > B.bottom THEN RETURN 0
width := IF A.right > B.right THEN B.right - A.left ELSE A.right - B.left
height := IF A.bottom > B.bottom THEN B.bottom - A.top ELSE A.bottom - B.top
RETURN width * height
add a comment |
I recently ran into this problem as well and applied Yves' answer, but somehow that led to the wrong area size, so I rewrote it.
Assuming two rectangles A and B, find out how much they overlap and if so, return the area size:
IF A.right < B.left OR A.left > B.right
OR A.bottom < B.top OR A.top > B.bottom THEN RETURN 0
width := IF A.right > B.right THEN B.right - A.left ELSE A.right - B.left
height := IF A.bottom > B.bottom THEN B.bottom - A.top ELSE A.bottom - B.top
RETURN width * height
add a comment |
I recently ran into this problem as well and applied Yves' answer, but somehow that led to the wrong area size, so I rewrote it.
Assuming two rectangles A and B, find out how much they overlap and if so, return the area size:
IF A.right < B.left OR A.left > B.right
OR A.bottom < B.top OR A.top > B.bottom THEN RETURN 0
width := IF A.right > B.right THEN B.right - A.left ELSE A.right - B.left
height := IF A.bottom > B.bottom THEN B.bottom - A.top ELSE A.bottom - B.top
RETURN width * height
I recently ran into this problem as well and applied Yves' answer, but somehow that led to the wrong area size, so I rewrote it.
Assuming two rectangles A and B, find out how much they overlap and if so, return the area size:
IF A.right < B.left OR A.left > B.right
OR A.bottom < B.top OR A.top > B.bottom THEN RETURN 0
width := IF A.right > B.right THEN B.right - A.left ELSE A.right - B.left
height := IF A.bottom > B.bottom THEN B.bottom - A.top ELSE A.bottom - B.top
RETURN width * height
answered Apr 16 '13 at 9:02
Ja͢ckJa͢ck
145k27212267
145k27212267
add a comment |
add a comment |
Just fixing previous answers so that the ratio is between 0 and 1 (using Python):
# (x1,y1) top-left coord, (x2,y2) bottom-right coord, (w,h) size
A = {'x1': 0, 'y1': 0, 'x2': 99, 'y2': 99, 'w': 100, 'h': 100}
B = {'x1': 0, 'y1': 0, 'x2': 49, 'y2': 49, 'w': 50, 'h': 50}
# overlap between A and B
SA = A['w']*A['h']
SB = B['w']*B['h']
SI = np.max([ 0, 1 + np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, 1 + np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ])
SU = SA + SB - SI
overlap_AB = float(SI) / float(SU)
print 'overlap between A and B: %f' % overlap_AB
# overlap between A and A
B = A
SB = B['w']*B['h']
SI = np.max([ 0, 1 + np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, 1 + np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ])
SU = SA + SB - SI
overlap_AA = float(SI) / float(SU)
print 'overlap between A and A: %f' % overlap_AA
The output will be:
overlap between A and B: 0.250000
overlap between A and A: 1.000000
1
Note. This answer uses NumPy.
– rayryeng
May 24 '17 at 6:05
@Alessio B how about a condition where one rectangle is inside other?
– prb
Nov 27 '18 at 7:57
add a comment |
Just fixing previous answers so that the ratio is between 0 and 1 (using Python):
# (x1,y1) top-left coord, (x2,y2) bottom-right coord, (w,h) size
A = {'x1': 0, 'y1': 0, 'x2': 99, 'y2': 99, 'w': 100, 'h': 100}
B = {'x1': 0, 'y1': 0, 'x2': 49, 'y2': 49, 'w': 50, 'h': 50}
# overlap between A and B
SA = A['w']*A['h']
SB = B['w']*B['h']
SI = np.max([ 0, 1 + np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, 1 + np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ])
SU = SA + SB - SI
overlap_AB = float(SI) / float(SU)
print 'overlap between A and B: %f' % overlap_AB
# overlap between A and A
B = A
SB = B['w']*B['h']
SI = np.max([ 0, 1 + np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, 1 + np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ])
SU = SA + SB - SI
overlap_AA = float(SI) / float(SU)
print 'overlap between A and A: %f' % overlap_AA
The output will be:
overlap between A and B: 0.250000
overlap between A and A: 1.000000
1
Note. This answer uses NumPy.
– rayryeng
May 24 '17 at 6:05
@Alessio B how about a condition where one rectangle is inside other?
– prb
Nov 27 '18 at 7:57
add a comment |
Just fixing previous answers so that the ratio is between 0 and 1 (using Python):
# (x1,y1) top-left coord, (x2,y2) bottom-right coord, (w,h) size
A = {'x1': 0, 'y1': 0, 'x2': 99, 'y2': 99, 'w': 100, 'h': 100}
B = {'x1': 0, 'y1': 0, 'x2': 49, 'y2': 49, 'w': 50, 'h': 50}
# overlap between A and B
SA = A['w']*A['h']
SB = B['w']*B['h']
SI = np.max([ 0, 1 + np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, 1 + np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ])
SU = SA + SB - SI
overlap_AB = float(SI) / float(SU)
print 'overlap between A and B: %f' % overlap_AB
# overlap between A and A
B = A
SB = B['w']*B['h']
SI = np.max([ 0, 1 + np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, 1 + np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ])
SU = SA + SB - SI
overlap_AA = float(SI) / float(SU)
print 'overlap between A and A: %f' % overlap_AA
The output will be:
overlap between A and B: 0.250000
overlap between A and A: 1.000000
Just fixing previous answers so that the ratio is between 0 and 1 (using Python):
# (x1,y1) top-left coord, (x2,y2) bottom-right coord, (w,h) size
A = {'x1': 0, 'y1': 0, 'x2': 99, 'y2': 99, 'w': 100, 'h': 100}
B = {'x1': 0, 'y1': 0, 'x2': 49, 'y2': 49, 'w': 50, 'h': 50}
# overlap between A and B
SA = A['w']*A['h']
SB = B['w']*B['h']
SI = np.max([ 0, 1 + np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, 1 + np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ])
SU = SA + SB - SI
overlap_AB = float(SI) / float(SU)
print 'overlap between A and B: %f' % overlap_AB
# overlap between A and A
B = A
SB = B['w']*B['h']
SI = np.max([ 0, 1 + np.min([A['x2'],B['x2']]) - np.max([A['x1'],B['x1']]) ]) * np.max([ 0, 1 + np.min([A['y2'],B['y2']]) - np.max([A['y1'],B['y1']]) ])
SU = SA + SB - SI
overlap_AA = float(SI) / float(SU)
print 'overlap between A and A: %f' % overlap_AA
The output will be:
overlap between A and B: 0.250000
overlap between A and A: 1.000000
answered Mar 24 '14 at 15:19
Alessio BAlessio B
16838
16838
1
Note. This answer uses NumPy.
– rayryeng
May 24 '17 at 6:05
@Alessio B how about a condition where one rectangle is inside other?
– prb
Nov 27 '18 at 7:57
add a comment |
1
Note. This answer uses NumPy.
– rayryeng
May 24 '17 at 6:05
@Alessio B how about a condition where one rectangle is inside other?
– prb
Nov 27 '18 at 7:57
1
1
Note. This answer uses NumPy.
– rayryeng
May 24 '17 at 6:05
Note. This answer uses NumPy.
– rayryeng
May 24 '17 at 6:05
@Alessio B how about a condition where one rectangle is inside other?
– prb
Nov 27 '18 at 7:57
@Alessio B how about a condition where one rectangle is inside other?
– prb
Nov 27 '18 at 7:57
add a comment |
While the accepted answer given is correct, I think it's worth exploring this answer in a way that will make the rationale for the answer completely obvious. This is too common an algorithm to have an incomplete (or worse, controversial) answer. Furthermore, with only a passing glance at the given formula, you may miss the beauty and extensibility of the algorithm, and the implicit decisions that are being made.
First, consider one way to define a two dimensional box is with:
- (x, y) for the top left point
- (x, y) for the bottom right point
This might look like:

I indicate the top left with a triangle and the bottom right with a circle. This is to avoid opaque syntax like x1, x2 for this example.
Two overlapping rectangles might look like this:

Notice that to find the overlap you're looking for the place where the orange and the blue collide:

Once you recognize this, it becomes obvious that overlap is the result of finding and multiplying these two darkened lines:

The length of each line is the minimum value of the circle point between the lines we wish to compare, minus the maximum value of the triangle points.

Here, I'm using the two-toned shape to show that both the orange and the blue are compared. The small letter after the shape indicates that the triangles are compared along that axis.
For example, in the top equation in this previous image you can see the orange and blue triangles are compared to look for the maximum value between the two. The attribute that is compared is the x attribute. The maximum x value between the orange and blue triangles is 210.
Another way to say the same thing is: the length of the line that can fit onto both of the lines that we are comparing is found by subtracting the closest point on the longest side of the line from the furthest point on the closest side of the line.

Finding those lines gives complete information of the overlapping areas.

Once you have this, finding the percentage of overlap is trivial:

But wait, if the orange rectangle does not overlap with the blue one then you're going to have a problem:

With this example, you get a -850 for our overlapping area, that can't be right. Even worse, if a detection doesn't overlap with either dimension (neither on the x or y axis) then you will still get a positive number because both dimensions are negative. This is why you see the Max(0, ...) * Max(0, ...) as part of the solution; it ensures that if any of the overlaps are negative you'll get a 0 back from your function.
The final formula in keeping with our symbology:

It's worth noting that using the max(0, ...) function may not be necessary. You may want to know if something overlaps along one of its dimensions rather than all of them; if you use max then you will obliterate that information. For that reason, consider how you want to deal with non-overlapping images. Normally, the max function is fine to use, but it's worth being aware what it's doing.
Finally, notice that since this comparison is only concerned with linear measurements it can be scaled to arbitrary dimensions or arbitrary overlapping quadrilaterals.
To summarize:
intersecting_area =
max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y))
percent_coverage = intersecting_area / (orange_area + blue_area - intersecting_area)
thanks for the nice explanation. What if the bounding box is inside another bounding box?
– prb
Nov 27 '18 at 7:50
1
@prb take this equation:max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y))and put in numbers so that all the orange triangles are bigger than the blue triangles (but less than the blue circles) and all the orange circles are less than the blue circles (but more than the blue triangles). Report your findings
– Connor
Nov 27 '18 at 16:51
Is there a way we can do it for multiple bounding boxes?
– prb
Nov 28 '18 at 9:08
@prb what did you find with the previous one? Other people could benefit from your comment here.
– Connor
Nov 28 '18 at 15:44
@prb To scale the number of rectangles increase the number of colors for each shape comparison. E.g. With a third, green rectangle you would look formax(0, min(orange.circle.x, blue.circle.x, green.circle.x) - max(....
– Connor
Nov 28 '18 at 15:58
add a comment |
While the accepted answer given is correct, I think it's worth exploring this answer in a way that will make the rationale for the answer completely obvious. This is too common an algorithm to have an incomplete (or worse, controversial) answer. Furthermore, with only a passing glance at the given formula, you may miss the beauty and extensibility of the algorithm, and the implicit decisions that are being made.
First, consider one way to define a two dimensional box is with:
- (x, y) for the top left point
- (x, y) for the bottom right point
This might look like:

I indicate the top left with a triangle and the bottom right with a circle. This is to avoid opaque syntax like x1, x2 for this example.
Two overlapping rectangles might look like this:

Notice that to find the overlap you're looking for the place where the orange and the blue collide:

Once you recognize this, it becomes obvious that overlap is the result of finding and multiplying these two darkened lines:

The length of each line is the minimum value of the circle point between the lines we wish to compare, minus the maximum value of the triangle points.

Here, I'm using the two-toned shape to show that both the orange and the blue are compared. The small letter after the shape indicates that the triangles are compared along that axis.
For example, in the top equation in this previous image you can see the orange and blue triangles are compared to look for the maximum value between the two. The attribute that is compared is the x attribute. The maximum x value between the orange and blue triangles is 210.
Another way to say the same thing is: the length of the line that can fit onto both of the lines that we are comparing is found by subtracting the closest point on the longest side of the line from the furthest point on the closest side of the line.

Finding those lines gives complete information of the overlapping areas.

Once you have this, finding the percentage of overlap is trivial:

But wait, if the orange rectangle does not overlap with the blue one then you're going to have a problem:

With this example, you get a -850 for our overlapping area, that can't be right. Even worse, if a detection doesn't overlap with either dimension (neither on the x or y axis) then you will still get a positive number because both dimensions are negative. This is why you see the Max(0, ...) * Max(0, ...) as part of the solution; it ensures that if any of the overlaps are negative you'll get a 0 back from your function.
The final formula in keeping with our symbology:

It's worth noting that using the max(0, ...) function may not be necessary. You may want to know if something overlaps along one of its dimensions rather than all of them; if you use max then you will obliterate that information. For that reason, consider how you want to deal with non-overlapping images. Normally, the max function is fine to use, but it's worth being aware what it's doing.
Finally, notice that since this comparison is only concerned with linear measurements it can be scaled to arbitrary dimensions or arbitrary overlapping quadrilaterals.
To summarize:
intersecting_area =
max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y))
percent_coverage = intersecting_area / (orange_area + blue_area - intersecting_area)
thanks for the nice explanation. What if the bounding box is inside another bounding box?
– prb
Nov 27 '18 at 7:50
1
@prb take this equation:max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y))and put in numbers so that all the orange triangles are bigger than the blue triangles (but less than the blue circles) and all the orange circles are less than the blue circles (but more than the blue triangles). Report your findings
– Connor
Nov 27 '18 at 16:51
Is there a way we can do it for multiple bounding boxes?
– prb
Nov 28 '18 at 9:08
@prb what did you find with the previous one? Other people could benefit from your comment here.
– Connor
Nov 28 '18 at 15:44
@prb To scale the number of rectangles increase the number of colors for each shape comparison. E.g. With a third, green rectangle you would look formax(0, min(orange.circle.x, blue.circle.x, green.circle.x) - max(....
– Connor
Nov 28 '18 at 15:58
add a comment |
While the accepted answer given is correct, I think it's worth exploring this answer in a way that will make the rationale for the answer completely obvious. This is too common an algorithm to have an incomplete (or worse, controversial) answer. Furthermore, with only a passing glance at the given formula, you may miss the beauty and extensibility of the algorithm, and the implicit decisions that are being made.
First, consider one way to define a two dimensional box is with:
- (x, y) for the top left point
- (x, y) for the bottom right point
This might look like:

I indicate the top left with a triangle and the bottom right with a circle. This is to avoid opaque syntax like x1, x2 for this example.
Two overlapping rectangles might look like this:

Notice that to find the overlap you're looking for the place where the orange and the blue collide:

Once you recognize this, it becomes obvious that overlap is the result of finding and multiplying these two darkened lines:

The length of each line is the minimum value of the circle point between the lines we wish to compare, minus the maximum value of the triangle points.

Here, I'm using the two-toned shape to show that both the orange and the blue are compared. The small letter after the shape indicates that the triangles are compared along that axis.
For example, in the top equation in this previous image you can see the orange and blue triangles are compared to look for the maximum value between the two. The attribute that is compared is the x attribute. The maximum x value between the orange and blue triangles is 210.
Another way to say the same thing is: the length of the line that can fit onto both of the lines that we are comparing is found by subtracting the closest point on the longest side of the line from the furthest point on the closest side of the line.

Finding those lines gives complete information of the overlapping areas.

Once you have this, finding the percentage of overlap is trivial:

But wait, if the orange rectangle does not overlap with the blue one then you're going to have a problem:

With this example, you get a -850 for our overlapping area, that can't be right. Even worse, if a detection doesn't overlap with either dimension (neither on the x or y axis) then you will still get a positive number because both dimensions are negative. This is why you see the Max(0, ...) * Max(0, ...) as part of the solution; it ensures that if any of the overlaps are negative you'll get a 0 back from your function.
The final formula in keeping with our symbology:

It's worth noting that using the max(0, ...) function may not be necessary. You may want to know if something overlaps along one of its dimensions rather than all of them; if you use max then you will obliterate that information. For that reason, consider how you want to deal with non-overlapping images. Normally, the max function is fine to use, but it's worth being aware what it's doing.
Finally, notice that since this comparison is only concerned with linear measurements it can be scaled to arbitrary dimensions or arbitrary overlapping quadrilaterals.
To summarize:
intersecting_area =
max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y))
percent_coverage = intersecting_area / (orange_area + blue_area - intersecting_area)
While the accepted answer given is correct, I think it's worth exploring this answer in a way that will make the rationale for the answer completely obvious. This is too common an algorithm to have an incomplete (or worse, controversial) answer. Furthermore, with only a passing glance at the given formula, you may miss the beauty and extensibility of the algorithm, and the implicit decisions that are being made.
First, consider one way to define a two dimensional box is with:
- (x, y) for the top left point
- (x, y) for the bottom right point
This might look like:

I indicate the top left with a triangle and the bottom right with a circle. This is to avoid opaque syntax like x1, x2 for this example.
Two overlapping rectangles might look like this:

Notice that to find the overlap you're looking for the place where the orange and the blue collide:

Once you recognize this, it becomes obvious that overlap is the result of finding and multiplying these two darkened lines:

The length of each line is the minimum value of the circle point between the lines we wish to compare, minus the maximum value of the triangle points.

Here, I'm using the two-toned shape to show that both the orange and the blue are compared. The small letter after the shape indicates that the triangles are compared along that axis.
For example, in the top equation in this previous image you can see the orange and blue triangles are compared to look for the maximum value between the two. The attribute that is compared is the x attribute. The maximum x value between the orange and blue triangles is 210.
Another way to say the same thing is: the length of the line that can fit onto both of the lines that we are comparing is found by subtracting the closest point on the longest side of the line from the furthest point on the closest side of the line.

Finding those lines gives complete information of the overlapping areas.

Once you have this, finding the percentage of overlap is trivial:

But wait, if the orange rectangle does not overlap with the blue one then you're going to have a problem:

With this example, you get a -850 for our overlapping area, that can't be right. Even worse, if a detection doesn't overlap with either dimension (neither on the x or y axis) then you will still get a positive number because both dimensions are negative. This is why you see the Max(0, ...) * Max(0, ...) as part of the solution; it ensures that if any of the overlaps are negative you'll get a 0 back from your function.
The final formula in keeping with our symbology:

It's worth noting that using the max(0, ...) function may not be necessary. You may want to know if something overlaps along one of its dimensions rather than all of them; if you use max then you will obliterate that information. For that reason, consider how you want to deal with non-overlapping images. Normally, the max function is fine to use, but it's worth being aware what it's doing.
Finally, notice that since this comparison is only concerned with linear measurements it can be scaled to arbitrary dimensions or arbitrary overlapping quadrilaterals.
To summarize:
intersecting_area =
max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y))
percent_coverage = intersecting_area / (orange_area + blue_area - intersecting_area)
edited Nov 27 '18 at 22:09
answered Sep 5 '18 at 23:58
ConnorConnor
175212
175212
thanks for the nice explanation. What if the bounding box is inside another bounding box?
– prb
Nov 27 '18 at 7:50
1
@prb take this equation:max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y))and put in numbers so that all the orange triangles are bigger than the blue triangles (but less than the blue circles) and all the orange circles are less than the blue circles (but more than the blue triangles). Report your findings
– Connor
Nov 27 '18 at 16:51
Is there a way we can do it for multiple bounding boxes?
– prb
Nov 28 '18 at 9:08
@prb what did you find with the previous one? Other people could benefit from your comment here.
– Connor
Nov 28 '18 at 15:44
@prb To scale the number of rectangles increase the number of colors for each shape comparison. E.g. With a third, green rectangle you would look formax(0, min(orange.circle.x, blue.circle.x, green.circle.x) - max(....
– Connor
Nov 28 '18 at 15:58
add a comment |
thanks for the nice explanation. What if the bounding box is inside another bounding box?
– prb
Nov 27 '18 at 7:50
1
@prb take this equation:max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y))and put in numbers so that all the orange triangles are bigger than the blue triangles (but less than the blue circles) and all the orange circles are less than the blue circles (but more than the blue triangles). Report your findings
– Connor
Nov 27 '18 at 16:51
Is there a way we can do it for multiple bounding boxes?
– prb
Nov 28 '18 at 9:08
@prb what did you find with the previous one? Other people could benefit from your comment here.
– Connor
Nov 28 '18 at 15:44
@prb To scale the number of rectangles increase the number of colors for each shape comparison. E.g. With a third, green rectangle you would look formax(0, min(orange.circle.x, blue.circle.x, green.circle.x) - max(....
– Connor
Nov 28 '18 at 15:58
thanks for the nice explanation. What if the bounding box is inside another bounding box?
– prb
Nov 27 '18 at 7:50
thanks for the nice explanation. What if the bounding box is inside another bounding box?
– prb
Nov 27 '18 at 7:50
1
1
@prb take this equation:
max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y)) and put in numbers so that all the orange triangles are bigger than the blue triangles (but less than the blue circles) and all the orange circles are less than the blue circles (but more than the blue triangles). Report your findings– Connor
Nov 27 '18 at 16:51
@prb take this equation:
max(0, min(orange.circle.x, blue.circle.x) - max(orange.triangle.x, blue.triangle.x)) * max(0, min(orange.circle.y, blue.circle.y) - max(orange.triangle.y, blue.triangle.y)) and put in numbers so that all the orange triangles are bigger than the blue triangles (but less than the blue circles) and all the orange circles are less than the blue circles (but more than the blue triangles). Report your findings– Connor
Nov 27 '18 at 16:51
Is there a way we can do it for multiple bounding boxes?
– prb
Nov 28 '18 at 9:08
Is there a way we can do it for multiple bounding boxes?
– prb
Nov 28 '18 at 9:08
@prb what did you find with the previous one? Other people could benefit from your comment here.
– Connor
Nov 28 '18 at 15:44
@prb what did you find with the previous one? Other people could benefit from your comment here.
– Connor
Nov 28 '18 at 15:44
@prb To scale the number of rectangles increase the number of colors for each shape comparison. E.g. With a third, green rectangle you would look for
max(0, min(orange.circle.x, blue.circle.x, green.circle.x) - max(....– Connor
Nov 28 '18 at 15:58
@prb To scale the number of rectangles increase the number of colors for each shape comparison. E.g. With a third, green rectangle you would look for
max(0, min(orange.circle.x, blue.circle.x, green.circle.x) - max(....– Connor
Nov 28 '18 at 15:58
add a comment |
Assuming that the rectangle must be parallel to x and y axis as that seems to be the situation from the previous comments and answers.
I cannot post comment yet, but I would like to point out that both previous answers seem to ignore the case when one side rectangle is totally within the side of the other rectangle. Please correct me if I am wrong.
Consider the case
a: (1,1), (4,4)
b: (2,2), (5,3)
In this case, we see that for the intersection, height must be bTop - bBottom because the vertical part of b is wholly contained in a.
We just need to add more cases as follows: (The code can be shorted if you treat top and bottom as the same thing as right and left, so that you do not need to duplicate the conditional chunk twice, but this should do.)
if aRight <= bLeft or bRight <= aLeft or aTop <= bBottom or bTop <= aBottom:
# There is no intersection in these cases
return 0
else:
# There is some intersection
if aRight >= bRight and aLeft <= bLeft:
# From x axis point of view, b is wholly contained in a
width = bRight - bLeft
elif bRight >= aRight and bLeft <= aLeft:
# From x axis point of view, a is wholly contained in b
width = aRight - aLeft
elif aRight >= bRight:
width = bRight - aLeft
else:
width = aRight - bLeft
if aTop >= bTop and aBottom <= bBottom:
# From y axis point of view, b is wholly contained in a
height = bTop - bBottom
elif bTop >= aTop and bBottom <= aBottom:
# From y axis point of view, a is wholly contained in b
height = aTop - aBottom
elif aTop >= bTop:
height = bTop - aBottom
else:
height = aTop - bBottom
return width * height
add a comment |
Assuming that the rectangle must be parallel to x and y axis as that seems to be the situation from the previous comments and answers.
I cannot post comment yet, but I would like to point out that both previous answers seem to ignore the case when one side rectangle is totally within the side of the other rectangle. Please correct me if I am wrong.
Consider the case
a: (1,1), (4,4)
b: (2,2), (5,3)
In this case, we see that for the intersection, height must be bTop - bBottom because the vertical part of b is wholly contained in a.
We just need to add more cases as follows: (The code can be shorted if you treat top and bottom as the same thing as right and left, so that you do not need to duplicate the conditional chunk twice, but this should do.)
if aRight <= bLeft or bRight <= aLeft or aTop <= bBottom or bTop <= aBottom:
# There is no intersection in these cases
return 0
else:
# There is some intersection
if aRight >= bRight and aLeft <= bLeft:
# From x axis point of view, b is wholly contained in a
width = bRight - bLeft
elif bRight >= aRight and bLeft <= aLeft:
# From x axis point of view, a is wholly contained in b
width = aRight - aLeft
elif aRight >= bRight:
width = bRight - aLeft
else:
width = aRight - bLeft
if aTop >= bTop and aBottom <= bBottom:
# From y axis point of view, b is wholly contained in a
height = bTop - bBottom
elif bTop >= aTop and bBottom <= aBottom:
# From y axis point of view, a is wholly contained in b
height = aTop - aBottom
elif aTop >= bTop:
height = bTop - aBottom
else:
height = aTop - bBottom
return width * height
add a comment |
Assuming that the rectangle must be parallel to x and y axis as that seems to be the situation from the previous comments and answers.
I cannot post comment yet, but I would like to point out that both previous answers seem to ignore the case when one side rectangle is totally within the side of the other rectangle. Please correct me if I am wrong.
Consider the case
a: (1,1), (4,4)
b: (2,2), (5,3)
In this case, we see that for the intersection, height must be bTop - bBottom because the vertical part of b is wholly contained in a.
We just need to add more cases as follows: (The code can be shorted if you treat top and bottom as the same thing as right and left, so that you do not need to duplicate the conditional chunk twice, but this should do.)
if aRight <= bLeft or bRight <= aLeft or aTop <= bBottom or bTop <= aBottom:
# There is no intersection in these cases
return 0
else:
# There is some intersection
if aRight >= bRight and aLeft <= bLeft:
# From x axis point of view, b is wholly contained in a
width = bRight - bLeft
elif bRight >= aRight and bLeft <= aLeft:
# From x axis point of view, a is wholly contained in b
width = aRight - aLeft
elif aRight >= bRight:
width = bRight - aLeft
else:
width = aRight - bLeft
if aTop >= bTop and aBottom <= bBottom:
# From y axis point of view, b is wholly contained in a
height = bTop - bBottom
elif bTop >= aTop and bBottom <= aBottom:
# From y axis point of view, a is wholly contained in b
height = aTop - aBottom
elif aTop >= bTop:
height = bTop - aBottom
else:
height = aTop - bBottom
return width * height
Assuming that the rectangle must be parallel to x and y axis as that seems to be the situation from the previous comments and answers.
I cannot post comment yet, but I would like to point out that both previous answers seem to ignore the case when one side rectangle is totally within the side of the other rectangle. Please correct me if I am wrong.
Consider the case
a: (1,1), (4,4)
b: (2,2), (5,3)
In this case, we see that for the intersection, height must be bTop - bBottom because the vertical part of b is wholly contained in a.
We just need to add more cases as follows: (The code can be shorted if you treat top and bottom as the same thing as right and left, so that you do not need to duplicate the conditional chunk twice, but this should do.)
if aRight <= bLeft or bRight <= aLeft or aTop <= bBottom or bTop <= aBottom:
# There is no intersection in these cases
return 0
else:
# There is some intersection
if aRight >= bRight and aLeft <= bLeft:
# From x axis point of view, b is wholly contained in a
width = bRight - bLeft
elif bRight >= aRight and bLeft <= aLeft:
# From x axis point of view, a is wholly contained in b
width = aRight - aLeft
elif aRight >= bRight:
width = bRight - aLeft
else:
width = aRight - bLeft
if aTop >= bTop and aBottom <= bBottom:
# From y axis point of view, b is wholly contained in a
height = bTop - bBottom
elif bTop >= aTop and bBottom <= aBottom:
# From y axis point of view, a is wholly contained in b
height = aTop - aBottom
elif aTop >= bTop:
height = bTop - aBottom
else:
height = aTop - bBottom
return width * height
edited Aug 1 '13 at 18:02
answered Aug 1 '13 at 17:46
SharShar
142128
142128
add a comment |
add a comment |
[ymin_a, xmin_a, ymax_a, xmax_a] = list(bbox_a)
[ymin_b, xmin_b, ymax_b, xmax_b] = list(bbox_b)
x_intersection = min(xmax_a, xmax_b) - max(xmin_a, xmin_b) + 1
y_intersection = min(ymax_a, ymax_b) - max(ymin_a, ymin_b) + 1
if x_intersection <= 0 or y_intersection <= 0:
return 0
else:
return x_intersection * y_intersection
add a comment |
[ymin_a, xmin_a, ymax_a, xmax_a] = list(bbox_a)
[ymin_b, xmin_b, ymax_b, xmax_b] = list(bbox_b)
x_intersection = min(xmax_a, xmax_b) - max(xmin_a, xmin_b) + 1
y_intersection = min(ymax_a, ymax_b) - max(ymin_a, ymin_b) + 1
if x_intersection <= 0 or y_intersection <= 0:
return 0
else:
return x_intersection * y_intersection
add a comment |
[ymin_a, xmin_a, ymax_a, xmax_a] = list(bbox_a)
[ymin_b, xmin_b, ymax_b, xmax_b] = list(bbox_b)
x_intersection = min(xmax_a, xmax_b) - max(xmin_a, xmin_b) + 1
y_intersection = min(ymax_a, ymax_b) - max(ymin_a, ymin_b) + 1
if x_intersection <= 0 or y_intersection <= 0:
return 0
else:
return x_intersection * y_intersection
[ymin_a, xmin_a, ymax_a, xmax_a] = list(bbox_a)
[ymin_b, xmin_b, ymax_b, xmax_b] = list(bbox_b)
x_intersection = min(xmax_a, xmax_b) - max(xmin_a, xmin_b) + 1
y_intersection = min(ymax_a, ymax_b) - max(ymin_a, ymin_b) + 1
if x_intersection <= 0 or y_intersection <= 0:
return 0
else:
return x_intersection * y_intersection
answered Jul 29 '14 at 15:13
FelixFelix
88241436
88241436
add a comment |
add a comment |
@User3025064 is correct and is the simplest solution, though, exclusivity must be checked first for rectangles that do not intersect e.g., for rectangles A & B (in Visual Basic):
If A.Top =< B.Bottom or A.Bottom => B.Top or A.Right =< B.Left or A.Left => B.Right then
Exit sub 'No intersection
else
width = ABS(Min(XA2, XB2) - Max(XA1, XB1))
height = ABS(Min(YA2, YB2) - Max(YA1, YB1))
Area = width * height 'Total intersection area.
End if
add a comment |
@User3025064 is correct and is the simplest solution, though, exclusivity must be checked first for rectangles that do not intersect e.g., for rectangles A & B (in Visual Basic):
If A.Top =< B.Bottom or A.Bottom => B.Top or A.Right =< B.Left or A.Left => B.Right then
Exit sub 'No intersection
else
width = ABS(Min(XA2, XB2) - Max(XA1, XB1))
height = ABS(Min(YA2, YB2) - Max(YA1, YB1))
Area = width * height 'Total intersection area.
End if
add a comment |
@User3025064 is correct and is the simplest solution, though, exclusivity must be checked first for rectangles that do not intersect e.g., for rectangles A & B (in Visual Basic):
If A.Top =< B.Bottom or A.Bottom => B.Top or A.Right =< B.Left or A.Left => B.Right then
Exit sub 'No intersection
else
width = ABS(Min(XA2, XB2) - Max(XA1, XB1))
height = ABS(Min(YA2, YB2) - Max(YA1, YB1))
Area = width * height 'Total intersection area.
End if
@User3025064 is correct and is the simplest solution, though, exclusivity must be checked first for rectangles that do not intersect e.g., for rectangles A & B (in Visual Basic):
If A.Top =< B.Bottom or A.Bottom => B.Top or A.Right =< B.Left or A.Left => B.Right then
Exit sub 'No intersection
else
width = ABS(Min(XA2, XB2) - Max(XA1, XB1))
height = ABS(Min(YA2, YB2) - Max(YA1, YB1))
Area = width * height 'Total intersection area.
End if
edited Dec 10 '15 at 16:38
cdmh
2,52411838
2,52411838
answered Mar 29 '14 at 20:04
user3476611user3476611
211
211
add a comment |
add a comment |
The answer of @user3025064 is the right answer. The accepted answer inadvertently flips the inner MAX and MIN calls.
We also don't need to check first if they intersect or not if we use the presented formula, MAX(0,x) as opposed to ABS(x). If they do not intersect, MAX(0,x) returns zero which makes the intersection area 0 (i.e. disjoint).
I suggest that @Yves Daoust fixes his answer because it is the accepted one that pops up to anyone who searches for that problem. Once again, here is the right formula for intersection:
SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
The rest as usual. Union:
SU = SA + SB - SI
and ratio:
SI/SU
1
are you sure? I have updated the correct answer based on your advice, but 30 people have suggested Yves was the correct answer, so I'm hoping you can double check your assumption for me. thanks.
– Patrick Collins
Jun 12 '15 at 16:53
Try this counter example: Two rectangles, side by side that do not overlap, soXA1<XA2<XB1<XB2. The width of the intersection according to Yves is:w = Max(0, Max(XA2, XB2) - Min(XA1, XB1)) = XB2-XA1which is a big rectangle that contains the gap between the two rectangles. In the fixed formula,w = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) = Max(0, XA2-XB1) = 0because XA2<XB1 hence XA2-XB1<0.w=0means no intersection.
– Hazem
Jun 14 '15 at 5:06
add a comment |
The answer of @user3025064 is the right answer. The accepted answer inadvertently flips the inner MAX and MIN calls.
We also don't need to check first if they intersect or not if we use the presented formula, MAX(0,x) as opposed to ABS(x). If they do not intersect, MAX(0,x) returns zero which makes the intersection area 0 (i.e. disjoint).
I suggest that @Yves Daoust fixes his answer because it is the accepted one that pops up to anyone who searches for that problem. Once again, here is the right formula for intersection:
SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
The rest as usual. Union:
SU = SA + SB - SI
and ratio:
SI/SU
1
are you sure? I have updated the correct answer based on your advice, but 30 people have suggested Yves was the correct answer, so I'm hoping you can double check your assumption for me. thanks.
– Patrick Collins
Jun 12 '15 at 16:53
Try this counter example: Two rectangles, side by side that do not overlap, soXA1<XA2<XB1<XB2. The width of the intersection according to Yves is:w = Max(0, Max(XA2, XB2) - Min(XA1, XB1)) = XB2-XA1which is a big rectangle that contains the gap between the two rectangles. In the fixed formula,w = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) = Max(0, XA2-XB1) = 0because XA2<XB1 hence XA2-XB1<0.w=0means no intersection.
– Hazem
Jun 14 '15 at 5:06
add a comment |
The answer of @user3025064 is the right answer. The accepted answer inadvertently flips the inner MAX and MIN calls.
We also don't need to check first if they intersect or not if we use the presented formula, MAX(0,x) as opposed to ABS(x). If they do not intersect, MAX(0,x) returns zero which makes the intersection area 0 (i.e. disjoint).
I suggest that @Yves Daoust fixes his answer because it is the accepted one that pops up to anyone who searches for that problem. Once again, here is the right formula for intersection:
SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
The rest as usual. Union:
SU = SA + SB - SI
and ratio:
SI/SU
The answer of @user3025064 is the right answer. The accepted answer inadvertently flips the inner MAX and MIN calls.
We also don't need to check first if they intersect or not if we use the presented formula, MAX(0,x) as opposed to ABS(x). If they do not intersect, MAX(0,x) returns zero which makes the intersection area 0 (i.e. disjoint).
I suggest that @Yves Daoust fixes his answer because it is the accepted one that pops up to anyone who searches for that problem. Once again, here is the right formula for intersection:
SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))
The rest as usual. Union:
SU = SA + SB - SI
and ratio:
SI/SU
answered Jun 12 '15 at 10:03
HazemHazem
16114
16114
1
are you sure? I have updated the correct answer based on your advice, but 30 people have suggested Yves was the correct answer, so I'm hoping you can double check your assumption for me. thanks.
– Patrick Collins
Jun 12 '15 at 16:53
Try this counter example: Two rectangles, side by side that do not overlap, soXA1<XA2<XB1<XB2. The width of the intersection according to Yves is:w = Max(0, Max(XA2, XB2) - Min(XA1, XB1)) = XB2-XA1which is a big rectangle that contains the gap between the two rectangles. In the fixed formula,w = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) = Max(0, XA2-XB1) = 0because XA2<XB1 hence XA2-XB1<0.w=0means no intersection.
– Hazem
Jun 14 '15 at 5:06
add a comment |
1
are you sure? I have updated the correct answer based on your advice, but 30 people have suggested Yves was the correct answer, so I'm hoping you can double check your assumption for me. thanks.
– Patrick Collins
Jun 12 '15 at 16:53
Try this counter example: Two rectangles, side by side that do not overlap, soXA1<XA2<XB1<XB2. The width of the intersection according to Yves is:w = Max(0, Max(XA2, XB2) - Min(XA1, XB1)) = XB2-XA1which is a big rectangle that contains the gap between the two rectangles. In the fixed formula,w = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) = Max(0, XA2-XB1) = 0because XA2<XB1 hence XA2-XB1<0.w=0means no intersection.
– Hazem
Jun 14 '15 at 5:06
1
1
are you sure? I have updated the correct answer based on your advice, but 30 people have suggested Yves was the correct answer, so I'm hoping you can double check your assumption for me. thanks.
– Patrick Collins
Jun 12 '15 at 16:53
are you sure? I have updated the correct answer based on your advice, but 30 people have suggested Yves was the correct answer, so I'm hoping you can double check your assumption for me. thanks.
– Patrick Collins
Jun 12 '15 at 16:53
Try this counter example: Two rectangles, side by side that do not overlap, so
XA1<XA2<XB1<XB2 . The width of the intersection according to Yves is: w = Max(0, Max(XA2, XB2) - Min(XA1, XB1)) = XB2-XA1 which is a big rectangle that contains the gap between the two rectangles. In the fixed formula, w = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) = Max(0, XA2-XB1) = 0 because XA2<XB1 hence XA2-XB1<0. w=0 means no intersection.– Hazem
Jun 14 '15 at 5:06
Try this counter example: Two rectangles, side by side that do not overlap, so
XA1<XA2<XB1<XB2 . The width of the intersection according to Yves is: w = Max(0, Max(XA2, XB2) - Min(XA1, XB1)) = XB2-XA1 which is a big rectangle that contains the gap between the two rectangles. In the fixed formula, w = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) = Max(0, XA2-XB1) = 0 because XA2<XB1 hence XA2-XB1<0. w=0 means no intersection.– Hazem
Jun 14 '15 at 5:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f9324339%2fhow-much-do-two-rectangles-overlap%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
2
What have you come up with so far?
– Gumbo
Feb 17 '12 at 7:20
you mean to say, 2 rectangles are in a box with given co-ordinates?
– sgowd
Feb 17 '12 at 7:20
@sans481 Yes I corrected the question thanks.
– Patrick Collins
Feb 17 '12 at 7:22
Did you give a try on that? If you have, what did you do?
– sgowd
Feb 17 '12 at 7:26
1
The answer from user3025064 is the right answer.
– prometeu
Sep 29 '17 at 18:18