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.
An example:
line: start point: (4,9)
end point: (11,2)
rectangle: left-top: (1,5)
right-bottom: (7,1)
![](https://i-blog.csdnimg.cn/blog_migrate/3a3d9fd12fde3724889a3122da809fa7.jpeg)
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.
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
题意:给定一条线段的两个端点,再给定矩形顶点的两个横坐标和两个总坐标(矩形的边与坐标轴平行),求线段和矩形是否有公共点
需要注意两点:
1、矩形是实心的(The rectangle consists of four straight lines and the area in between),所以如果线段完全在矩形内部也算相交;
2、给定的矩形坐标并不是按照矩形的端点坐标给出的,而需要自行判断。
那么所需判断的就是给定线段和矩形四条边是否相交,如果不相交,再判断线段是否在矩形内部。
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
struct point{
double x,y;
point(double a=0,double b=0){
x=a;
y=b;
}
};
struct Lineser{
point s,e;
Lineser(point a,point b){
s=a;
e=b;
}
};
double multiply(point sp,point ep,point op){
return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
}
bool intersect(Lineser u,Lineser v){
return( (max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&& //排斥实验
(max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&
(max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&
(max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&
(multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=0)&& //跨立实验
(multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=0));
}
int main(){
int t;
scanf("%d",&t);
while(t--){
point s,e,l,r;
double x1,y1,x2,y2;
cin>>s.x>>s.y>>e.x>>e.y>>l.x>>l.y>>r.x>>r.y;
if(l.x<r.x)
x1=l.x,x2=r.x;
else
x1=r.x,x2=l.x;
if(l.y<r.y)
y1=l.y,y2=r.y;
else
y1=r.y,y2=l.y;
point a(x1,y1),b(x2,y1),c(x1,y2),d(x2,y2);
if(intersect(Lineser(a,b),Lineser(s,e))||intersect(Lineser(a,c),Lineser(s,e))||intersect(Lineser(b,d),Lineser(s,e))||intersect(Lineser(c,d),Lineser(s,e)))
printf("T\n");
else if(min(s.x,e.x)>x1&&max(s.x,e.x)<x2&&min(s.y,e.y)>y1&&max(s.y,e.y)<y2)
printf("T\n");
else
printf("F\n");
}
return 0;
}
下面是判断两条线段是否相交的模板:
struct point{
double x,y;
point(double a=0,double b=0){ //这种写法很好
x=a;
y=b;
}
};
struct Lineser{
point s,e;
Lineser(point a,point b){
s=a;
e=b;
}
};
double multiply(point sp,point ep,point op){
return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
}
bool intersect(Lineser u,Lineser v){
return( (max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&& //排斥实验
(max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&
(max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&
(max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&
(multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=0)&& //跨立实验
(multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=0));
}
int main(){
point s,e;
double x1,y1,x2,y2;
point a(x1,y1),b(x2,y1);
if(intersect(Lineser(a,b),Lineser(s,e)))
......
return 0;
}