判断直线是否相交以及求交点
存一个求直线交点的模板
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mod 1000000000+7
const double EPS=1e-10;
using namespace std;
double add(double a,double b)
{
if(abs(a+b)<EPS*(abs(a)+abs(b)))
return 0;
return a+b;
}
struct P
{
double x,y;
P (){}
P(double x,double y):x(x),y(y){
}
P operator + (P p){
return P(add(x,p.x),add(y,p.y));
}
P operator -(P p){
return P(add(x,-p.x),add(y,-p.y));
}
P operator *(double d){
return P(x*d,y*d);
}
double det(P p){
return add(x*p.y,-y*p.x);
}
double dot(P p){
return add(x*p.x,y*p.y);
}
};
P in(P p1,P p2,P q1,P q2){
return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1) );
}
bool equ(P a,P b){
if(fabs(a.x-b.x)<=EPS && fabs(a.y-b.y)<=EPS)
return 1;
return 0;
}
bool on_line(P p1,P p2,P p){
return ((p1-p).det(p2-p)==0);
}
P p[2],q[2];
int main()
{
int t;
scanf("%d",&t);
printf("INTERSECTING LINES OUTPUT\n");
while(t--)
{
scanf("%lf%lf%lf%lf",&p[0].x,&p[0].y,&q[0].x,&q[0].y);
scanf("%lf%lf%lf%lf",&p[1].x,&p[1].y,&q[1].x,&q[1].y);
if(fabs((p[0]-q[0]).det(p[1]-q[1]))<=EPS) //共线
{
if(on_line(p[1],q[1],p[0]))//是否为同直线
printf("LINE\n");
else printf("NONE\n");
continue;
}
P r;
r=in(p[0],q[0],p[1],q[1]); //计算交点
printf("POINT %.2lf %.2lf\n",r.x,r.y);
}
printf("END OF OUTPUT\n");
return 0;
}