#include<cstdio> #include<algorithm> #include<cmath> using namespace std; #define EPS 0.00000001 struct Point{ double x,y; }a[2],b[2]; typedef Point Vector; Vector operator * (const double &a,const Vector &v){ return (Vector){a*v.x,a*v.y}; } Vector operator - (const Point &a,const Point &b){ return (Vector){a.x-b.x,a.y-b.y}; } Vector operator + (const Vector &a,const Vector &b){ return (Vector){a.x+b.x,a.y+b.y}; } double Cross(const Vector &a,const Vector &b){ return a.x*b.y-a.y*b.x; } Point GetIntersection(Point P,Vector v,Point Q,Vector w){ return P+(Cross(w,P-Q)/Cross(v,w))*v; } bool inLine(Point p,Point a,Point b){ if(a.x>b.x){ swap(a,b); } if(p.x-a.x>-EPS && b.x-p.x>-EPS){ return 1; } return 0; } int main(){ while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a[0].x,&a[0].y,&a[1].x,&a[1].y,&b[0].x,&b[0].y,&b[1].x,&b[1].y)!=EOF){ if(fabs(a[0].y-a[1].y)<EPS || fabs(b[0].y-b[1].y)<EPS){ puts("0.00"); continue; } if(fabs(Cross(a[0]-a[1],b[0]-b[1]))<EPS){ puts("0.00"); continue; } if(a[0].y<a[1].y){ swap(a[0],a[1]); } if(b[0].y<b[1].y){ swap(b[0],b[1]); } Point p=GetIntersection(a[0],a[1]-a[0],b[0],b[1]-b[0]); if((!inLine(p,a[0],a[1])) || (!inLine(p,b[0],b[1]))){ puts("0.00"); continue; } if(a[0].y<b[0].y){ swap(a[0],b[0]); swap(a[1],b[1]); } if(fabs(a[0].y-b[0].y)<EPS){ printf("%.2f\n",fabs(a[0].x-b[0].x)*0.5*fabs(p.y-a[0].y)); continue; } if((!fabs(a[0].x-a[1].x)<EPS) && (!fabs(b[0].x-b[1].x)<EPS)){ double ka=(a[0].y-a[1].y)/(a[0].x-a[1].x); double kb=(b[0].y-b[1].y)/(b[0].x-b[1].x); if((ka>EPS && kb>EPS) || (ka<EPS && kb<EPS)){ if(fabs(ka)-fabs(kb)>EPS){ if((a[0].x-b[0].x<EPS && p.x-b[0].x>-EPS) || (a[0].x-b[0].x>-EPS && p.x-b[0].x<EPS)){ puts("0.00"); continue; } } } } double rr=(b[0].y-p.y)/(a[0].y-p.y); Point bc=p+rr*(a[0]-p); printf("%.2f\n",0.5*(b[0].y-p.y)*fabs(bc.x-b[0].x)); } return 0; }
时间: 2025-01-08 03:12:16