poj1410 Intersection

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16011 Accepted: 4149

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

Source

Southwestern European Regional Contest 1995


题解:

求一条直线和一个矩形是否相交。

不懂网上的人为什么都判的那么复杂QWQ

只要判断直线是否和矩形的两条对角线相交就可以了,但是前面要判掉线段顶点在矩形内部的情况(这样相交的情况只能是线段两端点在矩形的两边,所以一定和对角线相交),否则线段就算和矩形相交,也不一定和对角线相交



AC代码(因为没注意到矩阵坐标并不一定是左上和右下,导致一直WA):

#include <iostream>  
#include <stdio.h>  
#include <string.h>  
#include <algorithm>  
#include <queue>  
#include <map>  
#include <vector>  
#include <set>  
#include <string>  
#include <math.h>
using namespace std;
struct aaa{
	double x,y;
}a[100],b[100];
int t1;
double direction(aaa pi,aaa pj,aaa pk){  
    return(pj.x-pi.x)*(pk.y-pi.y)-(pk.x-pi.x)*(pj.y-pi.y);  
}  
int on_segment(aaa pi,aaa pj,aaa pk){  
    if((min(pi.x,pj.x)<=pk.x)&&(max(pi.x,pj.x)>=pk.x)&&(min(pi.y,pj.y)<=pk.y)&&(max(pi.y,pj.y)>=pk.y))return 1;  
    return 0;  
}  
int pd(int i,int j){  
    int rr=0;  
    if(((max(a[i].x,b[i].x)>=min(a[j].x,b[j].x))&&(min(a[i].x,b[i].x)<=max(a[j].x,b[j].x))  
     &&(max(a[i].y,b[i].y)>=min(a[j].y,b[j].y))&&(min(a[i].y,b[i].y)<=max(a[j].y,b[j].y)))){  
         rr=1;  
     }  
      else return 0;  
    double d1=direction(a[j],b[j],a[i]),d2=direction(a[j],b[j],b[i]),d3=direction(a[i],b[i],a[j]),d4=direction(a[i],b[i],b[j]);  
    if(((d1>0&&d2<0)||(d1<0&&d2>0))&&((d3>0&&d4<0)||(d3<0&&d4>0)))return 1;  
    if(d1==0&&on_segment(a[j],b[j],a[i]))return 1;  
    if(d2==0&&on_segment(a[j],b[j],b[i]))return 1;  
    if(d3==0&&on_segment(a[i],b[i],a[j]))return 1;  
    if(d4==0&&on_segment(a[i],b[i],b[j]))return 1;  
    return 0;  
}  
int main(){
	double x,y,xx,yy,xxx,yyy,xx1,yy1;
	scanf("%d",&t1);
	while(t1--){
		scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a[1].x,&a[1].y,&b[1].x,&b[1].y,&xxx,&yyy,&xx1,&yy1);
		if(xxx<xx1){
			x=xxx;xx=xx1;
		}
		 else{
		 	x=xx1;xx=xxx;
		 }
		if(yyy>yy1){
			y=yyy;yy=yy1;
		} 
		 else{
		 	y=yy1;yy=yyy;
		 }
		if((a[1].x>=x&&a[1].x<=xx&&a[1].y<=y&&a[1].y>=yy)||(b[1].x>=x&&b[1].x<=xx&&b[1].y<=y&&b[1].y>=yy)){
			printf("T\n");
			continue;
		}
		a[2].x=x;a[2].y=y;b[2].x=xx;b[2].y=yy;
		a[3].x=x;a[3].y=yy;b[3].x=xx;b[3].y=y;
		if(pd(1,2)||pd(1,3))printf("T\n");
		 else printf("F\n");
	}
}


阅读更多
个人分类: poj 计算几何
上一篇poj1556 The Doors
下一篇poj1039 Pipe
想对作者说点什么? 我来说一句
相关热词

没有更多推荐了,返回首页

关闭
关闭
关闭