原题:
求两直线交点
思路借鉴于:http://blog.csdn.net/zxy_snow/article/details/6341282
感谢大佬
#include<cstdio> #include<algorithm> #include<cstring> #define eps 1e-8 using namespace std; int T; struct point//点(向量的结构体) { double x,y; point() {}//初始化 point (int _x,int _y) : x(_x),y(_y) {};//用一对坐标初始化点 inline point operator + (const point &rhs) const//向量加法 { return point(x+rhs.x,y+rhs.y); } inline point operator - (const point &rhs) const//向量减法 { return point(x-rhs.x,y-rhs.y); } inline int operator * (const point &rhs) const//向量叉乘 //向量叉乘的几何意义是以两个向量为邻边的平行四边形的有向面积 也就是|a|*|b|*sin<a,b> 这里的sin<a,b>决定了 //如果a,b是逆时针的,那么sin<a,b>大于0,有向面积大于0,反之<0 { return x*rhs.y-y*rhs.x; } friend inline int dot(const point &lhs,const point &rhs)//向量点乘 { return lhs.x*rhs.x+lhs.y*rhs.y; } }p1,p2,p3,p4; inline bool check(const point &p1,const point &p2,const point &p3) { if (abs((p1-p3)*(p2-p3))<eps) return 1; return 0; } double abs(double x) { return x>0?x:-x; } int main() { scanf("%d",&T); puts("INTERSECTING LINES OUTPUT"); while (T--) { 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); if (check(p1,p2,p3) && check(p1,p2,p4)) { puts("LINE"); continue; } if (abs((p2.x-p1.x)*(p4.y-p3.y)-(p4.x-p3.x)*(p2.y-p1.y))<eps) { puts("NONE"); continue; } double a1=(p1.y-p2.y),b1=(p2.x-p1.x),c1=(p1.x*p2.y-p2.x*p1.y); double a2=(p3.y-p4.y),b2=(p4.x-p3.x),c2=(p3.x*p4.y-p4.x*p3.y); printf("POINT %.2f %.2f ",(c1*b2-c2*b1)/(a2*b1-a1*b2),(a2*c1-a1*c2)/(a1*b2-a2*b1)); } puts("END OF OUTPUT"); return 0; }