POJ 1269 Intersecting Lines 计算几何基础

#include<iostream>
#include<iomanip>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

const double eps=1e-8;

inline bool zero(double x)
{
    return fabs(x)<eps;
}

inline int sgn(double x)
{
    if (fabs(x)<eps) return 0;
    if (x<0) return -1;
    return 1;
}

struct Vector
{
    double x,y;
    Vector() {};
    Vector(double _x,double _y):x(_x),y(_y) {};
    Vector operator+(const Vector& b) const
    {
        return Vector(x+b.x,y+b.y);
    }
    Vector operator-(const Vector& b) const
    {
        return Vector(x-b.x,y-b.y);
    }
    Vector operator*(double q) const
    {
        return Vector(x*q,y*q);
    }
};

typedef Vector Point;

inline double DotProduct(const Vector& a,const Vector& b)
{
    return a.x*b.x+a.y*b.y;
}

inline double CrossProduct(const Vector& a,const Vector& b)
{
    return a.x*b.y-a.y*b.x;
}

struct Segment
{
    Point s,e;
    Segment() {};
    Segment(Point _s,Point _e):s(_s),e(_e) {};
};

bool IsIntersected(Segment l1,Segment l2)
{
    return max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
           max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
           max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
           max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
           sgn(CrossProduct(l2.s-l1.s,l1.e-l1.s))*sgn(CrossProduct(l2.e-l1.s,l1.e-l1.s)) <= 0 &&
           sgn(CrossProduct(l1.s-l2.s,l2.e-l2.s))*sgn(CrossProduct(l1.e-l2.s,l2.e-l2.s)) <= 0;
}

inline double Dist(Point a,Point b)
{
    return sqrt(DotProduct(b-a,b-a));
}

inline bool DotsInline(Point a,Point b,Point c)
{
    return zero(CrossProduct(a-c,b-c));
}

Point Intersection(Point u1,Point u2,Point v1,Point v2)
{
    Point Ans=u1;
    double temp=((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));
    Ans.x+=(u2.x-u1.x)*temp;
    Ans.y+=(u2.y-u1.y)*temp;
    return Ans;
}

int main()
{
    int n,x1,y1,x2,y2,x3,y3,x4,y4,temp,k1,k2,l1,l2;
    cin.sync_with_stdio(false);
    cout<<fixed<<setprecision(2);
    while (cin>>n)
    {
        cout<<"INTERSECTING LINES OUTPUT"<<endl;
        for (int i=1;i<=n;i++)
        {
            cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
            Point p1(x1,y1),p2(x2,y2),p3(x3,y3),p4(x4,y4);
            k1=x2-x1,l1=y2-y1,k2=x4-x3,l2=y4-y3;
            temp=__gcd(k1,l1);
            k1/=temp,l1/=temp;
            temp=__gcd(k2,l2);
            k2/=temp,l2/=temp;
            if (k1==k2&&l1==l2)
            {
                if (DotsInline(p1,p2,p3)&&DotsInline(p1,p2,p4))
                    cout<<"LINE"<<endl;
                else
                    cout<<"NONE"<<endl;
            }
            else
            {
                Point Ans=Intersection(p1,p2,p3,p4);
                cout<<"POINT "<<Ans.x<<' '<<Ans.y<<endl;
            }
        }
        cout<<"END OF OUTPUT"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值