UVA - 191 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)
线:起点:(4,9)
end point: (11,2)
终点:(11,2)
rectangle: left-top: (1,5)
矩形:左上角:(1,5)
right-bottom: (7,1)
右下角:(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.
如果线和矩形至少有一个共同点,则称该线与矩形相交。 矩形由四条直线和之间的区域组成。尽管所有输入值都是整数,但有效交叉点不必位于整数网格上。

【输入】
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:
输入包含n个测试用例。 输入文件的第一行包含数字n。 以下每行包含一个格式的测试用例:
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 left and bottom right do not imply any ordering of coordinates.
其中(xstart,ystart)是起点,(xend,yend)是行的终点,(xlef t,ytop)是左上角,(xright,ybottom)是矩形的右下角。 八个数字用空格分隔。 术语top lef t和right right并不意味着任何坐标顺序。

【输出】
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.
对于输入文件中的每个测试用例,输出文件应该包含一行,如果线段与矩形相交,则由字母‘T’组成;如果线段与矩形不相交,则包含字母‘F’。

【样例输入】
1
4 9 11 2 1 5 7 1

【样例输出】
F

题目链接:https://cn.vjudge.net/problem/UVA-191

基础计算几何,判断线段和线段,线段和点的关系

代码如下:

#include <iostream>
#include <cmath>
using namespace std;
static const double EPS=1e-8;
static const double PI=acos(-1.0);
int sgn(double x)
{
    if(fabs(x)<EPS) return 0;
    if(x<0) return -1;
    else return 1;
}
struct Point{
    double x,y;
    Point(){}
    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){}
    int relation(Point p) {
        int c=sgn((p-s)^(e-s));
        if(c<0) return 1; //左侧
        else if(c>0) return 2; //右侧
        else return 3; //在直线上
    }
    int segcrosseg(Line v) {
        int d1=sgn((e-s)^(v.s-s)),d2=sgn((e-s)^(v.e-s)),d3=sgn((v.e-v.s)^(s-v.s)),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()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        int xstart,ystart,xend,yend;
        cin>>xstart>>ystart>>xend>>yend;
        Point p(xstart,ystart),q(xend,yend);
        Line line(p,q);
        int xleft,ytop,xright,ybotton;
        cin>>xleft>>ytop>>xright>>ybotton;
        Point p1(xleft,ytop),p2(xleft,ybotton),p3(xright,ytop),p4(xright,ybotton);
        Line line1(p1,p3),line2(p2,p4),line3(p1,p2),line4(p3,p4);
        if(!line.segcrosseg(line1) && !line.segcrosseg(line2) && !line.segcrosseg(line3) && !line.segcrosseg(line4) && 
        !(line1.relation(p)+line2.relation(p)==3 && line1.relation(q)+line2.relation(q)==3 && 
        line3.relation(p)+line4.relation(p)==3 && line3.relation(q)+line4.relation(q)==3))
            cout<<"F"<<endl;
        else
            cout<<"T"<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值