# 已知空间两点组成的直线求线上某点的Z值

$\frac{X-x0}{m}=\frac{Y-y0}{n}=\frac{Z-z0}{p}$

#include<iostream>

using namespace std;

//三维double矢量
struct Vec3d
{
double x, y, z;

Vec3d()
{
x = 0.0;
y = 0.0;
z = 0.0;
}
Vec3d(double dx, double dy, double dz)
{
x = dx;
y = dy;
z = dz;
}
void Set(double dx, double dy, double dz)
{
x = dx;
y = dy;
z = dz;
}
};

bool CalLinePointZ(const Vec3d & v1, const Vec3d & v2, Vec3d & vp)
{
const double eps = 0.0000001;

//方向向量
Vec3d s(v2.x-v1.x, v2.y - v1.y, v2.z - v1.z);

//此时无法求值
if (abs(s.x) == eps && abs(s.y) == eps)
{
return false;
}

double t = 0;
if (abs(s.x) > eps && abs(s.y) == eps)
{
double t = (vp.x - v1.x) / s.x;
}
else if (abs(s.x) == eps && abs(s.y) > eps)
{
double t = (vp.y - v1.y) / s.y;
}
else
{
double tx = (vp.x - v1.x) / s.x;
double ty = (vp.y - v1.y) / s.y;

//说明点不可能在直线上
if (abs(tx - ty) > eps)
{
return false;
}
t = tx;
}

vp.z = t * s.z + v1.z;
return true;
}

int main()
{
Vec3d v1(0.0, 0.0, 3.7);
Vec3d v2(5.0, 5.0, 4.5);

Vec3d vp;
vp.x = 4.6;
vp.y = 4.6;
vp.z = 0.0;

if (CalLinePointZ(v1, v2, vp))
{
cout << "该点的高程：" << vp.z << endl;
}

return 0;
}


posted @ 2019-12-28 16:37  charlee44  阅读(804)  评论(0编辑  收藏  举报