POJ 1410 Intersection(线段与矩形相交)

判断矩形和线段是否相交,没什么说的,

正确理解题意中相交的定义

具体代码:

#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
typedef struct
{
    double x;
    double y;
}point;
point rect[4];
point line[2];

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

bool is_cross(point s1,point e1,point s2,point e2)//判断线段是否相交(非规范相交)
{
    return max(s1.x,e1.x) >= min(s2.x,e2.x)&&max(s2.x,e2.x) >= min(s1.x,e1.x)&&
    max(s1.y,e1.y) >= min(s2.y,e2.y)&&max(s2.y,e2.y) >= min(s1.y,e1.y)&&
    multi(s1,e1,s2)*multi(s1,e1,e2) <= 0&&multi(s2,e2,s1)*multi(s2,e2,e1) <= 0;
}
int main()
{
    int i,j;
    int t;
    bool flag;
    double bigx,smallx,bigy,smally;
    scanf("%d",&t);
    while(t--)
    {
        flag=true;
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&line[0].x,&line[0].y,&line[1].x,&line[1].y,&rect[3].x,&rect[3].y,&rect[1].x,&rect[1].y);
        rect[0].x=rect[3].x;
        rect[0].y=rect[1].y;
        rect[2].x=rect[1].x;
        rect[2].y=rect[3].y;
        bigx=max(rect[3].x,rect[1].x),smallx=min(rect[3].x,rect[1].x),bigy=max(rect[3].y,rect[1].y),smally=min(rect[3].y,rect[1].y);
        if(is_cross(line[0],line[1],rect[0],rect[1])&&is_cross(line[0],line[1],rect[1],rect[2])&&is_cross(line[0],line[1],rect[2],rect[3])&&is_cross(line[0],line[1],rect[3],rect[0]))
        {
            flag=false;
            printf("T\n");
            continue;
        }

        if((line[0].x>smallx&&line[0].x<bigx&&line[0].y>smally&&line[0].y<bigy)||(line[1].x>smallx&&line[1].x<bigx&&line[1].y>smally&&line[1].y<bigy))
        {
            flag=false;
            printf("T\n");
            continue;
        }
        printf("F\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值