## HubertXia

never give up, keep fighting.

# [Leetcode]-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.

//求两个正方形的面积(重叠地方只算一次)，已知两个正方形的对角顶点
//关键在于分别求取所有4个横纵轴坐标的两个中间值
//注意:当两个正方形无重叠的情况

#include <stdlib.h>
#include <stdio.h>

#define min(a,b) ((a)<(b))?(a):(b)
#define max(a,b) ((a)>(b))?(a):(b)

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
//正方形1对角顶点(A,B)     (C,D)
//正方形2对角顶点(E,F)     (G,H)
//横坐标: A,C,E,G
//纵坐标: B,D,F,H

//判断无重叠的情况
if((abs(A-G)-(C-A)-(G-E)) > 0)
return (G-E)*(H-F) + (D-B)*(C-A);
if((abs(E-C)-(C-A)-(G-E)) > 0)
return (G-E)*(H-F) + (D-B)*(C-A);
if((abs(H-B)-(D-B)-(H-F)) > 0)
return (G-E)*(H-F) + (D-B)*(C-A);
if((abs(D-F)-(D-B)-(H-F)) > 0)
return (G-E)*(H-F) + (D-B)*(C-A);

//if((A == B)&&(B == C)&&(C == D)) return (G-E)*(H-F);
//if((E == F)&&(F == G)&&(G == H)) return (D-B)*(C-A);

//有重叠的情况
int m=0,n=0;
n = abs((min(G,C))-(max(A,E)));
m = abs((min(D,H))-(max(B,F)));

return (G-E)*(H-F) + (D-B)*(C-A) - m*n;

}

int main()
{
int A = -2,B = -2,C = 2,D = 2,E = -2,F = -2,G = 2,H = 2;
int area = computeArea( A, B, C, D, E, F, G, H);
printf("area is :%d\n",area);
}


#### leetcode 223: Rectangle Area

2015-06-10 03:25:56

#### leetcode -- Rectangle Area -- 看看计算公式

2015-12-03 12:46:36

#### leetcode_question_85 Largest Rectangle in Histogram

2013-09-18 14:00:28

#### Leetcode_223_Rectangle Area

2015-07-13 22:02:28

#### LeetCode-Rectangle Area 解题报告

2015-06-29 22:09:12

#### Leetcode_85题：Maximal_Rectangle题解报告（C++）

2018-05-26 21:49:43

#### LeetCode: Largest Rectangle in Histogram(直方图最大面积)

2013-05-18 13:09:49

#### [C++]LeetCode: 133 Largest Rectangle in Histogram（最大矩形面积）

2015-02-04 21:01:20

#### LeetCode84 Largest Rectangle in Histogram java题解

2015-06-27 11:21:16

#### 【LeetCode-面试算法经典-Java实现】【223-Rectangle Area（矩形区域）】

2015-08-29 06:39:48