UVA--191--计算几何--判断线段交点--入坑

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)
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 xlef t ytop xright ybottom
where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xlef t, 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 lef t 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

卡点:判断线段在矩形内部的情况,很是难受;

思路:参考链接模板即可,注意左上右下的数据重新判断一次,否则出错;

(a1,b1)----(a2,b1)
|		|
|		|
(a1,b2)----(a2,b2)

模板:https://blog.csdn.net/queque_heiya/article/details/106187844

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath> 
using namespace std;
#define ll long long
const int maxa=2e3+10;
const double pi=1e-10;
const int inf=0x3f3f3f3f;
int n;
struct line{
	int x1,y1;
	int x2,y2;
}a[1];
struct point{
	int x1,y1;
	int x2,y2;
}b[4];
int a1,a2,b1,b2;
bool check(line &a,point &b){
	if(!(min(a.x1,a.x2)<=max(b.x1,b.x2)
	&&min(b.y1,b.y2)<=max(a.y1,a.y2)
	&&min(b.x1,b.x2)<=max(a.x1,a.x2)
	&&min(a.y1,a.y2)<=max(b.y1,b.y2)))	return false;
	double u,v,w,z;
    u=(b.x1-a.x1)*(a.y2-a.y1)-(a.x2-a.x1)*(b.y1-a.y1);
    v=(b.x2-a.x1)*(a.y2-a.y1)-(a.x2-a.x1)*(b.y2-a.y1);
    w=(a.x1-b.x1)*(b.y2-b.y1)-(b.x2-b.x1)*(a.y1-b.y1);
    z=(a.x2-b.x1)*(b.y2-b.y1)-(b.x2-b.x1)*(a.y2-b.y1);
    return (u*v<=pi&&w*z<=pi);
}
int main(){
	while(~scanf("%d",&n)){ 
		for(int i=0;i<n;i++){
			scanf("%d%d%d%d",&a[0].x1,&a[0].y1,&a[0].x2,&a[0].y2);//check(a,b); 
			scanf("%d%d%d%d",&a1,&b1,&a2,&b2);//生成四条直线的结构体数组
			if(a1>a2)	swap(a1,a2);
			if(b1<b2)	swap(b1,b2);
			b[0].x1=a1,b[0].y1=b1,b[0].x2=a2,b[0].y2=b1;
			b[1].x1=a1,b[1].y1=b1,b[1].x2=a1,b[1].y2=b2;
			b[2].x1=a1,b[2].y1=b2,b[2].x2=a2,b[2].y2=b2;
			b[3].x1=a2,b[3].y1=b2,b[3].x2=a2,b[3].y2=b1;//先判断线段是否在矩形内部
			if(a[0].x1<=a2&&a[0].x1>=a1&&a[0].x2<=a2&&a[0].x2>=a1
			&&a[0].y1<=b1&&a[0].y1>=b2&&a[0].y2<=b1&&a[0].y2>=b2){
				printf("T\n");//这里我写成了F,所以答案一直出错 
				continue; 
			} 
			int f=0;
			for(int j=0;j<4;j++)//再判断四边与线段的关系 
				if(check(a[0],b[j])){
					printf("T\n");
					f=1;
					break;
				}
			if(!f)	printf("F\n");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/queque_heiya/article/details/106222477