先求出两个多边行的面积和,再用半平面交求出两个多边形的并,总面积-2倍的并的面积就是答案了。。。
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 50005 #define maxm 3000005 #define eps 1e-10 #define mod 998244353 #define INF 999999999 #define lowbit(x) (x&(-x)) #define mp mark_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R #define debug(x) printf("AA x = %d BB\n", x); //#pragma comment (linker,"/STACK:102400000,102400000") typedef long long LL; //typedef int LL; using namespace std; LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();} // head struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) { } }; typedef Point Vector; struct Line { Point P; Vector v; double ang; Line() {} Line(Point P, Vector v) : P(P), v(v) { ang = atan2(v.y, v.x); } bool operator < (const Line &L) const { return ang < L.ang; } }; Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); } Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); } int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; } double Area2(Point A, Point B, Point C) { return Cross(B - A, C - A); } double PolyonArea(Point *p, int n) { double area = 0; for(int i = 1; i < n-1; i++) area += Cross(p[i] - p[0], p[i+1] - p[0]); return area / 2; } Vector Rotate(Vector A, double rad) { return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad)); } Vector Normal(Vector A) { double L = Length(A); return Vector(-A.y / L, A.x / L); } bool OnLeft(Line L, Point p) { return Cross(L.v, p - L.P) > 0; } Point GetIntersection(Line a, Line b) { Vector u = a.P - b.P; double t = Cross(b.v, u) / Cross(a.v, b.v); return a.P + a.v * t; } Point p[maxn]; Line q[maxn]; int HalfplaneInersection(Line* L, int n, Point* poly) { sort(L, L+n); int first, last; q[first = last = 0] = L[0]; for(int i = 1; i < n; i++) { while(first < last && !OnLeft(L[i], p[last - 1])) last--; while(first < last && !OnLeft(L[i], p[first])) first++; q[++last] = L[i]; if(fabs(Cross(q[last].v, q[last-1].v)) < eps) { last--; if(OnLeft(q[last], L[i].P)) q[last] = L[i]; } if(first < last) p[last-1] = GetIntersection(q[last-1], q[last]); } while(first < last && !OnLeft(q[first], p[last-1])) last--; if(last - first <= 1) return 0; p[last] = GetIntersection(q[last], q[first]); int m = 0; for(int i = first; i <= last; i++) poly[m++] = p[i]; return m; } int m1, m2; double ans; Point p1[maxn], p2[maxn], po[maxn], poly[maxn]; Line line[maxn]; void read(void) { for(int i = m1-1; i >= 0; i--) scanf("%lf%lf", &p1[i].x, &p1[i].y); scanf("%d", &m2); for(int i = m2-1; i >= 0; i--) scanf("%lf%lf", &p2[i].x, &p2[i].y); ans = fabs(PolyonArea(p1, m1)); ans += fabs(PolyonArea(p2, m2)); } void work(void) { for(int i = 0; i < m1; i++) line[i] = Line(p1[i], p1[(i+1)%m1] - p1[i]); for(int i = m1; i < m1+m2; i++) line[i] = Line(p2[i-m1], p2[(i+1-m1)%m2] - p2[i-m1]); int k = HalfplaneInersection(line, m1+m2, poly); ans -= 2.0 * fabs(PolyonArea(poly, k)); printf("%8.2f", ans); } int main(void) { while(scanf("%d", &m1), m1 != 0) { read(); work(); } printf("\n"); return 0; }