欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poj 1269 Intersecting Lines 【直线相交】

程序员文章站 2022-03-30 09:04:14
...



点击打开链接


题意:


         给你两个条直线,判断两条直线的关系,

         如果相交于一点,输出POINT 和该点坐标,

         如果重合输出 LINE

         如果平行输出 NONE


题解:


        赤裸裸的判断两线相交,,



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
#define x first
#define y second
using namespace std;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=105;
int sgn(double x){
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    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;
    }
    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);
            return make_pair(1,res);
        }
        double tt=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x+=(e.x-s.x)*tt;
        res.y+=(e.y-s.y)*tt;
        return make_pair(2,res);
    }
};
Point p1,p2,p3,p4;
Line t1,t2;
int main(){
    int t,n,cnt;
    double x1,x2,x3,x4,y1,y2,y3,y4;
    scanf("%d",&t);
    puts("INTERSECTING LINES OUTPUT");
    while(t--){
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
        p1={x1,y1};p2={x2,y2};
        p3={x3,y3};p4={x4,y4};
        t1={p1,p2};t2={p3,p4};
        pair<int,Point>ans=t1&t2;
        if(ans.x==0) puts("LINE");
        else if(ans.x==1) puts("NONE");
        else {
            printf("POINT %.2f %.2f\n",ans.y.x,ans.y.y);
        }
    }
    puts("END OF OUTPUT");
	return 0;
}