算法1

由向量垂直关系（`公式1`）：         点N在直线AB上，根据向量共线（`公式2`）：        由`公式2`得（`公式3`）：

`公式3`式代入`公式1`式，式中只有一个未知数k，整理化简解出k（公式4）：

`公式4`式代入`公式3`式即得到垂足N的坐标。

``````// 二维空间点到直线的垂足
struct Point
{
double x,y;
}
Point GetFootOfPerpendicular(
const Point &pt,     // 直线外一点
const Point &begin,  // 直线开始点
const Point &end)   // 直线结束点
{
Point retVal;

double dx = begin.x - end.x;
double dy = begin.y - end.y;
if(abs(dx) < 0.00000001 && abs(dy) < 0.00000001 )
{
retVal = begin;
return retVal;
}

double u = (pt.x - begin.x)*(begin.x - end.x) +
(pt.y - begin.y)*(begin.y - end.y);
u = u/((dx*dx)+(dy*dy));

retVal.x = begin.x + u*dx;
retVal.y = begin.y + u*dy;

return retVal;
}
``````

``````// 三维空间点到直线的垂足
struct Point
{
double x,y,z;
}
Point GetFootOfPerpendicular(
const Point &pt,     // 直线外一点
const Point &begin,  // 直线开始点
const Point &end)   // 直线结束点
{
Point retVal;

double dx = begin.x - end.x;
double dy = begin.y - end.y;
double dz = begin.z - end.z;
if(abs(dx) < 0.00000001 && abs(dy) < 0.00000001 && abs(dz) < 0.00000001 )
{
retVal = begin;
return retVal;
}

double u = (pt.x - begin.x)*(begin.x - end.x) +
(pt.y - begin.y)*(begin.y - end.y) + (pt.z - begin.z)*(begin.z - end.z);
u = u/((dx*dx)+(dy*dy)+(dz*dz));

retVal.x = begin.x + u*dx;
retVal.y = begin.y + u*dy;
retVal.y = begin.z + u*dz;

return retVal;
}
``````
算法2

https://stackoverflow.com/questions/9368436/3d-perpendicular-point-on-line-from-3d-point

``````Vector3 p1 = new Vector3(x1, y1, z1);
Vector3 p2 = new Vector3(x2, y2, z2);
Vector3 q = new Vector3(x3, y3, z3);

Vector3 u = p2 - p1;
Vector3 pq = q - p1;
Vector3 w2 = pq - Vector3.Multiply(u, Vector3.Dot(pq, u) / u.LengthSquared);

Vector3 f = q - w2;
``````
参考

Perpendicular on a line segment from a given point
https://stackoverflow.com/questions/10301001/perpendicular-on-a-line-segment-from-a-given-point

How do you find a point at a given perpendicular distance from a line?
https://stackoverflow.com/questions/133897/how-do-you-find-a-point-at-a-given-perpendicular-distance-from-a-line