poj 1410 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

题意:给定一条线段的两个端点,再给定矩形顶点的两个横坐标和两个总坐标(矩形的边与坐标轴平行),求线段和矩形是否有公共点

需要注意两点:

1、矩形是实心的(The rectangle consists of four straight lines and the area in between),所以如果线段完全在矩形内部也算相交;

2、给定的矩形坐标并不是按照矩形的端点坐标给出的,而需要自行判断。

那么所需判断的就是给定线段和矩形四条边是否相交,如果不相交,再判断线段是否在矩形内部。

#include<iostream>  
#include<algorithm>  
#include<string>  
#include<map>  
#include<vector>  
#include<cmath>  
#include<string.h>  
#include<stdlib.h>  
#include<cstdio>  
#define ll long long  
using namespace std;
struct point{  
    double x,y;  
    point(double a=0,double b=0){  
        x=a;  
        y=b;  
    }  
};  
struct Lineser{  
    point s,e;  
    Lineser(point a,point b){  
        s=a;  
        e=b;  
    }  
};  
double multiply(point sp,point ep,point op){  
    return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));  
}  
bool intersect(Lineser u,Lineser v){  
    return( (max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&&          //排斥实验  
            (max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&  
            (max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&  
            (max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&  
            (multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=0)&& //跨立实验  
            (multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=0));  
}  
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		point s,e,l,r;
		double x1,y1,x2,y2;
		cin>>s.x>>s.y>>e.x>>e.y>>l.x>>l.y>>r.x>>r.y;  
		if(l.x<r.x)
			x1=l.x,x2=r.x;
		else
			x1=r.x,x2=l.x;
		if(l.y<r.y)
			y1=l.y,y2=r.y;
		else
			y1=r.y,y2=l.y;
		point a(x1,y1),b(x2,y1),c(x1,y2),d(x2,y2);
		if(intersect(Lineser(a,b),Lineser(s,e))||intersect(Lineser(a,c),Lineser(s,e))||intersect(Lineser(b,d),Lineser(s,e))||intersect(Lineser(c,d),Lineser(s,e)))
			printf("T\n");  
		else if(min(s.x,e.x)>x1&&max(s.x,e.x)<x2&&min(s.y,e.y)>y1&&max(s.y,e.y)<y2)
			printf("T\n");  
		else
			printf("F\n");  
	}
	return 0;
}

下面是判断两条线段是否相交的模板:

struct point{  
    double x,y;  
    point(double a=0,double b=0){  //这种写法很好
        x=a;  
        y=b;  
    }  
};  
struct Lineser{  
    point s,e;  
    Lineser(point a,point b){  
        s=a;  
        e=b;  
    }  
};  
double multiply(point sp,point ep,point op){  
    return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));  
}  
bool intersect(Lineser u,Lineser v){  
    return( (max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&&          //排斥实验  
            (max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&  
            (max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&  
            (max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&  
            (multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=0)&& //跨立实验  
            (multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=0));  
}  
int main(){
	point s,e;
	double x1,y1,x2,y2;
	point a(x1,y1),b(x2,y1);
	if(intersect(Lineser(a,b),Lineser(s,e)))
		......
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值