您的位置:首页 > 其它

POJ 1269 || Intersecting Lines(直线重合,平行,相交判定

2015-03-26 20:10 435 查看

poj 不能用%lf输出,被坑了五六七八WA,从70多行的代码改到一百多行,发现卧槽原来是这个wa点,无法吐槽自己。

没用模板,自己从头文件开始狂敲。

等下去学一个模板好像非常简单粗暴。

两条直线的X乘等于0说明重合或者平行,再其中一条直线的点是否再另外一条直线上判断是否是重合就可以区分了。

两条直线的交点用向量偏移一下就好了。

PS:没有考虑线段退化成一点的情况。


感觉思维越想越搓,代码越改越长。来个简单粗暴的模板传送门:http://blog.csdn.net/accelerator_/article/details/44080923

0ms AC代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const double epx = 1e-8;
int dcmp( double x )
{
if( fabs(x) < epx )
return 0;
return x < 0? -1: 1;
}
struct pnode
{
double x,y;
pnode(){}
pnode( double a,double b):x(a),y(b){}
pnode operator + (const pnode &b)const{ return pnode( x+b.x,y+b.y );}
pnode operator - ( const pnode &b)const
{
return pnode( x - b.x,y - b.y );
}
double operator ^( const pnode &b)const
{
return x * b.y - y * b.x;
}
double operator * ( const pnode &b)const
{
return x * b.x + y * b.y;
}
pnode operator * (const double p)const
{
return pnode(x*p,y*p);
}
pnode operator /( const double p)const
{
return pnode(x / p,y/p);
}
}p;

typedef pnode vec;
struct pline
{
pnode st,ed;
pline(){}
pline( pnode a,pnode b):st(a),ed(b){}
void readl(){ scanf("%lf %lf %lf %lf",&st.x,&st.y,&ed.x,&ed.y);}
};
pline l1,l2;

double cross(pnode p0,pnode p1,pnode p2)
{
return (p1-p0)^(p2-p0);
}
double cross( pline a,pline b)
{
return (a.ed-a.st)^( b.ed - b.st);
}
double cross( pnode a,pnode b)
{
return a.x*b.y - a.y *b.x;
}

double dot( pnode p0,pnode p1,pnode p2)
{
return (p1-p0)*(p2-p0);
}
pnode inter_point(pline a,pline b)
{
vec u = a.st  - b.st;
double t = cross(b.ed -b.st,u) / cross(a,b);
return a.st + (a.ed - a.st)*t;
}
double dist(pline p)
{
return sqrt( dot( p.st,p.ed,p.ed));
}
double dist_to_line( pnode a,pline l)
{
pline v ( l.st,a);
return fabs( cross(v,l) ) /dist(l) ;
}
int  work()
{
if( dcmp( cross( l1,l2 ) )== 0)//line none
{
if( dist_to_line(l1.st,l2) < epx  )
return 0;
else
return 1;
}
else//point
{
p = inter_point(l1,l2);
return -1;
}

}
int main()
{
int cas;
scanf("%d",&cas);
puts("INTERSECTING LINES OUTPUT");
while(cas--)
{
l1.readl();
l2.readl();
//printf("%lf\n",cross(l1,l2));
int ans = work();
if( ans == 1)
puts("NONE");
else
if( ans == 0)
puts("LINE");
else
printf("POINT %.2f %.2f\n",p.x,p.y);

}
puts("END OF OUTPUT");
//system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: