「POJ1410」Intersection
Description
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
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
Sample Input
1 2 |
1 4 9 11 2 1 5 7 1 |
Sample Output
1 |
F |
题解
这题233333坑死人233333
请看discuss
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,x1,x2,y1,y2; struct point{int x,y;}p[5]; struct line{point a,b;}l[5],a; point sub(point a,point b) {point t;t.x=a.x-b.x;t.y=a.y-b.y;return t;} int cmul(point a,point b) {return a.x*b.y-a.y*b.x;} double dmul(int x1,int y1,int x2,int y2,int x,int y) {return (x1-x)*(x2-x)+(y1-y)*(y2-y);} int online(int x1,int y1,int x2,int y2,int x,int y) { if(dmul(x1,y1,x2,y2,x,y)<=0)return 1; return 0; } int turn(point a,point b,point c) {return cmul(sub(c,a),sub(b,a));} bool cross(line a,line b) { if(turn(a.a,a.b,b.a)*turn(a.a,a.b,b.b)>0)return 0; if(turn(b.a,b.b,a.a)*turn(b.a,b.b,a.b)>0)return 0; if(turn(a.a,a.b,b.a)*turn(a.a,a.b,b.b)==0) { if(online(a.a.x,a.a.y,a.b.x,a.b.y,b.a.x,b.a.y))return 1; if(online(a.a.x,a.a.y,a.b.x,a.b.y,b.b.x,b.b.y))return 1; return 0; } if(turn(b.a,b.b,a.a)*turn(b.a,b.b,a.b)==0) { if(online(b.a.x,b.a.y,b.b.x,b.b.y,a.a.x,a.a.y))return 1; if(online(b.a.x,b.a.y,b.b.x,b.b.y,a.b.x,a.b.y))return 1; return 0; } return 1; } bool jud() { for(int i=1;i<=4;i++)if(cross(l[i],a))return 1; return 0; } bool inre(point a) { if(a.x>x2||a.x<x1)return 0; if(a.y>y2||a.y<y1)return 0; return 1; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d%d%d",&a.a.x,&a.a.y,&a.b.x,&a.b.y); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(x1>x2)swap(x1,x2);if(y1>y2)swap(y1,y2); p[1].x=x1;p[1].y=y1; p[2].x=x2;p[2].y=y1; p[3].x=x1;p[3].y=y2; p[4].x=x2;p[4].y=y2; l[1].a=p[1];l[1].b=p[2]; l[2].a=p[1];l[2].b=p[3]; l[3].a=p[2];l[3].b=p[4]; l[4].a=p[3];l[4].b=p[4]; if(inre(a.a)||inre(a.b)||jud())printf("T\n"); else printf("F\n"); } return 0; } |