答案 0 :(得分:2)
假设在C#中存在一个Vector类型库,其中点积定义为
double Dot(Vector3 a, Vector3 b) => a.X * b.X + a.Y*b.Y + a.Z*b.Z;
和LengthSquared定义为
double LengthSquared ()=> Dot(this,this);
将点投影到超矩形的每个独立轴上以找到 投影的标量参数。然后使标量参数饱和 面部的限制。然后对组件求和以获得答案
public Vector3 ClosestPointTo
(Vector3 q, Vector3 origin, Vector3 v100, Vector3 v010, Vector3 v001)
{
var px = v100;
var py = v010;
var pz = v001;
var vx = (px - origin);
var vy = (py - origin);
var vz = (pz - origin);
var tx = Vector3.Dot( q - origin, vx ) / vx.LengthSquared();
var ty = Vector3.Dot( q - origin, vy ) / vy.LengthSquared();
var tz = Vector3.Dot( q - origin, vz ) / vz.LengthSquared();
tx = tx < 0 ? 0 : tx > 1 ? 1 : tx;
ty = ty < 0 ? 0 : ty > 1 ? 1 : ty;
tz = tz < 0 ? 0 : tz > 1 ? 1 : tz;
var p = tx * vx + ty * vy + tz * vz + origin;
return p;
}