模板题
注意原题中说的线段其实要当成没有端点的直线。被坑了= =
#include <cmath>
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std; #define eps 1e-8
#define PI acos(-1.0)//3.14159265358979323846
//判断一个数是否为0,是则返回true,否则返回false
#define zero(x)(((x)>0?(x):-(x))<eps)
//返回一个数的符号,正数返回1,负数返回2,否则返回0
#define _sign(x)((x)>eps?1:((x)<-eps?2:0)) struct point
{
double x,y;
point(){}
point(double xx,double yy):x(xx),y(yy)
{}
};
struct line
{
point a,b;
line(){} //默认构造函数
line(point ax,point bx):a(ax),b(bx)
{}
};//直线通过的两个点,而不是一般式的三个系数 int n;
double ax1,ay1,ax2,ay2,bx1,by1,bx2,by2; //求矢量[p0,p1],[p0,p2]的叉积
//p0是顶点
//若结果等于0,则这三点共线
//若结果大于0,则p0p2在p0p1的逆时针方向
//若结果小于0,则p0p2在p0p1的顺时针方向
double xmult(point p1,point p2,point p0)
{
return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
//计算dotproduct(P1-P0).(P2-P0)
double dmult(point p1,point p2,point p0)
{
return(p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}
//两点距离
double distance(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
//判三点共线
int dots_inline(point p1,point p2,point p3)
{
return zero(xmult(p1,p2,p3));
}
//判点是否在线段上,包括端点
int dot_online_in(point p,line l)
{
return zero(xmult(p,l.a,l.b))&&(l.a.x-p.x)*(l.b.x-p.x)<eps&&(l.a.y-p.y)*(l.b.y-p.y)<eps;
}
//判点是否在线段上,不包括端点
int dot_online_ex(point p,line l)
{
return dot_online_in(p,l)&&(!zero(p.x-l.a.x)||!zero(p.y-l.a.y))&&(!zero(p.x-l.b.x)||!zero(p.y-l.b.y));
}
//判两点在线段同侧,点在线段上返回0
int same_side(point p1,point p2,line l)
{
return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)>eps;
}
//判两点在线段异侧,点在线段上返回0
int opposite_side(point p1,point p2,line l)
{
return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)<-eps;
}
//判两直线平行
int parallel(line u,line v)
{
return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y));
}
//判两直线垂直
int perpendicular(line u,line v)
{
return zero((u.a.x-u.b.x)*(v.a.x-v.b.x)+(u.a.y-u.b.y)*(v.a.y-v.b.y));
}
//判两线段相交,包括端点和部分重合
int intersect_in(line u,line v)
{
if(!dots_inline(u.a,u.b,v.a)||!dots_inline(u.a,u.b,v.b))
return!same_side(u.a,u.b,v)&&!same_side(v.a,v.b,u);
return dot_online_in(u.a,v)||dot_online_in(u.b,v)||dot_online_in(v.a,u)||dot_online_in(v.b,u);
}
//判两线段相交,不包括端点和部分重合
int intersect_ex(line u,line v)
{
return opposite_side(u.a,u.b,v)&&opposite_side(v.a,v.b,u);
}
//计算两直线交点,注意事先判断直线是否平行!
//线段交点请另外判线段相交(同时还是要判断是否平行!)
point intersection(line u,line v)
{
point ret=u.a;
double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
ret.x+=(u.b.x-u.a.x)*t;
ret.y+=(u.b.y-u.a.y)*t;
return ret;
} int main()
{
cin>>n;
cout<<"INTERSECTING LINES OUTPUT"<<endl;
for (int i=;i<=n;i++)
{
cin>>ax1>>ay1>>ax2>>ay2>>bx1>>by1>>bx2>>by2;
point a1(ax1,ay1); point a2(ax2,ay2);
point b1(bx1,by1); point b2(bx2,by2);
line l1=line(point(ax1,ay1),point(ax2,ay2));
line l2=line(point(bx1,by1),point(bx2,by2));
if ((dots_inline(a1,a2,b1)>)&&(dots_inline(a1,a2,b2)>))
cout<<"LINE";
else if (parallel(l1,l2)>) cout<<"NONE";
else
{
point tm=intersection(l1,l2);
cout<<"POINT ";
printf("%.2f %.2f",tm.x,tm.y);
}
cout<<endl;
}
cout<<"END OF OUTPUT"<<endl;
return ;
}