# 如何在3d中获得点到平面的距离？

##### 问题描述投票：-1回答：3

3d geometry point projection plane
##### 3个回答
3

Distance

`````` dist = Abs(a*x1+b*y1+c*z1+d) / Sqrt(a^2+b^2+c^2)
``````

1

``````var a = pos1.y * (pos2.z - pos3.z) + pos2.y * (pos3.z - pos1.z) + pos3.y * (pos1.z - pos2.z);
var b = pos1.z * (pos2.x - pos3.x) + pos2.z * (pos3.x - pos1.x) + pos3.z * (pos1.x - pos2.x);
var c = pos1.x * (pos2.y - pos3.y) + pos2.x * (pos3.y - pos1.y) + pos3.x * (pos1.y - pos2.y);
var d = -(pos1.x * (pos2.y * pos3.z - pos3.y * pos2.z) +
pos2.x * (pos3.y * pos1.z - pos1.y * pos3.z) +
pos3.x * (pos1.y * pos2.z - pos2.y * pos1.z));

var dist = Math.Abs(a * point.x + b * point.y + c * point.z + d) / Math.Sqrt(a * a + b * b + c * c);
``````

1

1. 定义 让三角形点为`p0,p1,p2`和测试点`p`
2. 飞机正常 首先我们需要获得平面法线，即平面内任意两个非平行和非零向量的简单向量乘法： ```n = cross( p1-p0 , p2-p0 ) ``` 并将其规范化为单位向量（以简化内容）： ```n = n/|n| ```
3. 垂直距离 我们可以利用点积来做到这一点，所以只需要一个从平面上的任何一点进入测试点的矢量，然后用单位法线点... ```dist = |dot ( p-p0 , n )| ``` 最后一个绝对值（在标量距离上）将摆脱结果的符号，告诉你点`p`是否在正常`n`的方向或相反的一个有时这样的信息是想要的所以在这种情况下删除最外面的abs值并使用多边形缠绕和交叉产品操作数，以维持所需的法线方向。

``````U.x=p1.x-p0.x; V.x=p2.x-p0.x; // basis vectors on the plane
U.y=p1.y-p0.y; V.y=p2.y-p0.y;
U.z=p1.z-p0.z; V.z=p2.z-p0.z;
n.x=(U.y*V.z)-(U.z*V.y);      // plane normal
n.y=(U.z*V.x)-(U.x*V.z);
n.z=(U.x*V.y)-(U.y*V.x);
dist = sqrt( (n.x*n.x) + (n.y*n.y) + (n.z*n.z) ); // normalized
n.x /= dist;
n.y /= dist;
n.z /= dist;
dist = abs( (p.x-p0.x)*n.x + (p.y-p0.y)*n.y + (p.z-p0.z)*n.z ); // your perpendicular distance
``````