Http://blog.csdn.net/yanleigis/article/details/3158223
How to calculate the area of the intersection of two intersecting Rectangles?
Is the public part of the handover area?
Class rectangle {
Point min;
Point Max;
}
Rectangle rect1, rect2;
Rectangle rect;
Locate the two values in the middle of the X coordinate.
Locate the two values in the Y coordinate.
Rect. Min. x = max (rect1.min. X, rect2.min. X); // find the largest X from the smallest x
Rect. Min. Y = max (rect1.min. Y, rect2.min. Y); // find the largest y from the smallest y
Rect. Max. x = min (rect1.max. X, rect2.max. X); // find the smallest X from the largest x
Rect. Max. Y = min (rect1.max. Y, rect2.max. Y); // find the smallest y from the largest y
If (rect. Min. x <rect. Max. X & rect. Min. Y <rect. Max. Y) // The intersection of area
S = (rect. Max. x-rect.min.x) * (rect. Max. y-rect.min.y)
Else
S = 0;
How to determine whether two rectangles are intersecting
Assume that the rectangle is expressed by a pair of vertices (Minx, miny) (Maxx,Maxy)
Then two rectangles rect1 {(minx1, miny1) (maxx1,Maxy1 )},
Rect2 {(minx2, miny2) (maxx2,Maxy2 )}
The result of the intersection must be a rectangle that forms the rect {(Minx, miny) (Maxx,
Maxy)} Point-to-point coordinates are:
Minx
=Max (minx1,
Minx2)
Miny
=Max (miny1,
Miny2)
Maxx
=Min (maxx1,
Maxx2)
Maxy
=Min (maxy1,
Maxy2)
If the two rectangles do not overlap, the calculated point-to-point coordinates must meet
Minx
>Maxx
Or
Miny
>Maxy