Principle Analysis of rectangular intersection and Intersection Region

Source: Internet
Author: User

(1) design an algorithm to determine whether two rectangles overlap (that is, overlapping areas)
(2) If the two rectangles intersect, design an algorithm to find the intersecting area rectangle.

(1) The general idea of this problem is to determine whether the four vertices of a rectangle are in the area of another rectangle. This idea is the simplest, but inefficient, and has errors. Let's take a look at the error.

For example, the intersection of rectangles (overlapping areas) is divided into three types (there may be other Division). In the third case, (3), the two rectangles intersect, but there is no vertex of a rectangle inside another rectangle. Therefore, there is an error in that idea, and the intersection in this case cannot be checked.

Observe carefully and think of another idea, that is, to judge the horizontal and vertical distance of the center coordinates of the two rectangles, as long as the two values meet certain conditions, they can be intersecting.
Rectangle A wide Wa = Xa2-Xa1 high Ha = Ya2-Ya1
Wide Wb of rectangle B = Xb2-Xb1 high Hb = Yb2-Yb1
Center Coordinate of rectangle A (Xa3, Ya3) = (Xa2 + Xa1)/2, (Ya2 + Ya1)/2)
Center Coordinate of rectangle B (Xb3, Yb) = (Xb2 + Xb1)/2, (Yb2 + Yb1)/2)
Therefore, as long as the two formulas below are met, the two rectangles are at the same time. 1) | Xb3-Xa3 | <= Wa/2 + Wb/2
| Yb3-Ya3 | <= Ha/2 + Hb/2
That is:
| Xb2 + Xb1-Xa2-Xa1 | <= Xa2-Xa1 + Xb2-Xb1
| Yb2 + Yb1-Ya2-Ya1 | <= Y a2-Ya1 + Yb2-Yb1

(2) For this problem, assume that the two rectangles are intersecting, the rectangle after the intersection is set to C, the coordinate in the upper left corner of the rectangle C is (Xc1, Yc1), and the coordinate in the lower right corner is (Xc2, yc2), after observation, it is obvious that:
Xc1 = max (Xa1, Xb1)
Yc1 = max (Ya1, Yb1)
Xc2 = min (Xa2, Xb2)
Yc2 = min (Ya2, Yb2)
In this way, the intersection area of the rectangle is obtained.
In addition, it is noted that, without the assumption that the rectangles intersect, the definitions (Xc1, Yc1), (Xc2, Yc2), and Xc1, Yc1, Xc2, the value of Yc2 is derived from the preceding four formulas. In this way, the intersection of rectangles can be determined based on the values of Xc1, Yc1, Xc2, and Yc2.
Xc1, Yc1, Xc2, and Yc2 must satisfy the following two formulas at the same time, which indicates that the two rectangles intersect.
3) Xc1 <= Xc2
4) Yc1 <= Yc2
That is:
Max (Xa1, Xb1) <= min (Xa2, Xb2)
Max (Ya1, Yb1) <= min (Ya2, Yb2)

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.