poj 1410 Intersection
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.
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.
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 /***********************************************************
2 * OS : Win7
3 * Complier : GCC(G++)
4 * All Rights Reserved by GingerZeng.
5 **********************************************************/
6
7 #include<cstdio>
8 #include<cstring>
9 #include<algorithm>
10 #include<cmath>
11 #include<cstdlib>
12 #include<iostream>
13 using namespace std;
14 typedef long long ll;
15 const double eps=0.0000000001;
16 double inter(double ax,double ay,double bx,double by,double cx,double cy,double dx,double dy)
17 {
18 if(min(ax,bx)>max(cx,dx)||min(ay,by)>max(cy,dy)||
19 min(cx,dx)>max(ax,bx)||min(cy,dy)>max(ay,by))return false;
20 double h,i,j,k;
21 h=(bx-ax)*(cy-ay)-(by-ay)*(cx-ax);
22 i=(bx-ax)*(dy-ay)-(by-ay)*(dx-ax);
23 j=(dx-cx)*(ay-cy)-(dy-cy)*(ax-cx);
24 k=(dx-cx)*(by-cy)-(dy-cy)*(bx-cx);
25 if(h*i<=eps&&j*k<=eps)return 1.0;
26 else return 0.0;
27 }
28 int main()
29 {
30 int n;
31 //freopen("a.txt","r",stdin);
32 scanf("%d",&n);
33 while(n--)
34 {
35 double xs,ys,xe,ye,xl,yt,xr,yb;
36 scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&xs,&ys,&xe,&ye,&xl,&yt,&xr,&yb);
37 if(min(xs,xe)>=min(xl,xr)&&max(xs,xe)<=max(xl,xr)&&
38 max(ys,ye)<=max(yt,yb)&&min(ys,ye)>=min(yt,yb))printf("T\n");
39 else
40 {
41 double temp1=inter(xs,ys,xe,ye,xl,yt,xr,yt);
42 double temp2=inter(xs,ys,xe,ye,xl,yt,xl,yb);
43 double temp3=inter(xs,ys,xe,ye,xr,yb,xl,yb);
44 double temp4=inter(xs,ys,xe,ye,xr,yt,xr,yb);
45 if(temp1||temp2||temp3||temp4)printf("T\n");
46 else printf("F\n");
47 }
48
49 }
50 return 0;
51 }