听说这道题是个大大的坑题
结果wa了十多发,,,,还是没找到原因
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; double eps=1e-8; int cmp(double x) { if(fabs(x)<eps) return 0; if(x<0)return -1; return 1; } struct Point { double x,y; Point (){} Point (double _x,double _y) { x=_x,y=_y; } Point operator -(const Point &b)const { return Point (x-b.x,y-b.y); } double operator *(const Point &b)const { return x*b.x+y*b.y; } double operator ^(const Point &b)const { return x*b.y-y*b.x; } }; struct Line { Point s,e; Line (){} Line (Point _s,Point _e) { s=_s; e=_e; } }; double xmult(Point p0,Point p1,Point p2) { return cmp((p2-p1)^(p0-p1)); } bool seg_seg(Line l1,Line l2)//相交return true { return xmult(l1.s,l2.s,l2.e)*xmult(l1.e,l2.s,l2.e)<=0&& xmult(l2.s,l1.s,l1.e)*xmult(l2.e,l1.s,l1.e)<=0; } Line line[5]; int main () { int n; double x1,x2,x3,x4,y1,y2,y3,y4; scanf("%d",&n); while(n--) { scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); if(x3>x4)swap(x3,x4); if(y3<y4)swap(y3,y4); line[0]=Line(Point(x1,y1),Point(x2,y2)); if(x1>x3&&x1<x4&&x2<x4&&x2>x3&&y1>y4&&y1<y3&&y2>y4&&y2<y3) { printf("T\n"); continue; } else { line[1]=Line(Point(x3,y4),Point(x3,y3)); line[2]=Line(Point(x3,y4),Point(x4,y4)); line[3]=Line(Point(x4,y3),Point(x4,y4)); line[4]=Line(Point(x4,y3),Point(x3,y3)); for(int i=1;i<=4;i++) { if(seg_seg(line[0],line[i])) { printf("T\n");//相交了 break; } if(i==4) printf("F\n");//没相交 } } } return 0; }
已经发现的 坑点:
1:相交的意思是:与这个矩形整体相交,即分为:(1)与四条边中的几条相交,(2)或者在矩形内部不与四条边相交;
2:给出的矩形的四条边不一定是左上边的和右下边的,需要进行比较。(太坑)