Intersection (计算几何)

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.
果然需要积累模板...一下子就用到了

简单说就是先判断线段是不是在矩形内,再分别判断和四条边是否相交...

判断相交貌似一直是比较棘手的问题啊,不过这种线线相交还好...

#include<cstdio>
#include<cstring>
#include<cmath>
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
struct point
{
    double x, y;
};

double xmult(point p1, point p2, point p0)
{
    return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
}

double dmult(point p1, point p2, point p0)
{
    return (p1.x - p0.x)*(p2.x - p0.x) + (p1.y - p0.y)*(p2.y - p0.y);
}

int dots_inline(point p1, point p2, point p3)
{
    return zero(xmult(p1, p2, p3));
}

int dot_online_in(point p, point l1, point l2)
{
    return zero(xmult(p, l1, l2)) && (l1.x - p.x)*(l2.x - p.x)<eps && (l1.y - p.y)*(l2.y - p.y)<eps;
}

int same_side(point p1, point p2, point l1, point l2)
{
    return xmult(l1, p1, l2)*xmult(l1, p2, l2)>eps;
}

int intersect_in(point u1, point u2, point v1, point v2)
{
    if (!dots_inline(u1, u2, v1) || !dots_inline(u1, u2, v2))
        return !same_side(u1, u2, v1, v2) && !same_side(v1, v2, u1, u2);
    return dot_online_in(u1, v1, v2) || dot_online_in(u2, v1, v2) || dot_online_in(v1, u1, u2) || dot_online_in(v2, u1, u2);
}
int main()
{
    point x, y;
    point d1, d2, d3, d4;
    int cas, i, j, k;
    scanf("%d", &cas);
    while (cas--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x.x, &x.y, &y.x, &y.y, &d4.x, &d4.y, &d2.x, &d2.y);
        d1.x = d4.x, d1.y = d2.y;
        d3.x = d2.x, d3.y = d4.y;
        if ((x.x >= d2.x&&x.x <= d4.x) || (x.x >= d4.x&&x.x <= d2.x))
            if ((x.y >= d2.y&&x.y <= d4.y) || (x.y >= d4.y&&x.y <= d2.y))
            {
                printf("T\n");
                continue;
            }
        if ((y.x >= d2.x&&y.x <= d4.x) || (y.x >= d4.x&&y.x <= d2.x))
            if ((y.y >= d2.y&&y.y <= d4.y) || (y.y >= d4.y&&y.y <= d2.y))
            {
                printf("T\n");
                continue;
            }
        if (intersect_in(x, y, d1, d2) || intersect_in(x, y, d3, d2) || intersect_in(x, y, d3, d4) || intersect_in(x, y, d1, d4))
            printf("T\n");
        else printf("F\n");
    }
    return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值