Intersecting Lines--POJ1269(判断两条直线的关系 && 求两条直线的交点)

http://poj.org/problem?id=1269

我今天才知道原来标准的浮点输出用%.2f   并不是%.2lf  所以wa了好几次  

 

题目大意:   就给你两个线段 然后求这两个线段所在的直线的关系  有共线  平行  和相交

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#define N 200
const double ESP = 1e-8;
struct Point
{
    double x, y;

    Point(double x=0,double y=0):x(x),y(y) {}
    Point operator + (const Point &temp)const{
        return Point(x+temp.x, y+temp.y);
    }
    Point operator - (const Point &temp)const{
        return Point(x-temp.x, y-temp.y);
    }
    bool operator == (const Point &temp)const{
        return (fabs(x-temp.x) < ESP && fabs(y-temp.y) < ESP);
    }
    int operator * (const Point &temp)const{
        double t=(x*temp.y)-(y*temp.x);
        if(t > ESP)
            return 1;
        if(fabs(t) < ESP)
            return 0;
        return -1;
    }
};

struct node
{
    Point A,B;
    node(Point A=0,Point B=0):A(A),B(B){}

};

Point line(Point u1,Point u2,Point v1,Point v2)///求交点模板
{
    Point ret=u1;
    double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));

    ret.x+=(u2.x-u1.x)*t;
    ret.y+=(u2.y-u1.y)*t;

    return ret;
}

int main()
{
    int n;
    scanf("%d",&n);
    printf("INTERSECTING LINES OUTPUT\n");
    while(n--)
    {
        Point p[10];
        node a[5];
        double x1,x2,x3,x4,y1,y2,y3,y4;
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
        p[1]=Point(x1,y1);
        p[2]=Point(x2,y2);
        p[3]=Point(x3,y3);
        p[4]=Point(x4,y4);
        a[1]=node(p[1],p[2]);
        a[2]=node(p[3],p[4]);
        if(fabs((a[1].A-a[2].A)*(a[2].B-a[2].A))==0 && fabs((a[1].B-a[2].A)*(a[2].B-a[2].A))==0)///判断共线 如果a[1]的两个点都在直线a[2]上  就说明共线
            printf("LINE\n");
        else
        {
            if(fabs((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1))<ESP)///如果不共线 并且斜率相等的话  就说明是平行
                printf("NONE\n");
            else///求交点
            {
                Point d;
                d=line(p[1],p[2],p[3],p[4]);
                printf("POINT %.2f %.2f\n",d.x,d.y);
            }
        }
    }
    printf("END OF OUTPUT\n");
    return 0;
}

 

posted @ 2016-04-21 12:02  啦咯  阅读(215)  评论(0编辑  收藏  举报