poj1269(直线交点)

 

传送门:Intersecting Lines

题意:给出N组直线,每组2条直线,求出直线是否相交。如果共线则输出LINE,相交则输入点坐标,否则输出NONE.

分析:模板裸题,直接上模板。。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>

using namespace std;

const double eps = 1e-8;
const double PI = acos(-1.0);
const int N = 110;
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    //叉积
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
};
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;e = _e;
    }
    //两直线相交求交点
    //第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
    //只有第一个值为2时,交点才有意义
    pair<int,Point> operator &(const Line &b)const
    {
        Point res = s;
        if(sgn((s-e)^(b.s-b.e)) == 0)
        {
            if(sgn((s-b.e)^(b.s-b.e)) == 0)
                return make_pair(0,res);//重合
            else return make_pair(1,res);//平行
        }
        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x += (e.x-s.x)*t;
        res.y += (e.y-s.y)*t;
        return make_pair(2,res);
    }
};
Line seg[10];
int main()
{
    int T;
    scanf("%d",&T);
    puts("INTERSECTING LINES OUTPUT");
    while(T--)
    {
        for(int i=1;i<=2;i++)
        {
            double a,b,c,d;
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            seg[i]=Line(Point(a,b),Point(c,d));
        }
        pair<int,Point> p=seg[1]&seg[2];
        if(p.first==0)puts("LINE");
        else if(p.first==1)puts("NONE");
        else
        {
            printf("POINT %.2lf %.2lf\n",p.second.x,p.second.y);
        }
    }
    puts("END OF OUTPUT");
    return 0;
}
View Code

 

posted on 2015-03-12 19:58  lienus  阅读(397)  评论(0编辑  收藏  举报

导航