POJ 1269 Intersecing Lines <计算几何>

题目

水题。。。

代码

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

const double EPS=1e-8;

struct Point;
typedef Point Vec;
int dblcmp(double);

struct Point{
    double x,y;
    Point(){}
    Point(double xx,double yy):x(xx),y(yy){}
    Vec operator -(Point p){
        return Point(x-p.x,y-p.y);
    }
    double operator ^(Vec v){
        return x*v.y-y*v.x;
    }
    friend ostream& operator <<(ostream&,Point&);
};

ostream& operator <<(ostream& out,Point p){
    return cout<<fixed<<setprecision(2)<<p.x<<' '<<p.y;
}

struct Line{
    Point p1,p2;
    Line(){}
    Line(Point pp1,Point pp2):p1(pp1),p2(pp2){}
    bool isParallel(Line l){
        return dblcmp((p2-p1)^(l.p2-l.p1))==0;
    }
    bool isCoincide(Line l){
        return dblcmp((p2-p1)^(l.p1-p1))==0&&
                dblcmp((p2-p1)^(l.p2-p1))==0;
    }
    Point intersection(Line l){
        Vec v1=p2-p1;
        Vec v2=l.p2-l.p1;
        double a1=-v1.y,a2=-v2.y;
        double b1=v1.x,b2=v2.x;
        double c1=p1.y*b1+p1.x*a1,c2=l.p1.y*b2+l.p1.x*a2;
        double d=a1*b2-a2*b1;
        return Point((c1*b2-c2*b1)/d,(a1*c2-a2*c1)/d);
    }
};

int dblcmp(double x){
    return fabs(x)<EPS?0:(x>0?1:-1);
}

int main(){
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    Line line_a,line_b;
    cout<<"INTERSECTING LINES OUTPUT"<<endl;
    while(n--){
        double x1,x2,x3,x4,y1,y2,y3,y4;
        cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
        line_a=Line(Point(x1,y1),Point(x2,y2));
        line_b=Line(Point(x3,y3),Point(x4,y4));
        if(line_a.isCoincide(line_b)) cout<<"LINE"<<endl;
        else if(line_a.isParallel(line_b)) cout<<"NONE"<<endl;
        else cout<<"POINT "<<line_a.intersection(line_b)<<endl;
    }
    cout<<"END OF OUTPUT"<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值