判直线平行,重合,直线交点——poj1269

#include <cstdio>
#include <cmath>
#define db double
using namespace std;
const db eps=1e-6;
const db pi = acos(-1);
int sign(db k){
    if (k>eps) return 1; else if (k<-eps) return -1; return 0;
}
int cmp(db k1,db k2){return sign(k1-k2);}
struct point{
    db x,y;
    point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
    point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
    point operator * (db k1) const{return (point){x*k1,y*k1};}
    point operator / (db k1) const{return (point){x/k1,y/k1};}
};
db dot(point k1,point k2){
    return k1.x*k2.x+k1.y*k2.y;
}
db cross(point k1,point k2){
    return k1.x*k2.y-k1.y*k2.x;
}
int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}
int inmid(point k1,point k2,point k3){//k3在[k1,k2]
    return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y);
}
point getLL (point k1,point k2,point k3,point k4){//两直线交点
    db w1=cross(k1-k3,k4-k3),w2=cross(k4-k3,k2-k3);
    return (k1*w2+k2*w1)/(w1+w2);
}
bool onS(point k1,point k2,point q){//q在[k1,k2]
    return inmid(k1,k2,q)&&sign(cross(k1-q,k2-k1))==0;
}
int checkLL(point k1,point k2,point k3,point k4){//求两条直线是否 (平行||重合)
    return cmp(cross(k3-k1,k4-k1),cross(k3-k2,k4-k2))==0;
}
struct Line{
    point p[2];
};

db xl,yl,x2,y2;
int n;
point p[5];
int main(){
    scanf("%d",&n);
    puts("INTERSECTING LINES OUTPUT");
    while (n--){
        for(int i=1;i<=4;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        
        if(checkLL(p[1],p[2],p[3],p[4])){
            if(sign(cross(p[1]-p[3],p[2]-p[3]))==0){
                printf("LINE\n");
            } else{
                printf("NONE\n");
            }
        } else{
            point tmp = getLL(p[1],p[2],p[3],p[4]);
               printf("POINT %.2lf %.2lf\n",tmp.x,tmp.y);
        }
    }
    printf("END OF OUTPUT\n");
}

 

posted on 2020-02-16 21:52  zsben  阅读(171)  评论(0编辑  收藏  举报

导航