码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-1269 Intersecting Lines

时间:2018-11-23 23:25:47      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:cst   std   nes   amp   str   相交   algo   tor   while   

题意:判断两条直线相交,平行还是重合

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Point{
    double x,y;
    Point(double xx=0,double yy=0){
        x=xx,y=yy;
    }
}s1,s2,t1,t2;
struct Vector{
    double x,y;
    Vector(double xx=0,double yy=0){
        x=xx,y=yy;
    }
};
int t;
int dcmp(double x){return fabs(x)<1e-9?0:(x>0?1:-1);}
Vector operator - (Point a,Point b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator * (double p,Vector a){return Vector(a.x*p,a.y*p);}
Point operator + (Point a,Vector b){return Point(a.x+b.x,a.y+b.y);}
double operator * (Vector a,Vector b){return a.x*b.y-a.y*b.x;}
double len(Vector a){return sqrt(a.x*a.x+a.y*a.y);}
double distl(Point p,Point a,Point b){
    Vector v1=b-a,v2=p-a;
    return v1*v2/len(v1);
}
Point fla(Point a,Point b,Point c,Point d){
    Vector v1=b-a,v2=d-c,v3=a-c;
    return a+v2*v3/(v1*v2)*v1;
}
int check(Point a,Point b,Point c,Point d){
    Vector x=b-a,y=d-c;
    Vector v1=c-a,v2=d-a;
    int ax=dcmp(x*v1),bx=dcmp(x*v2);
    v1=a-c,v2=b-c;
    int ay=dcmp(y*v1),by=dcmp(y*v2);
    if((ax==bx&&ax==0)||(ay==by&&ay==0)) return 0;
    if((ax==bx&&(distl(c,a,b)==distl(d,a,b)))||(ay==by&&distl(a,c,d)==distl(b,c,d))) return 1;
    return 2;
}
int main(){
    scanf("%d",&t);
    printf("INTERSECTING LINES OUTPUT\n");
    while(t--){
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&s1.x,&s1.y,&t1.x,&t1.y,&s2.x,&s2.y,&t2.x,&t2.y);
        int x=check(s1,t1,s2,t2);
        if(!x) printf("LINE\n");
        if(x==1) printf("NONE\n");
        if(x==2){
            Point b=fla(s1,t1,s2,t2);
            printf("POINT %.2lf %.2lf\n",b.x,b.y);
        }
    }
    printf("END OF OUTPUT\n");
    return 0;
}

POJ-1269 Intersecting Lines

标签:cst   std   nes   amp   str   相交   algo   tor   while   

原文地址:https://www.cnblogs.com/nianheng/p/10010090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!