Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11708 | Accepted: 5265 |
Description
Input
Output
Sample Input
5 0 0 4 4 0 4 4 0 5 0 7 6 1 0 2 3 5 0 7 6 3 -6 4 -3 2 0 2 27 1 5 18 5 0 3 4 0 1 2 2 5
Sample Output
INTERSECTING LINES OUTPUT POINT 2.00 2.00 NONE LINE POINT 2.00 5.00 POINT 1.07 2.20 END OF OUTPUT
求交点参考大白书上面的方法,判平行用叉积,判重合加一条辅助线再用一次叉积,例如直线AB和CD,在平行的基础上,只要A在CD上就是重合,也就是AC和CD平行。
代码:
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #define type double using namespace std; const double eps=1e-8; int sgn(double x){ if(fabs(x)<eps) return 0; if(x<0) return -1; return 1; } struct vec{ //向量 type x,y; vec(type xx=0,type yy=0):x(xx),y(yy){} bool operator==(vec a){ return !sgn(x-a.x)&&!sgn(y-a.y); } vec operator*(type k){ return vec(k*x,k*y); } }; struct point{ //点 type x,y; point(type xx=0,type yy=0):x(xx),y(yy){} vec operator-(point a){ return vec(x-a.x,y-a.y); } point operator+(vec a){ return point(x+a.x,y+a.y); } bool operator==(point a){ return !sgn(x-a.x)&&!sgn(y-a.y); } }; type crossp(vec a,vec b){ //叉积a×b return a.x*b.y-b.x*a.y; } //直线ab和直线cd求交点 //调用前保证ab和cd有交点,即sgn(crossp(v,w))!=0 point line_ins(point a,point b,point c,point d){ vec u=a-c,v=b-a,w=d-c; double t=crossp(w,u)/crossp(v,w); return a+v*t; } int main() { int n; point a,b,c,d; cin>>n; puts("INTERSECTING LINES OUTPUT"); for(int i=0;i<n;i++){ cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y; if(sgn(crossp(b-a,d-c))){ point ans=line_ins(a,b,c,d); printf("POINT %.2f %.2f\n",ans.x,ans.y); } else if(sgn(crossp(a-c,d-c))){ //ca和cd是否平行,也就是否是同一直线 puts("NONE"); } else puts("LINE"); } puts("END OF OUTPUT"); return 0; }