前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode 223. Rectangle Area

Leetcode 223. Rectangle Area

作者头像
triplebee
发布2018-01-12 14:39:27
5950
发布2018-01-12 14:39:27
举报

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;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档