首页 > 代码库 > Intersection

Intersection

Intersection

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

题目的意思是,给出一个实心矩形和一条直线,判断是否有交点.

看清楚了,这可是实心的矩形哦,所以,包含在矩形内的线段也是可以的.那么另一种情况,就是存在交点.那么只要判断线段与矩形四条边是否有交点就是了.

技术分享
 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 } 
View Code

 

Intersection