poj 1410 Intersection (线段相交判定)

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14287 Accepted: 3735

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

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:给出一个矩形和一条线段,判断两者是否有交点。

题解:四条边与给出的线段判断是否相交,然后特判线段是否在矩形的内部。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define eps 1e-7
using namespace std;
struct point
{
	double x,y;
	point (double X=0,double Y=0) {
		x=X,y=Y;
	}
}tmp,tmp1,a[10];
typedef point vector;
point operator -(point a,point b){
	return vector (a.x-b.x,a.y-b.y);
}
int n;
int dcmp(double x)
{
	if (fabs(x)<eps) return 0;
	return x<0?-1:1;
}
double  cross(vector a,vector b)
{
	return a.x*b.y-a.y*b.x;
}
int segmentpd(point a1,point a2,point b1,point b2)
{
	if (min(a1.x,a2.x)>max(b1.x,b2.x)||min(a1.y,a2.y)>max(b1.y,b2.y)||min(b1.x,b2.x)>max(a1.x,a2.x)||min(b1.y,b2.y)>max(a1.y,a2.y)) return 0;
	double c1=cross(a2-a1,b1-a1),c2=cross(a2-a1,b2-a1),
	    c3=cross(b2-b1,a1-b1),c4=cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<=0&&dcmp(c3)*dcmp(c4)<=0;
}

int pd(point now)
{
	double x=now.x; double y=now.y;
	return x>=a[1].x&&x<=a[3].x&&y<=a[1].y&&y>=a[3].y;
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
	    scanf("%lf%lf",&tmp.x,&tmp.y);
	    scanf("%lf%lf",&tmp1.x,&tmp1.y);
		scanf("%lf%lf",&a[1].x,&a[1].y);
		scanf("%lf%lf",&a[3].x,&a[3].y);
		if (a[1].x>a[3].x) swap(a[1].x,a[3].x);
		if (a[1].y<a[3].y) swap(a[1].y,a[3].y);
        a[2].x=a[3].x; a[2].y=a[1].y;
		a[4].x=a[1].x; a[4].y=a[3].y;
		int t=0;
		t+=segmentpd(tmp,tmp1,a[1],a[2]);
		t+=segmentpd(tmp,tmp1,a[2],a[3]);
		t+=segmentpd(tmp,tmp1,a[3],a[4]);
		t+=segmentpd(tmp,tmp1,a[4],a[1]);
	    if (pd(tmp)||pd(tmp1)) t++;
		if (t) printf("T\n");
		else printf("F\n");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值