题意:给你线段和矩形,问是否相交,或矩形包含线段;
注意:线段相交时,<(0,0),(1,0)> 和<(3,0),(4,0)> 不相交。。。。郁闷死了。。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; const double INF = 1e20; const double EPS = 1e-6; bool zero(double t){return -EPS<t&&t<EPS;} struct cvector{ double x,y; cvector(double a,double b){x=a,y=b;} cvector(){} }; cvector operator+(cvector a,cvector b){ return cvector(a.x+b.x,a.y+b.y); } cvector operator-(cvector a,cvector b){ return cvector(b.x-a.x,b.y-a.y); } cvector operator*(double a,cvector b){ return cvector(a*b.x,a*b.y); } double operator*(cvector a,cvector b){ return a.x*b.x+a.y*b.y; } double operator^(cvector a,cvector b){ return a.x*b.y-b.x*a.y; } double length(double t){return t>0?t:-t;} double length(cvector t){return sqrt(t*t);} struct cpoint{ double x,y; cpoint(double a,double b){x=a,y=b;} cpoint(){} }; cvector operator-(cpoint a,cpoint b){ return cvector(b.x-a.x,b.y-a.y); } double dist(cpoint a,cpoint b){ return length(b-a); } struct cline{ cpoint a,b; }; bool intersect(cline a,cline b){ if(zero((a.a-a.b)^(b.a-b.b))) return false; return ((a.a-b.a)^(b.b-b.a))*((a.b-b.a)^(b.b-b.a))<EPS&& ((b.a-a.a)^(a.b-a.a))*((b.b-a.a)^(a.b-a.a))<EPS; } cpoint st,en; cline b[4],tmp; bool in(cpoint t) { for(int i=0;i<4;i++) if(((t-b[i].a)^(b[i].b-b[i].a))>EPS) { return false; } return true; } int main() { freopen("in.txt","r",stdin); int cas; scanf("%d",&cas); while(cas--) { scanf("%lf%lf%lf%lf",&tmp.a.x,&tmp.a.y,&tmp.b.x,&tmp.b.y); scanf("%lf%lf%lf%lf",&st.x,&st.y,&en.x,&en.y); if(st.x>en.x) swap(st.x,en.x); if(st.y<en.y) swap(st.y,en.y); b[0].a.x=st.x,b[0].a.y=en.y;b[0].b=en; b[1].a=en;b[1].b.x=en.x,b[1].b.y=st.y; b[2].a=b[1].b;b[2].b=st; b[3].a=st;b[3].b = b[0].a; if(in(tmp.a)||in(tmp.b)) { printf("T\n"); continue; } bool f =false; for(int i=0;i<4;i++) if(intersect(tmp,b[i])) { f = true;break; } if(f) printf("T\n"); else printf("F\n"); } return 0; }