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
1 4 9 11 2 1 5 7 1
Sample Output
F
如果线段在矩形内部或者与矩形有公共点,则为T,否则为F
需要分别判定严格相交、非严格相交、重合判定以及判断是否在内部
#include <cstdio>
#include <cstring>
#include <cmath>
#include<iostream>
#include <algorithm>
using namespace std ;
#define eps 1e-8
struct Point {
double x,y;
Point() {}
Point(double _x,double _y) {
x=_x;
y=_y;
}
};
double cross_product(Point a,Point b) { //求点a和点b的叉积
return a.x*b.y-a.y*b.x;
}
struct Line {
Point a,b;
Line() {}
Line(Point _a,Point _b) {
a=_a;
b=_b;
}
}p[100005];
bool judge(Line a,Line b) {//严格相交,如果相交则返回true
if(cross_product(Point(b.a.x-a.a.x,b.a.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y))*
cross_product(Point(b.b.x-a.a.x,b.b.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y))<=-eps
&&cross_product(Point(a.a.x-b.a.x,a.a.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y))*
cross_product(Point(a.b.x-b.a.x,a.b.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y))<=-eps)
return true;
else
return false;
}
bool judge3(Line a,Line b){//判断是否重合,如果重合则返回true
if(fabs(cross_product(Point(a.b.x-a.a.x,a.b.y-a.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.x)))<=eps&&
fabs(cross_product(Point(b.a.x-a.a.x,b.a.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y)))<=eps){
if((b.a.x<=a.b.x&&a.b.x<=b.b.x&&b.a.y<=a.b.y&&a.b.y<=b.b.y)||(a.a.x<=b.b.x&&b.b.x<=a.b.x&&a.a.y<=b.b.y&&b.b.y<=a.b.y))
return true;
else
return false;
}
else{
return false;
}
}
bool judge2(Line a,Line b){//非严格相交,可以T字形,如果相交则返回true,非严格相交与严格相交互不包含
double ans1=cross_product(Point(b.a.x-a.a.x,b.a.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y))*
cross_product(Point(b.b.x-a.a.x,b.b.y-a.a.y),Point(a.b.x-a.a.x,a.b.y-a.a.y));
double ans2=cross_product(Point(a.a.x-b.a.x,a.a.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y))*
cross_product(Point(a.b.x-b.a.x,a.b.y-b.a.y),Point(b.b.x-b.a.x,b.b.y-b.a.y));
if(fabs(ans1)<=eps&&ans2<-eps||fabs(ans2)<=eps&&ans1<-eps){
return true;
}
return false;
}
int main(){
int test;
scanf("%d",&test);
while(test--){
double a,b,c,d;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
Point segment1=Point(a,b);
Point segment2=Point(c,d);
Line sge=Line(segment1,segment2);
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
double temp1=min(a,c);
double temp2=max(a,c);
double temp3=min(b,d);
double temp4=max(b,d);
a=temp1,b=temp4,c=temp2,d=temp3;
Point rec_a=Point(a,b);
Point rec_b=Point(a,d);
Point rec_c=Point(c,d);
Point rec_d=Point(c,b);
Line la=Line(rec_a,rec_b);
Line lb=Line(rec_b,rec_c);
Line lc=Line(rec_c,rec_d);
Line ld=Line(rec_d,rec_a);
if(segment1.x<=c&&segment1.x>=a&&segment1.y>=d&&segment1.y<=b||
segment2.x<=c&&segment2.x>=a&&segment2.y>=d&&segment2.y<=b){
printf("T\n");
continue;
}
if(judge(sge,la)||judge2(sge,la)||judge2(sge,la)){
printf("T\n");
continue;
}
if(judge(sge,lb)||judge2(sge,lb)||judge2(sge,lb)){
printf("T\n");
continue;
}
if(judge(sge,lc)||judge2(sge,lc)||judge2(sge,lc)){
printf("T\n");
continue;
}
if(judge(sge,ld)||judge2(sge,ld)||judge2(sge,ld)){
printf("T\n");
continue;
}
printf("F\n");
}
}
//5
//2 2 2 5
//0 1 2 0