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.

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

题意:求线段是否在矩形内或是否于矩形相交;设线段两坐标分别为

(lx_start, ly_start),(lx_end,ly_end);

扫描二维码关注公众号,回复: 13301804 查看本文章

红色代表线段,黑色代表边

矩形内:

与矩形相交(即与四条边中的一条或多条相交):

特殊情况:lx_end==lx_start 或 ly_end==ly_start

普通情况:

即判断线段相交,需分别判断x,y,(1),(2)线段与边皆不相交,(3)相交

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int k;
	cin>>k;
	while(k--)
	{
		double lx_start,ly_start,lx_end,ly_end;
		double x2,y2,x1,y1;
		double x_max,y_max,x_min,y_min;
		double lx_max,ly_max,lx_min,ly_min;
		cin>>lx_start>>ly_start>>lx_end>>ly_end;
		cin>>x1>>y1>>x2>>y2;
		x_max=max(x1,x2);
		x_min=min(x1,x2);
		y_max=max(y1,y2);
		y_min=min(y1,y2);
		ly_max=max(ly_end,ly_start);
		ly_min=min(ly_end,ly_start);
		lx_max=max(lx_end,lx_start);
		lx_min=min(lx_end,lx_start);
		int f=0,mark=0;
		if(lx_start<=x_max&&lx_start>=x_min)f++;
		if(ly_start<=y_max&&ly_start>=y_min)f++;
		if(lx_end<=x_max&&lx_end>=x_min)f++;
		if(ly_end<=y_max&&ly_end>=y_min)f++;
		if(lx_end==lx_start) //特殊情况 ,是针对普通情况下而言的(即特殊情况不单独拎出来,在普通情况下,分母可能为0) 
		{
			if(lx_end>=x_min&&lx_end<=x_max)
			{
				if(ly_min<=y_min&&ly_max>=y_min)mark=1;
				if(ly_min<=y_max&&ly_max>=y_max)mark=1;
			}
		}
		else if(ly_end==ly_start)//特殊情况 
		{
			if(ly_end>=y_min&&ly_end<=y_max)
			{
				if(lx_min<=x_min&&lx_max>=x_min) mark=1;
				if(lx_min<=x_max&&lx_max>=x_max) mark=1;
			}
		}
		else	//判断四条边,即四条线段与题目所给线段是否相交 
		{
			double a=(ly_end-ly_start)/(lx_end-lx_start);
			double b=ly_end-lx_end*a;
			double t;
			if(x_min>=lx_min&&x_min<=lx_max)//针对普通情况 , (1) 
			{
				t=a*x_min+b;
				if(t<=y_max&&t>=y_min)mark=1;//针对普通情况 , (2) 
			}
			if(x_max>=lx_min&&x_max<=lx_max)
			{
				t=a*x_max+b;
				if(t<=y_max&&t>=y_min)mark=1;
			}
			if(y_min>=ly_min&&y_min<=ly_max)
			{
				t=(y_min-b)/a;
				if(t<=x_max&&t>=x_min)mark=1;
			}
			if(y_max>=ly_min&&y_max<=ly_max)
			{
				t=(y_max-b)/a;
				if(t<=x_max&&t>=x_min)mark=1;
			}
		}
		if(mark==1||f==4)puts("T");
		else puts("F");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_52898168/article/details/120873897
今日推荐