求相交点
/*
线段相交模板:判相交、求交点
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h> const double eps = 1e-;
struct Point{
double x,y;
};
Point P_ans;
double cross( Point a,Point b,Point c ){
return ( b.x-a.x )*( c.y-a.y )-( b.y-a.y )*( c.x-a.x );
}
int solve( Point a,Point b,Point c,Point d ){
if( fabs(cross(a,b,c))<=eps&&fabs(cross(a,b,d))<=eps )
return -;//两条线段在同一条直线上
if( fabs((b.x-a.x)*(d.y-c.y)-(b.y-a.y)*(d.x-c.x))<=eps )
return ;//两条线断平行
/*
求交点用到叉积(必须保证有交点)
交点为p0(x,y)
(A-p0)*(B-p0)=0
(C-p0)*(D-p0)=0
*/
double a1,a2,b1,b2,c1,c2;
a1 = a.y-b.y,b1 = b.x-a.x,c1 = a.x*b.y-b.x*a.y;
a2 = c.y-d.y,b2 = d.x-c.x,c2 = c.x*d.y-d.x*c.y;
P_ans.x = (b1*c2-b2*c1)/(a1*b2-a2*b1);
P_ans.y = (a2*c1-a1*c2)/(a1*b2-a2*b1);
return ;
}
int main(){
int n;
Point p1,p2,p3,p4;
scanf("%d",&n);
printf("INTERSECTING LINES OUTPUT\n");
while( n-- ){
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);
int f = solve( p1,p2,p3,p4 );
if( f==- ) puts("LINE");
else if( f== ) puts("NONE");
else{
printf("POINT %.2lf %.2lf\n",P_ans.x,P_ans.y);
}
}
printf("END OF OUTPUT\n");
return ;
}