X-man

导航

poj 1269 Intersecting Lines

//判断直线有无交点
//斜率相同再判断点是否在直线上
//或一个
#include<stdio.h>
#include<math.h>
#define eps 1e-8
#define zero(x)(((x)>0 ? (x):(-x))<eps)
struct Point
{
    double x,y;
};

struct Line
{
    Point a,b;
}line1,line2;
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)//p0p1•p0p2点积
{
    return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}
int slope(Line line1,Line line2)
{
    if(fabs((line1.a.y-line1.b.y)*(line2.a.x-line2.b.x)-
            (line2.a.y-line2.b.y)*(line1.a.x-line1.b.x))<eps)
    return 1;
    return 0;
}

/*int dot_online_in(Point p,Line l)
{
    return zero(xmult(p,l.a,l.b))&&dmult(l.a,l.b,p)<eps;
}*/
int dot_online_in(Point p,Line l) //判点是否在线段上,包括端点
{                  //叉积判断是否在直线上
    //printf("%d\n%lf\n",zero(xmult(p,l.a,l.b)),dmult(l.a,l.b,p));
    //return zero(xmult(p,l.a,l.b))&&(l.a.x-p.x)*(l.b.x-p.x)<eps&&(l.a.y-p.y)*(l.b.y-p.y)<eps;
    return zero(xmult(p,l.a,l.b));//&&dmult(l.a,l.b,p)<eps; //等价
}

Point intersection(Line u,Line v)
{
    Point ret=u.a;
    double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))
            /((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
    ret.x+=(u.b.x-u.a.x)*t;
    ret.y+=(u.b.y-u.a.y)*t;
    return ret;
}

int main()
{
    int _case;
    int i,j,k;
    scanf("%d",&_case);
    printf("INTERSECTING LINES OUTPUT\n");
    for(i=0;i<_case;i++)
    {
        scanf("%lf%lf%lf%lf",&line1.a.x,&line1.a.y,&line1.b.x,&line1.b.y);
        scanf("%lf%lf%lf%lf",&line2.a.x,&line2.a.y,&line2.b.x,&line2.b.y);
        if(slope(line1,line2))
        {
            if(dot_online_in(line1.a,line2))
            {
                printf("LINE\n");
            }
            else
            {
                printf("NONE\n");
            }
        }
        else
        {
            Point ans=intersection(line1,line2);
            printf("POINT %.2lf %.2lf\n",ans.x,ans.y);
        }
    }
    printf("END OF OUTPUT\n");
    return 0;
}

 

posted on 2013-07-23 18:07  雨钝风轻  阅读(284)  评论(0编辑  收藏  举报