直线相交 模板 大全
重载了很多东西,支持
两对a,b,c;
两个点加一对a,b,c;
两对点x,y;
还有getline的助攻
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
const double eps=1e-10;
double add(double a,double b) //有误差的浮点加法
{
if(abs(a + b) < eps * (abs(a) + abs(b))) return 0;
return a + b;
}
struct Point
{
double x,y;
Point() {}
Point(double _x,double _y):x(_x),y(_y) {}
Point operator + (Point p)
{
return Point(add(x,p.x),add(y,p.y));
}
void input()
{
scanf("%lf%lf",&x,&y);
}
};
struct Line
{
Point p,v;
Line() {}
Line(Point _p,Point _v):p(_p),v(_v) {}
};
void getline(Point x,Point y,double &a,double &b,double &c) //两点x、y确定一条直线a、b、c为其系数
{
a = y.y - x.y;
b = x.x - y.x;
c = y.x * x.y - x.x * y.y;
}
Point intersect(Point x,Point y,double a,double b,double c) //求x、y形成的直线与已知直线a、b、c、的交点
{
double u = fabs(a * x.x + b * x.y + c);
double v = fabs(a * y.x + b * y.y + c);
Point pt;
pt.x=(x.x * v + y.x * u) / (u + v);
pt.y=(x.y * v + y.y * u) / (u + v);
return pt;
}
Point intersect(double a,double b,double c,double a1,double b1,double c1) //求直线a、b、c、与已知直线a1、b1、c1、的交点
{
double v1=b*c1-b1*c;
double v2=a1*c-a*c1;
double u=a*b1-a1*b;
Point pt;
pt.x=v1/u;
pt.y=v2/u;
return pt;
}
Point intersect(Point x,Point y,Point x1,Point y1) //求x、y形成的直线与已知直线a、b、c、的交点
{
double ta = y1.y - x1.y;
double tb = x1.x - y1.x;
double tc = y1.x * x1.y - x1.x * y1.y;
double u = fabs(ta * x.x + tb * x.y + tc);
double v = fabs(ta * y.x + tb * y.y + tc);
Point pt;
pt.x=(x.x * v + y.x * u) / (u + v);
pt.y=(x.y * v + y.y * u) / (u + v);
return pt;
}
int main()
{
Line l;
Point s,e;
scanf ("%lf%lf",&s.x,&s.y);
scanf ("%lf%lf",&e.x,&e.y);
scanf ("%lf%lf",&l.p.x,&l.p.y);
scanf ("%lf%lf",&l.v.x,&l.v.y);
printf ("%f,%f\n",intersect(l.p,l.p+l.v,s,e).x,intersect(l.p,l.p+l.v,s,e).y);
return 0;
}