Leetcode 223. Rectangle Area

2018-01-12 14:39:27 浏览数 (2)

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Assume that the total area is never beyond the maximum possible value of int.

求像个矩形覆盖的面积之和。

其实搞计算机视觉的时候会经常遇到这个问题,算IOU的时候就是这样。

用A的面积 B的面积-相交的面积。

计算相交面积之前判断一下是否相交即可。

代码语言:javascript复制
class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int areaA = (C - A) * (D - B);
        int areaB = (G - E) * (H - F);
        if(C < E || A > G || B > H || D < F) return areaA   areaB;
        int inter = (min(D, H) - max(B, F)) * (min(G, C) - max(A, E));
        return areaA   areaB - inter;
    }
};

0 人点赞