# 1. 概述

$P=O+tD$

$t = \frac{d}{Mod(D)}$

# 2. 实现

#include <iostream>

using namespace std;

// 2D Point
struct Vector2d
{
public:
Vector2d()
{
}

Vector2d(double dx, double dy)
{
x = dx;
y = dy;
}

// 矢量赋值
void set(double dx, double dy)
{
x = dx;
y = dy;
}

// 矢量相加
Vector2d operator + (const Vector2d& v) const
{
return Vector2d(x + v.x, y + v.y);
}

// 矢量相减
Vector2d operator - (const Vector2d& v) const
{
return Vector2d(x - v.x, y - v.y);
}

//矢量数乘
Vector2d Scalar(double c) const
{
return Vector2d(c*x, c*y);
}

// 矢量点积
double Dot(const Vector2d& v) const
{
return x * v.x + y * v.y;
}

//向量的模
double Mod() const
{
return sqrt(x * x + y * y);
}

double x, y;
};

void CalPointFromLineWithDistance(const Vector2d & O, const Vector2d & E, double d, Vector2d& P)
{
Vector2d D = E - O;
double t = d / D.Mod();
P = O + D.Scalar(t);
}

int main()
{
Vector2d O(1.0, 2.4);
Vector2d E(10.2, 11.5);
double d = 5;
Vector2d P;

CalPointFromLineWithDistance(O, E, d, P);
cout << "计算的点为：" << P.x<<'\t' << P.y << '\n';

cout << "验算距离是否为"<<d<<"：" <<(P-O).Mod()<< '\n';
}


posted @ 2020-03-04 20:51  charlee44  阅读(252)  评论(0编辑  收藏