其他分享
首页 > 其他分享> > 矩形相交问题

矩形相交问题

作者:互联网

LeetCode 223. 矩形面积

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        using ll = long long;
        ll x = static_cast<ll>(min(C, G)) - static_cast<ll>(max(A, E));
        ll y = static_cast<ll>(min(D, H)) - static_cast<ll>(max(B, F));
        if(x < 0 || y < 0) x = y = 0;
        ll rec1 = abs(D-B)*abs(C-A);
        ll rec2 = abs(H-F)*abs(G-E);
        ll ans = rec1 - x*y + rec2;
        return static_cast<int>(ans);
    }
};

LeetCode 836. 矩形重叠

class Solution {
public:
    bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
        return min(rec1[2], rec2[2]) > max(rec1[0], rec2[0]) && 
            min(rec1[3], rec2[3]) > max(rec1[1], rec2[1]);
    }
};

标签:rec1,int,ll,相交,问题,cast,static,rec2,矩形
来源: https://www.cnblogs.com/littleorange/p/12527142.html