用的是初中学的方法
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define eps 1e-8 using namespace std; struct Point { double x,y; Point() {}; Point(double xx,double yy) { x=xx; y=yy; } }ans; int getCross(Point p1,Point p2,Point q1,Point q2) { double k1=(p1.y-p2.y)/(p1.x-p2.x); double k2=(q1.y-q2.y)/(q1.x-q2.x); double b1=p1.y-k1*p1.x; double b2=q1.y-k2*q1.x; if(p1.x==p2.x && q1.x==q2.x) return p1.x==q1.x? 1:-1; if(p1.x==p2.x) { ans=Point(p1.x,k2*p1.x+b2); return 0; } if(q1.x==q2.x) { ans=Point(q1.x,k1*q1.x+b1); return 0; } if(fabs(k1-k2)<eps) return fabs(b1-b2)<eps? 1:-1; ans.x=(b2-b1)/(k1-k2); ans.y=k1*ans.x+b1; return 0; } int main() { // freopen("in.txt","r",stdin); int t; scanf("%d",&t); Point pot[4]; puts("INTERSECTING LINES OUTPUT"); while(t--) { double x,y; for(int i=0; i<4; i++) { scanf("%lf%lf",&x,&y); pot[i]=Point(x,y); } int tmp=getCross(pot[0],pot[1],pot[2],pot[3]); if(tmp==-1) puts("NONE"); else if(tmp==1) puts("LINE"); else printf("POINT %.2f %.2f ",ans.x,ans.y); } puts("END OF OUTPUT"); return 0; }