POJ1410 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.
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 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <iostream> 5 6 using namespace std; 7 const double eps = 1e-18; 8 int sgn(double x){if (fabs(x)<eps) return 0; return (x>0)?1:-1;} 9 struct Point{ 10 double x,y; 11 Point(int _x = 0,int _y = 0):x(_x),y(_y){} 12 bool operator == (Point b)const{ 13 return sgn(x - b.x) == 0 && sgn(y - b.y) == 0; 14 } 15 Point operator - (const Point &b)const{ 16 return Point(x - b.x,y - b.y); 17 } 18 double operator ^(const Point &b)const{ 19 return x*b.y - y*b.x; 20 } 21 double operator *(const Point &b)const{ 22 return x*b.x + y*b.y; 23 } 24 Point operator +(const Point &b)const{ 25 return Point(x+b.x,y+b.y); 26 } 27 Point operator *(const double &k)const{ 28 return Point(x*k,y*k); 29 } 30 Point operator /(const double &k)const{ 31 return Point(x/k,y/k); 32 } 33 }; 34 35 struct Line{ 36 Point s,e; 37 int segcrossseg(Line v){ 38 int d1 = sgn((e - s)^(v.s - s)); 39 int d2 = sgn((e - s)^(v.e - s)); 40 int d3 = sgn((v.e - v.s)^(s - v.s)); 41 int d4 = sgn((v.e - v.s)^(e - v.s)); 42 if( (d1^d2)== - 2 && (d3^d4)== - 2 ) 43 return 2; 44 return (d1==0 && sgn((v.s - s)*(v.s - e))<=0) || 45 (d2==0 && sgn((v.e - s)*(v.e - e))<=0) || 46 (d3==0 && sgn((s - v.s)*(s - v.e))<=0) || 47 (d4==0 && sgn((e - v.s)*(e - v.e))<=0); 48 } 49 }; 50 51 int n; 52 Line a,b,c,d,line; 53 double x1,Y1,x2,y2,x3,y3,x4,y4; 54 int main(){ 55 scanf("%d",&n); 56 while(n--){ 57 scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&Y1,&x2,&y2,&x3,&y3,&x4,&y4); 58 line.s = Point{x1,Y1}; line.e = Point{x2,y2}; 59 a.s = Point{x3,y3}; a.e = Point{x3,y4}; 60 b.s = Point{x3,y4}; b.e = Point{x4,y4}; 61 c.s = Point{x4,y4}; c.e = Point{x4,y3}; 62 d.s = Point{x4,y3}; d.e = Point{x3,y3}; 63 if (line.segcrossseg(a) || line.segcrossseg(b) 64 || line.segcrossseg(c) || line.segcrossseg(d) 65 || (sgn(fabs(x1-x3)+fabs(x1-x4)-fabs(x3-x4))==0 66 && sgn(fabs(Y1-y3)+fabs(Y1-y4)-fabs(y3-y4))==0)) puts("T"); 67 else puts("F"); 68 } 69 }