主要是利用向量的叉积,求交点的时候就是解二元一次方程组,没什么难度,采用斜率法算的因为精度损失过大,可能导致wrong
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define eps 1e-8
using namespace std;
struct Point
{
double x,y;
Point():x(0),y(0){}
Point ( double a , double b )
: x(a),y(b){}
};
struct Line
{
Point u,v;
};
int cross ( Point a , Point b , Point c , Point d )
{
return (a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x);
}
int meet ( Line l1 , Line l2 )
{
if ( fabs (cross ( l1.u , l1.v , l2.u , l2.v )) > eps )
return 1;
if ( fabs ( cross( l1.u , l2.u , l2.u , l2.v )) < eps )
return 2;
return 0;
}
double area ( Point a , Point b , Point c )
{
return fabs ( cross ( a , b , a , c ) );
}
/*Point intersect ( Line l1 , Line l2 )
{
double k = area ( l1.u , l1.v , l2.u ) / area ( l1.u, l1.v ,l2.v );
return Point ((l2.u.x+k*l2.v.x)/(1+k) , (l2.u.y+k*l2.v.y)/(1+k));
}*/
Point intersect ( Line l1 , Line l2 )
{
double a1 = l1.u.y - l1.v.y;
double b1 = l1.v.x - l1.u.x;
double c1 = l1.u.x*l1.v.y-l1.v.x*l1.u.y;
double a2 = l2.u.y - l2.v.y;
double b2 = l2.v.x - l2.u.x;
double c2 = l2.u.x*l2.v.y - l2.v.x*l2.u.y;
double x = ( b1*c2 - b2*c1 ) / (a1*b2 - a2*b1 );
double y = ( a2*c1 - a1*c2 ) / (a1*b2 - a2*b1 );
return Point ( x , y );
}
int main ( )
{
int n;
while ( ~scanf ( "%d" , &n ) )
{
Line l[3];
puts ( "INTERSECTING LINES OUTPUT" );
for ( int i = 1 ; i <= n ; i++ )
{
for ( int i =1 ; i <=2 ; i++ )
scanf ( "%lf%lf%lf%lf" , &l[i].u.x , &l[i].u.y ,
&l[i].v.x , &l[i].v.y);
int temp = meet ( l[1] , l[2] );
if ( temp == 1 )
{
Point p = intersect ( l[1] , l[2] );
printf ( "POINT %.2f %.2f\n" , p.x , p.y );
}
if ( temp == 2 )
puts ( "LINE" );
if ( temp == 0 )
puts ( "NONE" );
}
puts ( "END OF OUTPUT" );
}
}