uva 191 Intersection

题目:判断线段是否与矩形相交。

分析:计算几何,简单题。先判断线段在矩形内的情况,然后判断线段与四边关系即可。

注意:线段在矩形内部属于相交,线段与边平行时的特判。

/*
线段相交 
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>

#define deff 1e-6
//#define T
using namespace std;
struct Point
{
  double x;double y;
};


int direction(Point* pi,Point* pj,Point* pk)
{
    Point p1,p2;
    p1.x = pk->x - pi->x;
    p1.y = pk->y - pi->y;
    p2.x = pj->x - pi->x;
    p2.y = pj->y - pi->y;
    double cross = p1.x*p2.y - p2.x*p1.y;
    if(cross > deff)
        return 1;
    else if(cross < -deff)
        return -1;
    else
        return 0;
}

bool onSegment(Point* pi,Point* pj,Point* pk)
{
    double minx,miny,maxx,maxy;
    if(pi->x > pj->x)
    {
        minx = pj->x; maxx = pi->x;
    }
    else
    {
        minx = pi->x; maxx = pj->x;
    }
    if(pi->y > pj->y)
    {
        miny = pj->y; maxy = pi->y;
    }
    else
    {
        miny = pi->y; maxy = pj->y;
    }
    return (minx <= pk->x)&&(maxx >= pk->x)&&(miny <=pk->y)&&(maxy >= pk->y);
}

bool segmentIntersect(Point* p1,Point* p2,Point* p3,Point* p4)
{
    int d1 = direction(p3,p4,p1);
    int d2 = direction(p3,p4,p2);
    int d3 = direction(p1,p2,p3);
    int d4 = direction(p1,p2,p4);
    if(d1*d2 < 0&&d3*d4 < 0)
    {
       // printf("1\n");
         return true;
    }

    if(!d1 && onSegment(p3,p4,p1))
    {
        //printf("2\n");
        return true;
    }

    if(!d2 && onSegment(p3,p4,p2))
        {
           // printf("3\n");
            return true;
        }
    if(!d3 && onSegment(p1,p2,p3))
        {
           // printf("4\n");
            return true;
        }
    if(!d4 && onSegment(p1,p2,p4))
        {
           // printf("5\n");
            return true;
        }
    return false;
}

//判断线段是否在矩形内
bool inRectangle(Point* p1,Point* p2,Point* p3,Point* p4)
{
    return onSegment(p3,p4,p1)&&onSegment(p3,p4,p2);
}

int main()
{
    #ifdef T
     freopen("in.txt","r",stdin);
     freopen("out.txt","w",stdout);

    #endif
    Point p1,p2,p3,p4,p5,p6;
    bool b1,b2,b3,b4;
    int n;
   // int cas = 0;
    scanf("%d",&n);
    while(n--)
    {
       // printf("cas = %d\n",cas++);
        scanf("%lf %lf %lf %lf",&p1.x,&p1.y,&p2.x,&p2.y);
        scanf("%lf %lf %lf %lf",&p3.x,&p3.y,&p5.x,&p5.y);
        if(inRectangle(&p1,&p2,&p3,&p5))
        {
             printf("T\n");
             continue;
        }

        p4.x = p5.x;p4.y =p3.y;
        p6.x = p3.x;p6.y =p5.y;
        b1 = segmentIntersect(&p1,&p2,&p3,&p4);b2 = segmentIntersect(&p1,&p2,&p4,&p5);
        b3 = segmentIntersect(&p1,&p2,&p5,&p6);b4 = segmentIntersect(&p1,&p2,&p6,&p3);
        if(b1||b2||b3||b4)
         printf("T\n");
         else
            printf("F\n");

    }

   return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值