circle - rectangle intersection - algorithm

I have problem with circle-rectangle intersection.Though A number
of discussion i found about it ,i could not get my answer.My problem is -I have a rectangle lower portion(100-200,0-50) of my view/window(320 X 480).And a ball is moving here and there.And
sometimes it collides with the rectangle and bounce back.And my problem is how will i know in which axis circle collide with the
rectangle, in x-axis or y axis,means in which line(x=100 or x=200 or y==50) circle intersect with rectangle.
*Ball comes from outside of rectangle.

To see if it hits one of the lines full on is easy: just check for a collision between the bounding box of the circle and each of the lines in turn.
The problem is if it hits a corner... then you have to intersect the circle with each line. This can be done, but requires a bit more mathematics. It will also bounce off at an unusual angle.

Say the ball's center is moving along a time trajectory that can be described as x = a t + b and y = c t + d -- any linear, uniform-speed motion can be described this way. Since you say that it's initially (say at t=0) outside the rectangle, we know that at that time x < 100 or x > 200, or y < 0 or y > 50 (one of the conditions of x, and one of the conditions of y, can both be true, but at least one must be -- if they were all false we'd be inside the rectangle).
So check "at what time and exactly where will that point intersect each of the four lines that make up the rectangle"; i.e., solve for t when x = 100 (which gives t = (100 - b) / a, and therefore y = c (100 - b) / a + d), x = 200, y = 0, y = 50. Discard the solutions where t < 0 (those were things that happened in the past), as well as ones where the other variable falls outside of the rectangle's boundaries (for example, for the t = 100 case I just mentioned, you can ignore the apparent solution if (100 - b) / a < 0, or c (100 - b) / a + v < 0, or c (100 - b) / a + v > 50). If none of the four is left, this means the ball (with a radius of 0...) will not hit the rectangle along its current trajectory (it may if and when it bounces and thus changes trajectory, but those will be separate computations). If one or more are left, the one with the minimum value of t is the one you want. Once you know where and when the center would hit the rectangle, taking account of the radius can be done separately, but won't change the issue of which rectangle side the ball hits.
The cases where the ball "glances" (hits the rectangle just because it does have a radius greater than zero) are harder, but one approach is, if the normal computation shows the ball "not hitting", repeat it after shifting the ball (by the amount of its radius) to both side of the trajectory-line it's following -- this will tell you if the ball IS in fact going to hit, and, if so, which side (assuming hits on corners can be counted as hits on one of the sides converging on that corner;-).

How about:
Let centre of circle have coordinates cx, cy, radius cr.
if cx > 100 - cr and cx <= 100 and cy <= 50
collision with left upright
else if cy >= 50 and cy < 50 + cr and cx > 100 and cx < 200
collision with top
else if cx < 200 + cr and cx >= 200 and cy <= 50
collision with right upright
else if ( cx - 100 ) ** 2 + ( cy - 50 ) ** 2 < cr ** 2
collision with top left corner
else if ( cx - 200 ) ** 2 + ( cy - 50 ) ** 2 < cr ** 2
collision with top right corner
else
no collision
Corner collisions will need special treatment to work out how bounce will work based on exact point of contact and direction of travel. This also leaves a large part of the screen where collisions will not be detected (inside the rectangle), which I'm sure you could add to the above algorithm.
Doing a quick search seems to indicate that this problem has been asked before...

decrease rectange by size of radius on each side and move circle as point.

Related

How do I iterate over the outer left border of a circle?

I need to draw a circle with a specific range. In my case I need to start from the top and then go down to the bottom of the circle, going over the left outer border of the circle. I need to know the length of each line and the X and Y coordinates. I basically see a circle as a bunch of lines stacked on top of each other where as it goes further down the horizontal line length grows and grows until it reaches the middle point, then it goes all the way back and shrinks and shrinks. Then you have a circle. I need to iterate over each of those lines, knowing their X and Y coordinates from the left side so that I can then do line_to_the_left(x, y, length) to draw the circle.
How would an algorithm taking a range look like that does this? I know that one thing I would need is Pi.
You can make loop over integer Y values. For center coordinates cx, cy and radius R:
for y = - R ... R:
hw = (int) sqrt(R^2 - y^2) //halfwidth
line(cy + y, cx - hw, cy + y, cx + hw) //left and right ends of line

Collision Detection between a line and a circle in python(tkinter)

I writing a python program in which a circle bounces off of user drawn lines. There are multiple circles that bounce off the wall. For each one, the shortest distance from the center of the circle and the ball should be calculated. I would prefer if this code was very efficient because my current algorithm lags the computer a lot. If point a is the starting point ,and point b is the end point, and point c is the center, and r is the radius, how would I calculate the shortest distance between the ball? This algorithm should also work if the X coordinate of the ball is out of range of x coordinates in segment AB.
Please post python code
Any help would be appreciated!
Here's what I have so far:
lineList is a list with 4 values that contains beginning and end coordinates of the user drawn lines
center is the center of the ball
global lineList, numobjects
if not(0 in lineList):
beginCoord = [lineList[0],lineList[1]]
endCoord = [lineList[2]-500,lineList[3]-500]
center = [xCoordinate[i],yCoordinate[i]+15]
distance1 = math.sqrt((lineList[1] - center[1])**2 + (lineList[0] - center[0])**2)
slope1 = math.tan((lineList[1] - lineList[3]) / (lineList[0] - lineList[2]))
try:
slope2 = math.tan((center[1] - beginCoord[1])/(center[0]-beginCoord[0]))
angle1 = slope2 + slope1
circleDistance = distance1 * math.sin(angle1)
except:
#If the circle is directly above beginCoord
circleDistance = center[1] - lineList[1]
global numbounces
if circleDistance < 2 and circleDistance > -2:
print(circleDistance)
b = False
b2=False
if xCoordinate[i] < 0:
xCoordinate[i] += 1
speed1[i] *= -1
b=True
elif xCoordinate[i] > 0:
xCoordinate[i] -= 1
speed1[i] *= -1
b=True
if yCoordinate[i] < 0:
yCoordinate[i] += 1
speed2[i] *= -1
b2=True
elif yCoordinate[i] > 0:
yCoordinate[i] -= 1
speed2[i] *= -1
b2=True
if b and b2:
#Only delete the line if the ball reversed directions
numbounces += 1
#Add a ball after 5 bounces
if numbounces % 5 == 0 and numbounces != 0:
numobjects = 1
getData(numobjects)
canvas.delete("line")
lineList = [0,0,0,0]
To be correct we are not speaking not about lines, but rather segments.
I would suggest the following idea:
Since the ball is moving in some direction, the only points that might collide with something lie on a 180° arc - the part that is moving forward. Meaning at some point of time when you check for collision you have to check whether any of those points collided with something. The more points you check, the better the precision of the collision in time, but worse the complexity.
Checking the collision: you check whether any of the points is in between the extremes of the segment. You can do this by first checking the coordinates (example is given looking at your drawn line, meaning A.x < B.x and A.y > B.y) if (A.x <= point.x <= B.x && A.y >= point.y >= B.y if the condition satisfies, you check whether the 3 points form a line. Since you have already the coordinates of A and B you can deduce the equation of the line and check whether the point satisfies it.
In short: you check if the point satisfies the equation of the line and is inside the rectangle defined by the 2 points.
How to get the points you have to check: assuming 2k+1 is the number of points you want to check at some time, C is your center r the radius and V the vector of motion. Then the number of points from the left side of the direction vector and from the right side will be equal and be k (+1 point at the intersection of the circle and the motion vector). Then 90° / k is one angular division. Since you know the motion vector, you can calculate the angle between it and the horizontal line (let it be angle). You keep adding to go left and decrementing to go right from the motion vector the value of 90° / k exactly k times (let us denote this value by i) and calculate the position of the point by point.x = C.x + sin(i) * r and point.y = C.y + cos(i) * r.
Sry, I don't know python.
The shortest distance from a circle to a line is the shortest distance from its center to that line, minus the radius of the circle. If the distance from the center to the line is less than the radius, the line passes through the circle.
Finding the distance from a point to a line is documented many places, including here.
Sorry for not posting Python code, but it is pretty basic.

Area between two lines inside the square [-1,+1] x [-1,+1]

I'm working on a project in Matlab and need to find the area between two lines inside of the square [-1,+1]x[-1,+1] intersecting in a point (xIntersection,yIntersection). So the idea is to subtract the two lines and integrate between [-1, xIntersection] and [xIntersection, +1], sum the results and if it's negative, change its sign.
For details on how I find the intersection of the two lines check this link.
I'm using Matlab's function integral(), here a snippet of my code:
xIntersection = ((x_1 * y_2 - y_1 * x_2) * (x_3 - x_4) - (x_1 - x_2) * (x_3 * y_4 - y_3 * x_4) ) / ((x_1 - x_2) * (y_3 - y_4) - (y_1 - y_2) * (x_3 - x_4));
d = #(x) g(x) - f(x);
result = integral(d, -1, xIntersection) - int( d, xIntersection, 1)
if(result < 0),
result = result * -1;
end
Note that I defined previously in the code g(x) and f(x) but haven't reported it in the snippet.
The problem is that I soon realized that the lines could intersect either inside or outside of the square, furthermore they could intersect the square on any of its sides and the number of possible combinations grows up very quickly.
I.e.:
These are just 4 cases, but considering that f(+1), f(-1), g(+1), g(-1) could be inside the interval [-1,+1], above it or under it and that the intersection could be inside or outside of the square the total number is 3*3*3*3*2 = 162.
Obviously in each case the explicit function to integrate in order to get the area between the two lines is different, but I can't possibly think of writing a switch case for each one.
Any ideas?
I think my answer to your previous question still applies, for the most part.
If you want to compute the area of the region bounded by the smaller angle of the two lines and the boundaries of the square, then you can forget about the intersection, and forget about all the different cases.
You can just use the fact that
the area of the square S is equal to 4
the value of this integral
A = quadgk(#(x) ...
abs( max(min(line1(x),+1),-1) - max(min(line2(x),+1),-1) ), -1, +1);
gives you the area between the lines (sometimes the large angle, sometimes the small angle)
the value of of min(A, S-A) is the correct answer (always the small angle).
Assuming “between the lines” means “inside the smaller angle formed by the lines”:
With the lines l and h, S := [-1,+1]x[-1,+1] and B as the border of S.
Transform l to the form l_1 + t*l_2 with l_1 and l_2 beeing vectors. Do the same for h.
If the intersection is not inside S, find the 4 intersections of l and h with B. Sort them so you get a convex quadrilateral. Calculate its area.
Else:
Find the intersection point p and find the intersection angle α between l_2 and h_2. and then check:
If α in [90°,180°] or α in [270°,360°], swap l and h.
If α > 180°, set l_2 = −l_2
Set l_1 := h_1 := p. Do once for positive t and negative t
(forwards and backwards along l_2 and h_2 from p):
Find intersections s_l and s_h of l and h with B.
If on same border of B: compute area of triangle s_l, s_h, p
If on adjacent borders of B: find the corner c of B in between the hit borders and once again sort the four points s_l, s_h, p and c so you get an convex quadrilateral and calculate it’s area.
If on opposite borders, find the matching side of B (you can do this by looking at the direction of s_l-p). Using its two corner points c_1 and c_2, you now have 5 points that form a polygon. Sort them so the polygon is convex and calculate its area.
This is still quite a few cases, but I don’t think there’s a way around that. You can simplify this somewhat by also using the polygon formula for the triangle and the quadrilateral.
How to sort the points so you get a convex polygon/quadrilateral: select any one of them as p_1 and then sort the rest of the points according to angle to p_1.
If you define an intersection function and a polygon_area that takes a list of points, sorts them and returns the area, this algorithm should be rather simple to implement.
edit: Image to help explain the comment:
Hey guys thanks for your answers, I also thought of an empirical method to find the area between the lines and wanted to share it for the sake of discussion and completeness.
If you take a large number of random points within the square [-1,+1]x[-1,+1] you can measure the area as the fraction of the points which fall in the area between the two lines.
Here's a little snippet and two images to show the different accuracy of empirical result obtained with different number of points.
minX = -1;
maxX = +1;
errors = 0;
size = 10000;
for j=1:size,
%random point in [-1,+1]
T(j,:) = minX + (maxX - minX).*rand(2,1);
%equation of the two lines is used to compute the y-value
y1 = ( ( B(2) - A(2) ) / ( B(1) - A(1) ) ) * (T(j,1) - A(1)) + A(2);
y2 = (- W(1) / W(2)) * T(j,1) -tresh / W(2);
if(T(j,2) < y1),
%point is under line one
Y1 = -1;
else
%point is above line one
Y1 = +1;
end
if(T(j,2) < y2),
%point is under line two
Y2 = -1;
else
%point is above line two
Y2 = +1;
end
if(Y1 * Y2 < 0),
errors = errors + 1;
scatter(T(j,1),T(j,2),'fill','r')
else
scatter(T(j,1),T(j,2),'fill','g')
end
end
area = (errors / size) / 4;
And here are two images, it sure takes longer than the solution posted by #Rody but as you can see you can make it accurate.
Number of points = 2000
Number of points = 10000

Counting values in a Matrix within Boundaries

I've tried on my own using a "between the equations of a line" approach, but I need to do the following:
I have a matrix, n by n, which store 2D histogram counts. I need to be able to specify points in order, and have the program count everything between these points.
For now at least, I would be most content with a simple rectangle (however, the rectangle can be rotated any number of degrees).
From my Paint.exe'd picture of the histogram, you can see I'd like to be able to count within the blue boxes. Counting the horizontal (top right) rectangle is not a problem (specify the boundaries in a For loop as start/end bins of the matrix).
I'm stuck on how to define the boundaries in code to count within the other (leftmost) blue box. I'm using Igor Pro (from WaveMetrics) to do this, so for the most part, this is non-specific to a language.
Basically this is for analyzing areas of interest in these graphs. There are tools to analyze images which come with "within a polygon/freeform" type things, but they cannot accurately get the counts from this matrix (they analyze based on image colors, not counts). Also, I cannot filter based on "is there more than X in this bin?" as the same rectangle must be applied to a baseline "noise" matrix.
Ideas? I'm really stuck on getting a core concept of how this would work..
EDIT: My attempt, which does not appear to work properly, specifically came up empty when I put in a "box", similar to the right blue box above. I can't necessarily varify the skewed rectangle either (as we have no real way of counting it anyways..)
// Find polygon boundaries
s1 = (y2-y1)/(x2-x1)
o1 = s1==inf || s1==-inf ? 0 : y2 - (s1*x2)
s2 = (y3-y2)/(x3-x2)
o2 = s2==inf || s2==-inf ? 0 : y3 - (s2*x3)
s3 = (y4-y3)/(x4-x3)
o3 = s3==inf || s3==-inf ? 0 : y4 - (s3*x4)
s4 = (y1-y4)/(x1-x4)
o4 = s4==inf || s4==-inf ? 0 : y1 - (s4*x1)
// Get highest/lowest points (used in For loop)
maxX = max(max(max(x1, x2), x3), x4)
maxY = max(max(max(y1, y2), y3), y4)
minX = min(min(min(x1, x2), x3), x4)
minY = min(min(min(y1, y2), y3), y4)
For (i=minX; i<=maxX; i+=1) // Iterate over each X bin
For (j=minY; j<=maxY; j+=1) // Iterate over each Y bin
// | BETWEEN LINE 1 AND LINE 3? | | BETWEEN LINE 2 AND LINE 4? |
If ( ( ((s1*i + o1) > j && j > (s3*i + o3)) || ((s1*i + o1) < j && j < (s3*i +o3)) ) && ( ((s2*i + o2) > j && j > (s4*i + o4)) || ((s2*i + o2) < j && j < (s4*i +o4)) ) )
totalCount += matrixRef[i][j] // Add the count of this bin to the total count
EndIf
EndFor // End Y iteration
EndFor // End X iteration
Igor has a tool that is way more powerful than your current solution
Go to the windows menu
Select Help Windows
Select "XOP Index.ihf"
Search for "Select Points for Mask"
It will explain how to define polygons of any shape in a graph and obtain a mask for the points inside. Then you can run whatever code you want on them.
You can also make/change the polygons in code.
You can count the values in the diagonal rectangle the same way as in the horizontal rectangle, you will just have a more complex loop to determine where the boundaries are. You can do this by looping over a horizontal rectangle that includes the entire diagonal rectangle and only counting the values if they fall inside of the diagonal one.
If you know the points that make up the corners of the diagonal rectangle then you can get the equations for each boundary (slope-intercept equation). From there you see what side of each boundary the points are on. If they are on the sides that correspond to the inside of the rectangle you include them, if they are not then you don't.
You are going to have to find the X and Y points of the borders for each point that you are working on. If you are only checking against the border on the bottom left you would have something like:
for(each point in the matrix){
if(point.x > border X value # height Y && point.y > border Y value # column X)
include this point
else
don't include it
}
If you have the equations for the boundaries and you know what point you are analyzing you can use the known X value (from the point) to get the Y value of the boundary (how high it is in that column that you are looking at) and the know Y value (again, from the point you are analyzing) to get the X value of the boundary (how far in the boundary is at the height of the point).
For the full list of conditions you should have the point be:
bottom-left border: point.x > border.x && point.y > border.y
bottom-right border: point.x < border.x && point.y > border.y
top-left border: point.x > border.x && point.y < border.y
top-right border: point.x < border.x && point.y < border.y

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?
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%).
While the accepted answer 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.
We're going to build our way up to making these formulas intuitive:
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)
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 two circle points, minus the maximum value of the two triangle points.
Here, I'm using a two-toned triangle (and circle) to show that the orange and the blue points are compared with each other. The small letter 'y' after the two-toned triangle indicates that the triangles are compared along the y axis, the small 'x' means they are compared along the x axis.
For example, to find the length of the darkened blue line you can see the 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 triangles is 210.
Another way to say the same thing is:
The length of the new line that fits onto both the orange and blue lines is found by subtracting the furthest point on the closest side of the line from the closest point on the furthest side of the line.
Finding those lines gives complete information about 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 bounding boxes. 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)
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
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
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
Here is a working Function in C#:
public double calculateOverlapPercentage(Rectangle A, Rectangle B)
{
double result = 0.0;
//trivial cases
if (!A.IntersectsWith(B)) return 0.0;
if (A.X == B.X && A.Y == B.Y && A.Width == B.Width && A.Height == B.Height) return 100.0;
//# overlap between A and B
double SA = A.Width * A.Height;
double SB = B.Width * B.Height;
double SI = Math.Max(0, Math.Min(A.Right, B.Right) - Math.Max(A.Left, B.Left)) *
Math.Max(0, Math.Min(A.Bottom, B.Bottom) - Math.Max(A.Top, B.Top));
double SU = SA + SB - SI;
result = SI / SU; //ratio
result *= 100.0; //percentage
return result;
}
[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
#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
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

Resources