刚开始自己写别提WA多少遍了;
后来看到标达真的被惊讶到了;
代码可以这么美;
#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));
}
#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);
}
原文地址:http://blog.csdn.net/u012925008/article/details/46480641