223. Rectangle Area

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.

做这道题感觉回到了高中。

var computeArea = function(A, B, C, D, E, F, G, H) {
    var s = (C-A) * (D-B) + (G-E) * (H-F); // areas of the two rectangle
    if(E>=C || F>=D || G<=A || H<=B)    return s; // no overlap
    return s - (Math.min(C,G) - Math.max(A,E)) * (Math.min(D,H) - Math.max(B,F)); //areas of the two rectangle - overlap
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ps:缓慢翻译中...bezier曲线部分给我翻译吐了... 路径(path)定义了一个或多个形状, 或者子路径(...
    Falme丶阅读 1,183评论 2 2
  • Find the total area covered by two rectilinear rectangles...
    a_void阅读 119评论 0 0
  • 罗胖说,未来的世界是去中心化和社群化。 罗胖了解很多,他的概念总是引导着我们的大脑前行 。 “去中心化和社群化人”...
    Dflower阅读 580评论 0 2
  • 不知道从什么时候开始,我彻底的醒悟了。我的人生,只有我对它负责。从前啊,我是一个特别自卑的姑娘。从我知道自己...
    爱你新新阅读 580评论 0 0
  • 人命自有天数。为什么有的人活得长,有的人会夭折?有的人富贵,有的人贫穷?都是累世的福报决定的。为什么中国人有句老话...
    书法与禅阅读 2,603评论 0 0