POJ1269:Intersecting Lines——题解
http://poj.org/problem?id=1269
题目大意:给四个点,求前两个点所构成的直线和后两个点所构成的直线的位置关系(平行,重合,相交),如果是相交,输出交点坐标。
——————————————————————
http://blog.csdn.net/zxy_snow/article/details/6341282 这个人的博客有详细的证明过程,这里直接拿来用即可。
应当优先判断向量平行,这样再判断是否重合能更方便些。
#include<cstdio> #include<queue> #include<cctype> #include<cstring> #include<vector> #include<algorithm> using namespace std; typedef double dl; inline int read(){ int X=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=getchar();} while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } struct point{//既是向量又是点 dl x; dl y; }q[5]; inline point getmag(point a,point b){ point s; s.x=b.x-a.x;s.y=b.y-a.y; return s; } inline int multiX(point a,point b){ return a.x*b.y-b.x*a.y; } inline bool parallel_mag(point a,point b){ if(a.x*b.y==a.y*b.x)return 1; return 0; } inline void intersection(point a,point b,point c,point d){ dl a1=a.y-b.y,b1=b.x-a.x,c1=a.x*b.y-b.x*a.y; dl a2=c.y-d.y,b2=d.x-c.x,c2=c.x*d.y-d.x*c.y; dl x=(c1*b2-c2*b1)/(a2*b1-a1*b2); dl y=(a2*c1-a1*c2)/(a1*b2-a2*b1); printf("POINT %.2f %.2f\n",x,y); return; } int main(){ puts("INTERSECTING LINES OUTPUT"); int t=read(); while(t--){ for(int i=1;i<=4;i++){ q[i].x=read(); q[i].y=read(); } if(parallel_mag(getmag(q[1],q[2]),getmag(q[3],q[4]))){ if(!multiX(getmag(q[1],q[2]),getmag(q[1],q[3]))){ puts("LINE"); }else puts("NONE"); }else intersection(q[1],q[2],q[3],q[4]); } puts("END OF OUTPUT"); return 0; }