The principle analysis of the intersection of rectangles and the intersection of intersecting regions _ other synthesis

Source: Internet
Author: User
(1) Design an algorithm to determine whether two rectangles intersect (that is, overlapping regions)
(2) If two rectangles intersect, design an algorithm to find the intersecting region rectangle

(1) For this problem, the general idea is to determine whether the four vertices of a rectangle are in the region of another rectangle. This idea is the simplest, but inefficient, and there are errors, errors in where, the following analysis.

As shown above, the intersection of the rectangles (area overlap) is divided into three species (possibly with other partitions), for the third case, as in the figure (3), two rectangles intersect, but there is no vertex of a rectangle inside the other rectangle. So there is a mistake in that way, and the intersection of this situation is not checked out.

Looking closely at the diagram, think of another idea, that is, to judge the horizontal and vertical distances of the center coordinates of the two rectangles, as long as the two values meet certain conditions to intersect.
Wide Wa = xa2-xa1 High Ha = ya2-ya1 of Rectangle A
The wide Wb of rectangular B = xb2-xb1 high Hb = Yb2-yb1
Center coordinate of rectangle A (XA3,YA3) = ((XA2+XA1)/2, (YA2+YA1)/2)
Central coordinates of rectangular B (xb3,yb3) = ((XB2+XB1)/2, (YB2+YB1)/2)
So as long as the following two formulas are satisfied, you can show that the two rectangles intersect. 1) | Xb3-xa3 | <= WA/2 + WB/2
2) | Yb3-ya3 | <= HA/2 + HB/2
That
| Xb2+xb1-xa2-xa1 | <= Xa2-xa1 + xb2-xb1
| yb2+yb1-ya2-ya1 | <=y a2-ya1 + yb2-yb1

(2) For this problem, suppose that two rectangles intersect, the rectangle after the intersection is C, and the upper left corner of the rectangle C is (XC1,YC1), and the lower right corner coordinate is (XC2,YC2), and it is clearly possible to get the following view:
XC1 = max (XA1,XB1)
YC1 = max (YA1,YB1)
Xc2 = min (xa2,xb2)
Yc2 = min (ya2,yb2)
This will find the intersection of the rectangular region.
In addition, note that the definition (XC1,YC1), (XC2,YC2), and XC1,YC1,XC2,YC2 values are derived from the above four formulas, without assuming that the rectangles intersect. In this way, you can determine the intersection of rectangles based on the value of XC1,YC1,XC2,YC2.
Xc1,yc1,xc2,yc2 as long as the following two formulas are satisfied, you can show that two rectangles intersect.
3) Xc1 <= XC2
4) Yc1 <= YC2
That
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.