标签:void pac number ase order class close lin poi
Intersection
Input
Output
Sample Input
1 4 9 11 2 1 5 7 1
Sample Output
F
题目的意思是,给出一个实心矩形和一条直线,判断是否有交点.
看清楚了,这可是实心的矩形哦,所以,包含在矩形内的线段也是可以的.那么另一种情况,就是存在交点.那么只要判断线段与矩形四条边是否有交点就是了.
1 #include<cmath> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #define Vec point 6 using namespace std; 7 const double eps=1e-9; 8 int n; 9 struct point{ 10 double x,y; 11 void read(){scanf("%lf%lf",&x,&y);} 12 }seg1a,seg1b,seg2a,seg2b,seg3a,seg3b,seg4a,seg4b,seg0a,seg0b,ans; 13 int fabso(double x){return x<-eps?-1:x>eps;} 14 Vec operator + (point u,Vec v){ 15 Vec ret; ret.x=u.x+v.x,ret.y=u.y+v.y; 16 return ret; 17 } 18 Vec operator - (point u,point v){ 19 Vec ret; ret.x=u.x-v.x,ret.y=u.y-v.y; 20 return ret; 21 } 22 Vec operator * (Vec u,double v){ 23 Vec ret; ret.x=u.x*v,ret.y=u.y*v; 24 return ret; 25 } 26 Vec operator / (Vec u,double v){ 27 Vec ret; ret.x=u.x/v,ret.y=u.y/v; 28 return ret; 29 } 30 double cross(Vec u,Vec v){return u.x*v.y-u.y*v.x;} 31 bool dotonseg(point P,point U,point V){ 32 if ((P.x-U.x)*(P.x-V.x)>0) return 0; 33 if ((P.y-U.y)*(P.y-V.y)>0) return 0; 34 return cross(P-U,V-U)==0; 35 } 36 bool intersect(point P,point Q,point U,point V){ 37 if (fabso(cross(P-U,V-U)*cross(Q-U,V-U))<0&&fabso(cross(U-P,Q-P)*cross(V-P,Q-P))<0) return 1; 38 bool g1=dotonseg(P,U,V); 39 bool g2=dotonseg(Q,U,V); 40 bool g3=dotonseg(U,P,Q); 41 bool g4=dotonseg(V,P,Q); 42 if (g1||g2||g3||g4) return 1; 43 return 0; 44 } 45 bool side(point P,point Q,point U,point V){ 46 return cross(P-U,V-U)>0; 47 } 48 int main(){ 49 int T; 50 for (scanf("%d",&T); T; T--){ 51 seg0a.read(),seg0b.read(); 52 double a,b,c,d; scanf("%lf%lf%lf%lf",&a,&b,&c,&d); 53 if (a>c) swap(a,c); if (b<d) swap(b,d); 54 seg1a.x=a,seg1a.y=b,seg1b.x=a,seg1b.y=d; 55 seg2a.x=a,seg2a.y=d,seg2b.x=c,seg2b.y=d; 56 seg3a.x=c,seg3a.y=d,seg3b.x=c,seg3b.y=b; 57 seg4a.x=c,seg4a.y=b,seg4b.x=a,seg4b.y=b; 58 bool f1=intersect(seg0a,seg0b,seg1a,seg1b); 59 bool f2=intersect(seg0a,seg0b,seg2a,seg2b); 60 bool f3=intersect(seg0a,seg0b,seg3a,seg3b); 61 bool f4=intersect(seg0a,seg0b,seg4a,seg4b); 62 if (f1||f2||f3||f4) puts("T"); 63 else{ 64 f1=side(seg0a,seg0b,seg1a,seg1b); 65 f2=side(seg0a,seg0b,seg2a,seg2b); 66 f3=side(seg0a,seg0b,seg3a,seg3b); 67 f4=side(seg0a,seg0b,seg4a,seg4b); 68 if (f1==f2&&f2==f3&&f3==f4) puts("T"); else puts("F"); 69 } 70 } 71 return 0; 72 }
标签:void pac number ase order class close lin poi
原文地址:http://www.cnblogs.com/whc200305/p/7183865.html