POJ 1269 Intersecting Lines

Posted on 2017-01-11 11:08  ziliuziliu  阅读(117)  评论(0编辑  收藏  举报

直线求交,我的方法是叉积为0联立解方程。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 1050
#define eps 1e-9
using namespace std;
struct point
{
    double x,y;
    point (double x,double y):x(x),y(y) {}
    point () {}
    friend point operator -(point x,point y) 
    {
        return point(x.x-y.x,x.y-y.y);
    }
}p[maxn];
struct line
{
    point x,y,dt;
    line (point x,point y,point dt):x(x),y(y),dt(dt) {}
    line () {}
    friend double operator *(line x,line y)
    {
        return x.dt.x*y.dt.y-x.dt.y*y.dt.x;
    }
}l[maxn];
int n;
double a,b,c,d;
point ask_cross(line x,line y)
{
    double a,b,c,d,e,f;
    a=x.x.y-x.y.y;b=x.y.x-x.x.x;c=x.x.x*x.y.y-x.x.y*x.y.x;
    d=y.x.y-y.y.y;e=y.y.x-y.x.x;f=y.x.x*y.y.y-y.x.y*y.y.x;
    return point((b*f-c*e)/(a*e-b*d),(a*f-c*d)/(b*d-a*e));
}
int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        printf("INTERSECTING LINES OUTPUT\n");
        for (int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            l[1]=line(point(a,b),point(c,d),point(c,d)-point(a,b));
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            l[2]=line(point(a,b),point(c,d),point(c,d)-point(a,b));
            if (fabs(l[1]*l[2])<eps)
            {
                if (fabs(l[1]*line(l[1].x,l[2].x,l[2].x-l[1].x))<eps) printf("LINE\n");    
                else printf("NONE\n");
            }
            else
            {
                point now=ask_cross(l[1],l[2]);
                printf("POINT %.2f %.2f\n",now.x,now.y);
            }
        }
        printf("END OF OUTPUT\n");
    }
    return 0;
}