836. Rectangle Overlap [LeetCode]

836. Rectangle Overlap

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner.

Two rectangles overlap if the area of their intersection is positive.  To be clear, two rectangles that only touch at the corner or edges do not overlap.

Given two rectangles, return whether they overlap.

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

判断给定的两个矩形是否有重叠,如果有重叠,则返回true.

假如重叠的话,

左边界值:max(rec1[0], rec2[0]),右边界值:min(rec1[2], rec2[2]),

下边缘值:max(rec1[1], rec2[1]),上边缘值:min(rec1[3], rec2[3])

重叠的条件为:左边界  < 右边界  &&  下边缘 < 上边缘.

代码如下:

/**************************************************************************
 * 
 * 836. [Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap/)
 * 
 * An axis-aligned rectangle is represented as a list [x1, y1, x2, y2], 
 * where (x1, y1) is the coordinate of its bottom-left corner, 
 * and (x2, y2) is the coordinate of its top-right corner. 
 * Its top and bottom edges are parallel to the X-axis, and its left and 
 * right edges are parallel to the Y-axis. Two rectangles overlap 
 * if the area of their intersection is positive. To be clear, 
 * two rectangles that only touch at the corner or edges do not overlap. 
 * Given two axis-aligned rectangles rec1 and rec2, return true if they overlap, 
 * otherwise return false.
 * 
 * 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
 * 
 **************************************************************************/

 #define MAX(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a > _b ? _a : _b; })

 #define MIN(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a < _b ? _a : _b; })


bool isRectangleOverlap(int* rec1, int rec1Size, int* rec2, int rec2Size){
    if (NULL == rec1 || NULL == rec2 || rec1Size != 4 || rec2Size != 4) return false;
    
    int rec1_x1 = rec1[0];
    int rec1_y1 = rec1[1];
    int rec1_x2 = rec1[2];
    int rec1_y2 = rec1[3];
    
    int rec2_x1 = rec2[0];
    int rec2_y1 = rec2[1];
    int rec2_x2 = rec2[2];
    int rec2_y2 = rec2[3];
    
    int leftBoundary   = MAX(rec1_x1, rec2_x1);
    int ringhtBoundary = MIN(rec1_x2, rec2_x2);

    int bottomBoundary = MAX(rec1_y1, rec2_y1);
    int topBoundary    = MIN(rec1_y2, rec2_y2);
    
    return leftBoundary < ringhtBoundary && bottomBoundary < topBoundary;
}

简化后代码:


bool isRectangleOverlap(int* rec1, int rec1Size, int* rec2, int rec2Size){
    if (NULL == rec1 || NULL == rec2 || rec1Size != 4 || rec2Size != 4) return false;
    
    return MAX(rec1[0], rec2[0]) < MIN(rec1[2], rec2[2]) && \
           MAX(rec1[1], rec2[1]) < MIN(rec1[3], rec2[3]);
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luuyiran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值