//----fishman@Willian #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; const double esp = 1e-8; int sgn(double x) { if (fabs(x) < esp)return 0; return x < 0 ? -1 : 1; } int dcmp(double x, double y) { if (sgn(x - y) == 0)return 0; return x < y ? -1 : 1; } struct Point { double x, y; Point() {} Point(double x, double y) :x(x),y(y) {} Point operator + (Point B) { return Point(x + B.x, y + B.y); } Point operator - (Point B) { return Point(x - B.x, y - B.y); } Point operator * (double k){return Point(x*k,y*k);} Point operator / (double k){return Point(x/k,y/k);} bool operator==(Point B) { return sgn(x - B.x) == 0 && sgn(y - B.y) == 0; } bool operator< (Point B) { return sgn(x - B.x) < 0 || (sgn(x - B.x) == 0 && sgn(y - B.y) < 0); } void input(){scanf("%lf%lf",&x,&y);} }; typedef Point Vector; //定义向量 double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;} //点积 double Len(Vector A){return sqrt(Dot(A,A));} //向量的长度 double Len2(Vector A){return Dot(A,A);} //向量长度的平方 //A与B的夹角 double Angle(Vector A,Vector B){return acos(Dot(A,B)/Len(A)/Len(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 fabs(Cross(B-A,C-A));} double Area(Point A,Point B,Point C){return 0.5*fabs(Cross(B-A,C-A));} //距离 double Dis(Point A,Point B){return hypot(A.x-B.x,A.y-B.y);} Point Cross_Point(Point a,Point b,Point c,Point d){//line ab cd double s1=Cross(b-a,c-a); double s2=Cross(b-a,d-a); return Point(c.x*s2-d.x*s1,c.y*s2-d.y*s1)/(s2-s1); } bool Cross_segement(Point a,Point b,Point c,Point d){//line ab,cd double s1=Cross(b-a,c-a),s2=Cross(b-a,d-a); double t1=Cross(d-c,a-c),t2=Cross(d-c,b-c); return sgn(s1)*sgn(s2)<=0&&sgn(t2)*sgn(t1)<=0;//1相交,0不相交 } bool ins(Point x,Point a,Point b,Point c){//x是否在三角形abc内,1是,0否 return !(Area(a,b,c)-Area(a,b,x)-Area(a,c,x)-Area(b,c,x)); } int main() { int t; cin>>t; while(t--){ Point a1,b1,a2,b2; a1.input();b1.input(); a2.input();b2.input(); int flag=1; if(Cross_segement(a1,b1,a2,b2)==0){printf("0.00\n");continue;} Point o=Cross_Point(a1,b1,a2,b2); Point s,t; int cnt=0; if(a1.y>o.y){s=a1;++cnt;} if(b1.y>o.y){s=b1;++cnt;} if(a2.y>o.y){t=a2;++cnt;} if(b2.y>o.y){t=b2;++cnt;} if(cnt!=2){printf("0.00\n");continue;} if(ins(s,Point(t.x,o.y),o,t)==1||ins(t,Point(s.x,o.y),o,s)==1){printf("0.00\n");continue;} Point q,tmp; double ans=0; if(s.y<t.y){ q=Cross_Point(s,Point(s.x+1,s.y),a2,b2); ans=Area(o,s,q); } else{ q=Cross_Point(t,Point(t.x+1,t.y),a1,b1); ans=Area(o,t,q); } printf("%.2f\n",ans); } return 0; } |
Double click to view unformatted code.