计算几何——Intersection(线段与矩形相交)

题目链接http://poj.org/problem?id=1410

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)

计算几何——Intersection(线段与矩形相交)_第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

题意翻译

您要编写一个程序,该程序必须确定给定的线段是否与给定矩形相交。‎



计算几何——Intersection(线段与矩形相交)_第2张图片


‎例如:‎
‎行:起点:(4,9)终点:(11,2)矩形:(1,5)右下:(7,1)图1:线段不‎
‎相交‎

‎矩形 如果直线和矩形至少有一个共同点,则认为线与矩形相交。矩形由四条直线和中间的区域组成。尽管所有输入值都是整数值,但有效的交点不必位于整数网格上。‎

‎输入‎

‎输入由 n 个测试用例组成。输入文件的第一行包含数字 n。以下每行包含一个格式的测试用例: ‎
‎xstart ystart xend yend xright y‎

‎下底 (xstart, ystart) 是行的起始点和 (xend, yend) 和 (x 左, ytop) 左上和 (xright,y底部)矩形的右下角。这八个数字用空白分隔。左上和右下角的术语并不意味着坐标的任何排序。‎

‎输出‎

‎对于输入文件中的每个测试用例,如果线段与矩形相交,则输出文件应包含一条包含字母"T"的行,如果线段与矩形不相交,则输出文件应包含一条包含字母"T"的行;如果线段与矩形不相交,则输出文件应包含一条包含字母"T"的行。

判断矩形和线段相交问题,有一个trick是当线段在矩形中也为T,因为题意是最少相交一个点就是相交。最后判断相交就是判段线段和矩形四个边的相交了。

#include 
#include
#include
using namespace std;
typedef double db;
const db eps=1e-6;
struct Point{
	db x,y;
	Point(db x=0,db y=0):x(x),y(y){}
};
typedef Point Vector;
int sgn(db x){
	if(fabs(x)0) return 1;
	return -1;
}
Vector operator+(Vector a,Vector b){
	return Vector(a.x+b.x,a.y+b.y);
}
Vector operator-(Vector a,Vector b){
	return Vector(a.x-b.x,a.y-b.y);
}
Vector operator*(Vector a,db p){
	return Vector(a.x*p,a.y*p);
}
Vector operator/(Vector a,db p){
	return Vector(a.x/p,a.y/p);
}
db Cross(Vector a,Vector b){
	return a.x*b.y-a.y*b.x;
}
db Dot(Vector a,Vector b){
	return a.x*b.x+a.y*b.y;
}
bool OnSegment(Point p, Point a1, Point a2){
    return sgn(Cross(a1-p, a2-p)) == 0 && sgn(Dot(a1-p, a2-p)) < 0;
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
    double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);
    double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);
    //if判断控制是否允许线段在端点处相交,根据需要添加
    if(!sgn(c1) || !sgn(c2) || !sgn(c3) || !sgn(c4)){
        bool f1 = OnSegment(b1, a1, a2);
        bool f2 = OnSegment(b2, a1, a2);
        bool f3 = OnSegment(a1, b1, b2);
        bool f4 = OnSegment(a2, b1, b2);
        bool f = (f1|f2|f3|f4);
        return f;
    }
    return (sgn(c1)*sgn(c2) < 0 && sgn(c3)*sgn(c4) < 0);
}
struct Line{
	Point p,v;
//	Line(Point p,Point v):p(p),v(v){}
//	Point point(db t){
//		return v+(p-v)*t;
//	}
};
int main(int argc, char** argv) {
	int n;
	cin>>n;
	while(n--){
		db lx,ly,rx,ry,sx,sy,ex,ey;
		cin>>sx>>sy>>ex>>ey>>lx>>ly>>rx>>ry;
		Line l;
		l.p.x=sx,l.p.y=sy;
		l.v.x=ex,l.v.y=ey;
		
		Line up,down,left,right;
		
		up.p.x=lx,up.p.y=ly;
		up.v.x=rx,up.v.y=ly;
		
		down.p.x=lx,down.p.y=ry;
		down.v.x=rx,down.v.y=ry;
		
		left.p.x=lx,left.p.y=ry;
		left.v.x=lx,left.v.y=ly;
		
		right.p.x=rx,right.p.y=ry;
		right.v.x=rx,right.v.y=ly;
		
		if((sx>=lx&&sx<=rx)||(sx>=rx&&sx<=lx)){
			if((sy>=ry&&sy<=ly)||(sy>=ly&&sy<=ry)){
				printf("T\n");
				continue;
			}
			
		}
		if((ex>=lx&&ex<=rx)||(ex>=rx&&ex<=lx)){
			if((ey>=ry&&ey<=ly)||(ey>=ly&&ey<=ry)){
				printf("T\n");
				continue;
			}
			
		}
//		if((lx==sx&&ly==sy)||(lx==ex&&ly==ey)||(lx==sx&&ry==sy)||(lx==ex&&ry==ey)||(rx==sx&&ly==sy)||(rx==ex&&ly==ey)||(rx==sx&&ry==sy)||(rx==ex&&ry==ey)){
//			printf("T\n");
//			continue;
//		}
		if(SegmentProperIntersection(l.p,l.v,up.p,up.v)||SegmentProperIntersection(l.p,l.v,down.p,down.v)||SegmentProperIntersection(l.p,l.v,left.p,left.v)||SegmentProperIntersection(l.p,l.v,right.p,right.v))
		    printf("T\n");
		else printf("F\n");
	}
	return 0;
}

 

你可能感兴趣的:(C++算法入门,计算几何)