直线求交,我的方法是叉积为0联立解方程。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define maxn 1050 #define eps 1e-9 using namespace std; struct point { double x,y; point (double x,double y):x(x),y(y) {} point () {} friend point operator -(point x,point y) { return point(x.x-y.x,x.y-y.y); } }p[maxn]; struct line { point x,y,dt; line (point x,point y,point dt):x(x),y(y),dt(dt) {} line () {} friend double operator *(line x,line y) { return x.dt.x*y.dt.y-x.dt.y*y.dt.x; } }l[maxn]; int n; double a,b,c,d; point ask_cross(line x,line y) { double a,b,c,d,e,f; a=x.x.y-x.y.y;b=x.y.x-x.x.x;c=x.x.x*x.y.y-x.x.y*x.y.x; d=y.x.y-y.y.y;e=y.y.x-y.x.x;f=y.x.x*y.y.y-y.x.y*y.y.x; return point((b*f-c*e)/(a*e-b*d),(a*f-c*d)/(b*d-a*e)); } int main() { while (scanf("%d",&n)!=EOF) { printf("INTERSECTING LINES OUTPUT "); for (int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&a,&b,&c,&d); l[1]=line(point(a,b),point(c,d),point(c,d)-point(a,b)); scanf("%lf%lf%lf%lf",&a,&b,&c,&d); l[2]=line(point(a,b),point(c,d),point(c,d)-point(a,b)); if (fabs(l[1]*l[2])<eps) { if (fabs(l[1]*line(l[1].x,l[2].x,l[2].x-l[1].x))<eps) printf("LINE "); else printf("NONE "); } else { point now=ask_cross(l[1],l[2]); printf("POINT %.2f %.2f ",now.x,now.y); } } printf("END OF OUTPUT "); } return 0; }