# 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 Rectangle Area 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)