直线相交 模板 大全

重载了很多东西,支持

两对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;
}

转载于:https://www.cnblogs.com/nj-czy/p/5332683.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值