# LeetCode Rectangle Area

## LeetCode Rectangle Area

### 代码

#### 自己的

#define min(A, B) (A > B ? B : A)

bool pointInRectangle(int px, int py, int ax, int ay, int bx, int by) {
return ax <= px && px <= bx && ay <= py && py <= by;
}

int computeAreaSub(int A, int B, int C, int D, int E, int F, int G, int H) {
int area1 = (D - B) * (C - A), area2 = (H - F) * (G - E);
if (pointInRectangle(A, B, E, F, G, H)      // left down
&& pointInRectangle(A, D, E, F, G, H)      // left up
&& pointInRectangle(C, D, E, F, G, H)      // right up
&& pointInRectangle(C, B, E, F, G, H)) {   // right down
return area2;
}
if (pointInRectangle(A, B, E, F, G, H)      // left down
&& pointInRectangle(A, D, E, F, G, H)      // left up
&& !pointInRectangle(C, D, E, F, G, H)      // right up
&& !pointInRectangle(C, B, E, F, G, H)) {   // right down
return area1 + area2 - (D - B) * (G - A);
}
if (!pointInRectangle(A, B, E, F, G, H)      // left down
&& !pointInRectangle(A, D, E, F, G, H)      // left up
&& pointInRectangle(C, D, E, F, G, H)      // right up
&& pointInRectangle(C, B, E, F, G, H)) {   // right down
return area1 + area2 - (D - B) * (C - E);
}
if (!pointInRectangle(A, B, E, F, G, H)      // left down
&& pointInRectangle(A, D, E, F, G, H)      // left up
&& pointInRectangle(C, D, E, F, G, H)      // right up
&& !pointInRectangle(C, B, E, F, G, H)) {   // right down
return area1 + area2 - (D - F) * (C - A);
}
if (pointInRectangle(A, B, E, F, G, H)      // left down
&& !pointInRectangle(A, D, E, F, G, H)      // left up
&& !pointInRectangle(C, D, E, F, G, H)      // right up
&& pointInRectangle(C, B, E, F, G, H)) {   // right down
return area1 + area2 - (H - B) * (C - A);
}
if (pointInRectangle(A, B, E, F, G, H)      // left down
&& !pointInRectangle(A, D, E, F, G, H)      // left up
&& !pointInRectangle(C, D, E, F, G, H)      // right up
&& !pointInRectangle(C, B, E, F, G, H)) {   // right down
return area1 + area2 - (G - A) * (H - B);
}
if (!pointInRectangle(A, B, E, F, G, H)      // left down
&& !pointInRectangle(A, D, E, F, G, H)      // left up
&& pointInRectangle(C, D, E, F, G, H)      // right up
&& !pointInRectangle(C, B, E, F, G, H)) {   // right down
return area1 + area2 - (C - E) * (D - F);
}
if (!pointInRectangle(A, B, E, F, G, H)      // left down
&& pointInRectangle(A, D, E, F, G, H)      // left up
&& !pointInRectangle(C, D, E, F, G, H)      // right up
&& !pointInRectangle(C, B, E, F, G, H)) {   // right down
return area1 + area2 - (D - F) * (G - A);
}
if (!pointInRectangle(A, B, E, F, G, H)      // left down
&& !pointInRectangle(A, D, E, F, G, H)      // left up
&& !pointInRectangle(C, D, E, F, G, H)      // right up
&& pointInRectangle(C, B, E, F, G, H)) {   // right down
return area1 + area2 - (H - B) * (C - E);
}
return area1 + area2;
}

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int temp1 = computeAreaSub(A, B, C, D, E, F, G, H);
int temp2 = computeAreaSub(E, F, G, H, A, B, C, D);
return min(computeAreaSub(A, B, C, D, E, F, G, H), computeAreaSub(E, F, G, H, A, B, C, D));
}

#### Discuss的

#define min(A, B) (A > B ? B : A)
#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) {
int a = max(A, E), b = max(B, F), c = min(C, G), d = min(D, H);
return (C - A) * (D - B) + (G - E) * (H - F) - ((a < c && b < d) ? (c - a) * (d - b) : 0);
}

#### leetcode 223: Rectangle Area

2015-06-10 03:25:56

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

2015-12-03 12:46:36

#### 850. Rectangle Area II

2018-06-10 13:35:48

#### ［LeetCode］Rectangle Area

2015-07-19 12:29:05

#### 【Leetcode】Rectangle Area

2016-06-06 20:38:01

#### [LeetCode]223. Rectangle Area

2016-10-05 23:53:38

#### LeetCode(223)_Rectangle Area

2016-10-28 16:35:52

#### Rectangle Area -- leetcode

2015-09-10 17:02:58

#### LeetCode No.223 Rectangle Area

2016-11-11 19:46:18

#### leetcode Rectangle Area

2015-06-08 09:10:59