poj2826 An Easy Problem?!
题目描述:
题解:
这道题告诉我们POJ的数据是极强的……
计算几何。
有好几个特殊情况,都在这组数据里面。
10 6259 2664 8292 9080 1244 2972 9097 9680 0 1 1 0 1 0 2 1 0 1 2 1 1 0 1 2 0 0 10 10 0 0 9 8 0 0 10 10 0 0 8 9 0.9 3.1 4 0 0 3 2 2 0 0 0 2 0 0 -3 2 1 1 1 4 0 0 2 3 1 2 1 4 0 0 2 3 0 0 1 1 0 0 1 2
代码:
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const double eps = 1e-8; int dcmp(double x) { if(fabs(x)<=eps)return 0; return x>0?1:-1; } struct Point { double x,y; Point(){} Point(double x,double y):x(x),y(y){} Point operator + (const Point&a)const{return Point(x+a.x,y+a.y);} Point operator - (const Point&a)const{return Point(x-a.x,y-a.y);} Point operator * (const double&a)const{return Point(x*a,y*a);} double operator ^ (const Point&a)const{return x*a.y-y*a.x;} }a,b,c,d; typedef Point Vector; struct Line { Point p; Vector v; Line(){} Line(Point p,Vector v):p(p),v(v){} }s,t; int n; bool diff(Line l,Point a,Point b) { return dcmp(l.v^(a-l.p))*(l.v^(b-l.p))<=0; } Point L_L(Line a,Line b) { double t = ((b.p-a.p)^b.v)/(a.v^b.v); return a.p+a.v*t; } Point L_Y(Line a,double y) { return a.p+a.v*((y-a.p.y)/a.v.y); } void work() { scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y); if(a.y<b.y)swap(a,b); if(c.y<d.y)swap(c,d); if(a.y<c.y)swap(a,c),swap(b,d); s = Line(a,b-a),t = Line(c,d-c); if(!dcmp(a.y-b.y)||!dcmp(c.y-d.y)||!diff(s,c,d)||!diff(t,a,b)||dcmp(a.x-c.x)*dcmp((a-b)^(c-d))<=0) { puts("0.00"); }else { Point p = L_L(s,t),pp = L_Y(s,c.y); printf("%.2lf\n",fabs(((c-p)^(pp-p))/2)+eps); } } int main() { scanf("%d",&n); while(n--)work(); return 0; }