POJ1410--Intersection--点积叉积的应用

来源:互联网 发布:淘宝怎么批量删除宝贝 编辑:程序博客网 时间:2024/05/21 17:12

Description

You are to write a program that has to decide whether a given line segment intersects a given rectangle.

An example:
line: start point: (4,9)
end point: (11,2)
rectangle: left-top: (1,5)
right-bottom: (7,1)


Figure 1: Line segment does not intersect rectangle

The line is said to intersect the rectangle if the line and the rectangle have at least one point in common. The rectangle consists of four straight lines and the area in between. Although all input values are integer numbers, valid intersection points do not have to lay on the integer grid.

Input

The input consists of n test cases. The first line of the input file contains the number n. Each following line contains one test case of the format:
xstart ystart xend yend xleft ytop xright ybottom

where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xleft, ytop) the top left and (xright, ybottom) the bottom right corner of the rectangle. The eight numbers are separated by a blank. The terms top left and bottom right do not imply any ordering of coordinates.

Output

For each test case in the input file, the output file should contain a line consisting either of the letter "T" if the line segment intersects the rectangle or the letter "F" if the line segment does not intersect the rectangle.

Sample Input

14 9 11 2 1 5 7 1

Sample Output

F

/*只需要将线段与矩形4条边都判判看有木有相交就行此题有个小trick。就是当整条线段在矩形内的时候要返回T还有就是所给的数据不代表先给的就大。所以自己要比较*/#include <iostream>#include <cstdio>#include <cmath>using namespace std;#define precision 1e-6struct Point{double x,y;}point[8];double max(double a,double b){return a>b?a:b;}double min(double a,double b){return a>b?b:a;}int dblcmp(double d){if(fabs(d)<precision)return 0;return (d>0)?1:-1;}double det(double x1,double y1,double x2,double y2)//叉积运算{return x1*y2-x2*y1;}double cross(Point a,Point b,Point c){return det(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);//叉积运算判断左侧还是右侧}double dotdet(double x1,double y1,double x2,double y2)//点积运算{return x1*x2+y1*y2;}double dot(Point a,Point b,Point c)//点积运算判断是否共线{return dotdet(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);}int betweenCmp(Point a,Point b,Point c)//判断是否非规范相交{return dblcmp(dot(a,b,c));}int segcross(Point a,Point b,Point c,Point d){double s1,s2,s3,s4;int d1,d2,d3,d4;d1=dblcmp(s1=cross(a,b,c));d2=dblcmp(s2=cross(a,b,d));d3=dblcmp(s3=cross(c,d,a));d4=dblcmp(s4=cross(c,d,b));//若规范相交则求交点的代码if((d1^d2)==-2&&(d3^d4)==-2){return 1;}//判定非规范相交if(d1==0&&betweenCmp(c,a,b)<=0 ||   d2==0&&betweenCmp(d,a,b)<=0 ||   d3==0&&betweenCmp(a,c,d)<=0 ||   d4==0&&betweenCmp(b,c,d)<=0)   return 2;return 0;}int main(){int t;scanf("%d",&t);//xstart ystart xend yend double xstart,ystart,xend,yend;double x_start,y_start,x_end,y_end;while(t--){scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x_start,&y_start,&x_end,&y_end,&xstart,&ystart,&xend,¥d);point[1].x=xstart;point[1].y=ystart;point[2].x=xstart;point[2].y=yend;point[3].x=xend;point[3].y=yend;point[4].x=xend;point[4].y=ystart;point[5].x=x_start;point[5].y=y_start;point[6].x=x_end;point[6].y=y_end;bool flag=false;for(int i=1;i<=3;i++){if(segcross(point[i],point[i+1],point[5],point[6])){flag=true;break;}}if(!flag){if(segcross(point[1],point[4],point[5],point[6])){flag=true;}}if(!flag){double maxx=max(xstart,xend);double minx=min(xstart,xend);double maxy=max(ystart,yend);double miny=min(ystart,yend);if((x_start>=minx&&x_start<=maxx)&&(x_end>=minx&&x_end<=maxx)){if((y_start>=miny&&y_start<=maxy)&&(y_end>=miny&&y_end<=maxy)){flag=true;}}}if(flag){printf("T\n");}else printf("F\n");}return 0;}


 

原创粉丝点击