POJ 1410 Intersection 计算几何 判断线段与矩形位置

G - Intersection
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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



判断线段和矩形面积是否相交

ACcode:

#include <iostream>
#include <cstdio>
#include <cmath>
#define eps 1e-8
using namespace std;
int sgn(double x){
    if(fabs(x)<eps)return 0;
    if(x<0)return -1;
    return 1;
}
struct Point{
    double x,y;
    Point(){}
    void out(){
        printf("( %lf ,%lf )  ",x,y);
    }
    Point(double _x,double _y){
        x=_x;
        y=_y;
    }
    Point operator -(const Point &b)const{
        return Point(x-b.x,y-b.y);
    }
    double operator ^(const Point &b)const{
        return x*b.y-y*b.x;
    }
    double operator *(const Point &b)const{
        return x*b.x+y*b.y;
    }
};
struct Line{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e){
        s=_s;
        e=_e;
    }
    void out(){
        s.out();
        e.out();
        cout<<'\12';
    }
    int segcrossseg(Line v){
        int d1=sgn((e-s)^(v.s-s));
        int d2=sgn((e-s)^(v.e-s));
        int d3=sgn((v.e-v.s)^(s-v.s));
        int d4=sgn((v.e-v.s)^(e-v.s));
        if((d1^d2)==-2&&(d3^d4)==-2)return 2;
        return (d1==0&&sgn((v.s-s)*(v.s-e))<=0)||(d2==0&&sgn((v.e-s)*(v.e-e))<=0)||(d3==0&&sgn((s-v.s)*(s-v.e))<=0)||(d4==0&&sgn((e-v.s)*(e-v.e))<=0);
    }
};
int main(){
    int loop;
    scanf("%d",&loop);
    while(loop--){
        double x1,x2,y1,y2,sx,sy,ex,ey;
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&sx,&sy,&ex,&ey,&x1,&y1,&x2,&y2);
        Line tmp=Line(Point(sx,sy),Point(ex,ey));
        Line line1=Line(Point(x1,y1),Point(x1,y2));
        Line line2=Line(Point(x1,y1),Point(x2,y1));
        Line line3=Line(Point(x2,y2),Point(x2,y1));
        Line line4=Line(Point(x2,y2),Point(x1,y2));
        int ans=0;
        if(tmp.segcrossseg(line1))ans++;
        if(tmp.segcrossseg(line2))ans++;
        if(tmp.segcrossseg(line3))ans++;
        if(tmp.segcrossseg(line4))ans++;
        if(ans==0){
            double maxx=max(x1,x2);
            double maxy=max(y1,y2);
            double minx=min(x1,x2);
            double miny=min(y1,y2);
            if(maxx>sx&&maxy>sy&&maxx>ex&&maxy>ey&&minx<sx&&miny<sy&&minx<ex&&miny<ey)
                ans++;
        }
        printf(ans?"T\n":"F\n");
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值