47-- rectangles overlap brush title

82. rectangles overlap

Topic Link

https://leetcode-cn.com/problems/rectangle-overlap/

Title Description

Rectangular a list  [x1, y1, x2, y2] representation, which  (x1, y1) is the lower left corner coordinates, (x2, y2) the coordinates of the upper right corner.

If the intersecting area is positive, called two rectangles overlap. To be clear, the two rectangular sides or only at the corners of the contact does not constitute overlap.

Given two rectangles, it determines whether they overlap and returns the result.

Example 1:

Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3] 
Output: true

Example 2:

Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1] 
Output: false 

prompt:

  1. Two rectangular  rec1 and  rec2 are given in the form of a list containing four integers.
  2. All coordinates are in a rectangle  -10^9 and  10^9 between.
  3. x Default axis pointing to the right, y the axis point on the default.
  4. You can consider only rectangular case is being put.

Topic analysis

There are four conditions are not two rectangles overlap:

1. The second rectangular x1> = x2 first rectangle

2. The second rectangular y1> = first rectangle y2

3. The second rectangular y2> = y1 of the first rectangle

4. The second rectangular x2> = x1 first rectangle

/**
 * @param {number[]} rec1
 * @param {number[]} rec2
 * @return {boolean}
 */
var isRectangleOverlap = function(rec1, rec2) {
    return !(rec2[0] >= rec1[2]
    || rec2[1] >= rec1[3]
    || rec2[3] <= rec1[1]
    || rec2[2] <= rec1[0]
    );
};

  

Guess you like

Origin www.cnblogs.com/liu-xin1995/p/12520584.html