标签:小白 inter using code static 顶点 eps printf put
Problem Description
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 #define maxn 510 8 const double eps=1E-8; 9 int sig(double d){ 10 return(d>eps)-(d<-eps); 11 } 12 struct Point{ 13 double x,y; Point(){} 14 Point(double x,double y):x(x),y(y){} 15 bool operator==(const Point&p)const{ 16 return sig(x-p.x)==0&&sig(y-p.y)==0; 17 } 18 }; 19 double cross(Point o,Point a,Point b){ //叉积 20 return(a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y); 21 } 22 double area(Point* ps,int n){ 23 ps[n]=ps[0]; 24 double res=0; 25 for(int i=0;i<n;i++){ 26 res+=ps[i].x*ps[i+1].y-ps[i].y*ps[i+1].x; 27 } 28 return res/2.0; 29 } 30 int lineCross(Point a,Point b,Point c,Point d,Point&p){ 31 double s1,s2; 32 s1=cross(a,b,c); 33 s2=cross(a,b,d); 34 if(sig(s1)==0&&sig(s2)==0) return 2; 35 if(sig(s2-s1)==0) return 0; 36 p.x=(c.x*s2-d.x*s1)/(s2-s1); 37 p.y=(c.y*s2-d.y*s1)/(s2-s1); 38 return 1; 39 } 40 //多边形切割 41 //用直线ab切割多边形p,切割后的在向量(a,b)的左侧,并原地保存切割结果 42 //如果退化为一个点,也会返回去,此时n为1 43 void polygon_cut(Point*p,int&n,Point a,Point b){ 44 static Point pp[maxn]; 45 int m=0;p[n]=p[0]; 46 for(int i=0;i<n;i++){ 47 if(sig(cross(a,b,p[i]))>0) pp[m++]=p[i]; 48 if(sig(cross(a,b,p[i]))!=sig(cross(a,b,p[i+1]))) 49 lineCross(a,b,p[i],p[i+1],pp[m++]); 50 } 51 n=0; 52 for(int i=0;i<m;i++) 53 if(!i||!(pp[i]==pp[i-1])) 54 p[n++]=pp[i]; 55 while(n>1&&p[n-1]==p[0])n--; 56 } 57 //---------------华丽的分隔线-----------------// 58 //返回三角形oab和三角形ocd的有向交面积,o是原点// 59 double intersectArea(Point a,Point b,Point c,Point d){ 60 Point o(0,0); 61 int s1=sig(cross(o,a,b)); 62 int s2=sig(cross(o,c,d)); 63 if(s1==0||s2==0)return 0.0;//退化,面积为0 64 if(s1==-1) swap(a,b); 65 if(s2==-1) swap(c,d); 66 Point p[10]={o,a,b}; 67 int n=3; 68 polygon_cut(p,n,o,c); 69 polygon_cut(p,n,c,d); 70 polygon_cut(p,n,d,o); 71 double res=fabs(area(p,n)); 72 if(s1*s2==-1) res=-res;return res; 73 } 74 //求两多边形的交面积 75 double intersectArea(Point*ps1,int n1,Point*ps2,int n2){ 76 if(area(ps1,n1)<0) reverse(ps1,ps1+n1); 77 if(area(ps2,n2)<0) reverse(ps2,ps2+n2); 78 ps1[n1]=ps1[0]; 79 ps2[n2]=ps2[0]; 80 double res=0; 81 for(int i=0;i<n1;i++){ 82 for(int j=0;j<n2;j++){ 83 res+=intersectArea(ps1[i],ps1[i+1],ps2[j],ps2[j+1]); 84 } 85 } 86 return res;//assumeresispositive! 87 } 88 Point ps1[maxn],ps2[maxn]; 89 int n1,n2; 90 int main(){ 91 while(scanf("%d%d",&n1,&n2)!=EOF){ 92 for(int i=0;i<n1;i++) 93 scanf("%lf%lf",&ps1[i].x,&ps1[i].y); 94 for(int i=0;i<n2;i++) 95 scanf("%lf%lf",&ps2[i].x,&ps2[i].y); 96 double ans=intersectArea(ps1,n1,ps2,n2); 97 ans=fabs(area(ps1,n1))+fabs(area(ps2,n2))-ans;//容斥 98 printf("%.2f\n",ans); 99 } 100 return 0; 101 }
标签:小白 inter using code static 顶点 eps printf put
原文地址:http://www.cnblogs.com/agenthtb/p/7465853.html