zoj 1280 Intersecting Lines(两直线交点)

题意:n组数据,每组两条直线两端点坐标,判断线段平行、重合,相交;

思路:利用叉积跨立实验判断重合与平行,交点公式求交点;zoj过了,可是poj1269一样的题,poj上把%lf都改为%.f才能AC。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double epsi=1e-10;
inline int sign(const double &x){
    if(x>epsi) return 1;
    if(x<-epsi) return -1;
    return 0;
}
struct point{
    double x,y;
    point(double xx=0,double yy=0):x(xx),y(yy){}
    point operator -(const point &op2) const{
        return point(x-op2.x,y-op2.y);
    }
    double operator ^(const point &op2) const{
        return x*op2.y-y*op2.x;
    }
};
inline double sqr(const double &x){
    return x*x;
}
inline double mul(const point &p0,const point &p1,const point &p2){
    return (p1-p0)^(p2-p0);
}
inline double dis2(const point &p0,const point &p1){
    return sqr(p0.x-p1.x)+sqr(p0.y-p1.y);
}
inline double dis(const point &p0,const point &p1){
    return sqrt(dis2(p0,p1));
}
inline int cross(const point &p1,const point &p2,const point &p3,const point &p4,point &p){
    double a1=mul(p1,p2,p3),a2=mul(p1,p2,p4);
    if(sign(a1)==0&&sign(a2)==0) return 2;//重叠
    if(sign(a1-a2)==0) return 0;
    p.x=(a2*p3.x-a1*p4.x)/(a2-a1);
    p.y=(a2*p3.y-a1*p4.y)/(a2-a1);    //利用公式计算交点
    return 1;
}
point p1,p2,p3,p4,p;
int main()
{
   int test=0;
   scanf("%d",&test);
   printf("INTERSECTING LINES OUTPUT\n");
   while(test--){
     scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y,&p3.x,&p3.y,&p4.x,&p4.y);
     int m=cross(p1,p2,p3,p4,p);
     if(m==0) printf("NONE\n");
     else if(m==2) printf("LINE\n");
     else  printf("POINT %.2lf %.2lf\n",p.x,p.y);
   }
   printf("END OF OUTPUT\n");
   return 0;
}

 

posted on 2015-06-03 18:11  大树置林  阅读(212)  评论(0编辑  收藏  举报

导航