X-man

导航

poj 1410 Intersection

**必须注意几点


1、文中给出的左上顶点和右下顶点不保证x1<x2,y1>y2;即需要自己判断

2、文中似乎没说,但必须这么认为:线段完全在矩形内部要返回T.

3.判断两个线段相交时,注意它们在一条直线上的情况
#include<stdio.h>
struct Point
{
    int x,y;
}pointc,point1,point2,point[5];
struct Line
{
    Point a,b;
}linex;
int xmult(Point p1,Point p2,Point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int 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 main()
{
    int i,j,_case;
    scanf("%d",&_case);
    while(_case--)
    {
        scanf("%d%d%d%d",&linex.a.x,&linex.a.y,&linex.b.x,&linex.b.y);
        scanf("%d%d%d%d",&point1.x,&point1.y,&point2.x,&point2.y);
        if(point1.x>point2.x)
        {
            pointc.x=point2.x;
            point2.x=point1.x;
            point1.x=pointc.x;
        }
        if(point1.y>point2.y)
        {
            pointc.y=point2.y;
            point2.y=point1.y;
            point1.y=pointc.y;
        }
        if(linex.a.x>=point1.x&&linex.a.x<=point2.x&&
           linex.a.y>=point1.y&&linex.a.y<=point2.y)//至少有一个点在矩形中
        {
            printf("T\n");
            continue;
        }
        if(linex.b.x>=point1.x&&linex.b.x<=point2.x&&
           linex.b.y>=point2.y&&linex.b.y<=point1.y)//至少有一个点在矩形中
        {
            printf("T\n");
            continue;
        }
        point[0]=point1;
        point[1].x=point1.x;point[1].y=point2.y;
        point[2]=point2;
        point[3].x=point2.x;point[3].y=point1.y;
        point[4]=point1;
        for(i=0;i<4;i++)
        {
            int xm1=xmult(linex.a,point[i],point[i+1])*xmult(linex.b,point[i],point[i+1]);
            int xm2=xmult(point[i],linex.a,linex.b)*xmult(point[i+1],linex.a,linex.b);
            if(xm1<=0&&xm2<=0)
               {
                   //printf("%d %d %d %d\n",point[i].x,point[i].y,point[i+1].x,point[i+1].y);
                   if(xm1==0&&xm2==0)
                   {
                       if(dmult(linex.a,linex.b,point[i])<=0)break;
                       if(dmult(linex.a,linex.b,point[i+1])<=0)break;
                       if(dmult(point[i],point[i+1],linex.a)<=0)break;
                       if(dmult(point[i],point[i+1],linex.b)<=0)break;
                       continue;
                   }
                   break;
               }
        }
        if(i==4)printf("F\n");
        else printf("T\n");
    }
    return 0;
}
官方测试数据:

 http://hi.baidu.com/bobo__bai/item/899093e45920b70f8c3ea809

posted on 2013-07-24 20:06  雨钝风轻  阅读(157)  评论(0编辑  收藏  举报