求直线交点,直接上代码。
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct point{ int x,y; point (){} point (int a,int b): x(a),y(b){} friend point operator + (const point &a,const point &b){ return point(a.x+b.x,a.y+b.y); } friend point operator - (const point &a,const point &b){ return point(a.x-b.x,a.y-b.y); } friend point operator * (const int &r,const point &a){ return point(r*a.x,r*a.y); } }; inline int det(point a,point b) {return a.x*b.y-a.y*b.x;} inline int dot(point a,point b) {return a.x*b.x+a.y*b.y;} inline bool On (point p,point s,point t){ return det(p-s,t-s)==0; } inline bool parallel(point a,point b,point c,point d){return det(a-b,c-d)==0;} inline void line_make_point(point s1,point t1,point s2,point t2) //逻辑上必须先判断parallel { double a1=det(s1-s2,t2-s2); double a2=det(t1-s2,t2-s2); printf("POINT %.2f %.2f ",1/(a1-a2)*(a1*t1.x-a2*s1.x),1/(a1-a2)*(a1*t1.y-a2*s1.y)); } int t; point s1,t1,s2,t2,ans; int x,y; int main() { scanf("%d",&t); printf("INTERSECTING LINES OUTPUT "); while (t--) { scanf("%d%d",&x,&y);s1=point(x,y); scanf("%d%d",&x,&y);t1=point(x,y); scanf("%d%d",&x,&y);s2=point(x,y); scanf("%d%d",&x,&y);t2=point(x,y); if (parallel(s1,t1,s2,t2)) { if (On(s1,s2,t2)) printf("LINE "); else printf("NONE "); continue; } else { line_make_point(s1,t1,s2,t2); } } printf("END OF OUTPUT "); return 0; }