How do I obtain the area of the intersection of two transpose Rectangles?

Source: Internet
Author: User

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.