Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6318 | Accepted: 2627 |
Description
Input
Output
Sample Input
1 7 0 0 4 4 4 7 9 7 13 -1 8 -6 4 -4
Sample Output
80.00 ac代码:#include<stdio.h> #include<math.h> #include<string.h> #include<stack> #include<queue> #include<vector> #include<iostream> #include<algorithm> #define MAXN 60001 #define LL long long #define INF 0xfffffff #define mem(aa) memset(aa,0,sizeof(aa)) #define PI acos(-1) #define eps 1e-8 using namespace std; struct s { double x,y; }; s list[MAXN],p[MAXN],q[MAXN]; double a,b,c,area; int cnt,ccnt,n; void getline(s x,s y) { a=y.y-x.y;b=x.x-y.x;c=y.x*x.y-x.x*y.y; } s xlist(s x,s y) { double u=fabs(a*x.x+b*x.y+c); double v=fabs(a*y.x+b*y.y+c); s k; k.x=(x.x*v+y.x*u)/(u+v); k.y=(x.y*v+y.y*u)/(u+v); return k; } void cut() { ccnt=0; for(int i=1;i<=cnt;i++) { if(a*p[i].x+b*p[i].y+c>=0) q[++ccnt]=p[i]; else { if(a*p[i-1].x+b*p[i-1].y+c>0) q[++ccnt]=xlist(p[i],p[i-1]); if(a*p[i+1].x+b*p[i+1].y+c>0) q[++ccnt]=xlist(p[i],p[i+1]); } } for(int i=1;i<=ccnt;i++) p[i]=q[i]; p[ccnt+1]=q[1];p[0]=p[ccnt]; cnt=ccnt; } void solve() { int i; for(i=1;i<=n;i++) p[i]=list[i]; p[n+1]=p[1];p[0]=p[n]; cnt=n; for(i=1;i<=n;i++) { getline(list[i],list[i+1]); cut(); } area=0; for(i=1;i<=ccnt;i++) area+=p[i].x*p[i+1].y-p[i+1].x*p[i].y; area=fabs(area/2.0); } void guizheng() { for(int i=1;i<(n+1)/2;i++) swap(list[i],list[n-i]); } int main() { int t,i; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) scanf("%lf%lf",&list[i].x,&list[i].y); list[n+1]=list[1]; solve(); printf("%.2lf\n",area); } return 0; }