小白算法练习 计算几何 线段与矩阵相交 POJ1410 Intersection

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36336522/article/details/83042301

Intersection

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17373   Accepted: 4470

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. 

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

//题目没什么难度有几个坑点,第一个就是给个矩阵的点不一定都是左上角和右下角,顺序也可能不一样。第二个是共线的时候,这个可能就我一个没考虑吧。。。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
struct Point{
	double x,y;
	Point(double x,double y):x(x),y(y){};
	Point(){};
};
int cross(Point p1,Point p2,Point p3){ //叉积 
	double ans=((p1.x-p3.x)*(p1.y-p2.y)-(p1.x-p2.x)*(p1.y-p3.y));
	if(ans==0) return 0;
	else 
	{
		if(ans<0) return -1;
		else return 1;
	}
}
int ispara(Point A,Point B,Point C,Point D){  //线段与线段是否相交 
	double hhigh=max(A.y,B.y);double hlow=min(A.y,B.y);double hright=max(A.x,B.x);double hleft=min(A.x,B.x);
	double thigh=max(C.y,D.y);double tlow=min(C.y,D.y);double tright=max(C.x,D.x);double tleft=min(C.x,D.x);
	if(hlow>thigh || hhigh<tlow || hright<tleft || hleft>tright) return 0;//不相交 
	if(cross(C,A,B)*cross(D,A,B)>0) return 0;//没有共线有=0
	if(cross(A,C,D)*cross(B,C,D)>0) return 0;
	return 1; //相交 
}
int main()
{
	
	 
	
	int T;scanf("%d",&T);
	while(T--){
		Point S,E;
		scanf("%lf %lf %lf %lf",&S.x,&S.y,&E.x,&E.y);
		double Rlx,Rly,Rrx,Rry;
		scanf("%lf %lf %lf %lf",&Rlx,&Rly,&Rrx,&Rry);
		Point p1(Rlx,Rly),p2(Rlx,Rry),p3(Rrx,Rry),p4(Rrx,Rly);
		int flag=0;
		if(max(S.x,E.x)<=max(Rrx,Rlx) && max(S.y,E.y)<=max(Rly,Rry) && min(S.x,E.x)>=min(Rlx,Rrx) && min(S.y,E.y)>=min(Rry,Rly))
		{
			flag=1;
		}
		else
		{
			if
			(   
				ispara(S,E,p1,p2) || ispara(S,E,p2,p3) ||
				ispara(S,E,p3,p4) || ispara(S,E,p4,p1)
			)	flag=1;
		}
		if(flag)
		{
			printf("T\n");
		}
		else
		{
			printf("F\n");
		}
	}
	
	return 0; 
} 

猜你喜欢

转载自blog.csdn.net/qq_36336522/article/details/83042301