# Check if two rectangles have overlapping parts

Source: Internet
Author: User

Question: You are given and axis-aligned rectangles. You had to determine if these rectangles overlap each other or not.
Rectangle 1:p1 (x, y), P2 (y)
Rectangle 2:p3 (x, y), P4 (y)

This question gives the coordinates of two rectangles and asks if the two rectangles have coincident parts. This is how you draw a diagram:

The problem seems a bit complicated, and if you find a complex set of conditional expressions, you'll have a lot of effort. There is an easy way to try to look at the problem from the opposite side.

Assume that two rectangles are somewhat (P1, P2) and dots (P3, P4). When the two cuboid has overlapping parts, there must be one corner of the box inside the other box. Look at the following figure:

Now it seems complicated to look at the problem again, should it be represented by 8 conditional expressions? Can you simplify the problem?

On the contrary, there are 8 balls of different colors in the bag, what is the probability of not taking the black ball? We calculate the probability of taking out the black ball first, and then we can reduce it by 1.

Similarly, how can two rectangles not overlap? The answer is simple: When a rectangle is around the other rectangle.

The corresponding conditional expression:

`! (P2.y < P3.Y | | P1.y > P4.y | | p2.x < p3.x | | p1.x > p4.x)`

Simplify it with de Morgan's law:

`(p2.y <= p3.y && p1.y >= p4.y && p2.x >= p3.x && p1.x <= p4.x)`

Is it more neat?

Check if two rectangles have overlapping parts

Related Keywords:

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.