Update - Thanks everyone for your responses! After fixing a problem with vector normalization, the code below now works.
I'm a new user, and I was attempting to port some Mathematica code from MATLAB for calculating the minimum distance between a point in 3-space and a triangle in 3-space. This is partly a result of me wanting to have this functionality in Mathematica, and, I suppose, a good opportunity to practice learning the Mathematica syntax.
The original MATLAB code isn't mine (and can be found here), but appears to work well in MATLAB. Unfortunately, my translated version of the script in Mathematica returns nonsense despite my almost verbatim copy of the script.
I hope this question isn't inappropriate, but can anyone spot any naïve errors during the translation? Note that I had to change D
to Dnp
to avoid symbol protection issues.
P = {0.5, -0.3, 0.5};
P1 = {0, -1, 0};
P2 = {1, 0, 0};
P3 = {0, 0, 0};
vertices = {P1, P2, P3};
B = P1;
E0 = P2 - B;
E1 = P3 - B;
Dnp = B - P;
a = Dot[E0, E0];
b = Dot[E0, E1];
c = Dot[E1, E1];
d = Dot[E0, Dnp];
e = Dot[E1, Dnp];
f = Dot[Dnp, Dnp];
det = a*c - b*b;
s = b*e - c*d;
t = b*d - a*e;
If[(s + t) <= det,
If[s < 0,
If[t < 0,
If[(d < 0),
t = 0;
If[(-d >= a),
s = 1;
sqrDistance = a + 2*d + f;
,
s = -d/a;
sqrDistance = d*s + f;
];
,
s = 0;
If[(e >= 0),
t = 0;
sqrDistance = f;
,
If[(-e >= c),
t = 1;
sqrDistance = c + 2*e + f;
,
t = -e/c;
sqrDistance = e*t + f;
];
];
];
,
s = 0;
If[e >= 0,
t = 0;
sqrDistance = f;
,
If[-e >= c,
t = 1;
sqrDistance = c + 2*e + f;
,
t = -e/c;
sqrDistance = e*t + f;
];
];
];
,
If[t < 0,
t = 0;
If[d >= 0,
s = 0;
sqrDistance = f;
,
If[-d >= a,
s = 1;
sqrDistance = a + 2*d + f;
,
s = -d/a;
sqrDistance = d*s + f;
];
];
,
invDet = 1/det;
s = s*invDet;
t = t*invDet;
sqrDistance = s*(a*s + b*t + 2*d) + t*(b*s + c*t + 2*e) + f;
];
];
,
If[s < 0,
tmp0 = b + d;
tmp1 = c + e;
If[tmp1 > tmp0,
numer = tmp1 - tmp0;
denom = a - 2*b + c;
If[numer >= denom,
s = 1;
t = 0;
sqrDistance = a + 2*d + f;
,
s = numer/denom;
t = 1 - s;
sqrDistance = s*(a*s + b*t + 2*d) + t*(b*s + c*t + 2*e) + f;
];
,
s = 0;
If[tmp1 <= 0,
t = 1;
sqrDistance = c + 2*e + f;
,
If[e >= 0,
t = 0;
sqrDistance = f;
,
t = -e/c;
sqrDistance = e*t + f;
];
];
];
,
If[t < 0,
tmp0 = b + e;
tmp1 = a + d;
If[(tmp1 > tmp0),
numer = tmp1 - tmp0;
denom = a - 2*b + c;
If[(numer >= denom),
t = 1;
s = 0;
sqrDistance = c + 2*e + f;
,
t = numer/denom;
s = 1 - t;
sqrDistance = s*(a*s + b*t + 2*d) + t*(b*s + c*t + 2*e) + f;
];
,
t = 0;
If[(tmp1 <= 0),
s = 1;
sqrDistance = a + 2*d + f;
,
If[(d >= 0),
s = 0;
sqrDistance = f;
,
s = -d/a;
sqrDistance = d*s + f;
];
];
];
];
,
numer = c + e - b - d;
If[numer <= 0,
s = 0;
t = 1;
sqrDistance = c + 2*e + f;
,
denom = a - 2*b + c;
If[numer >= denom,
s = 1;
t = 0;
sqrDistance = a + 2*d + f;
,
s = numer/denom;
t = 1 - s;
sqrDistance = s*(a*s + b*t + 2*d) + t*(b*s + c*t + 2*e) + f;
];
];
];
];
If[(sqrDistance < 0), sqrDistance = 0;];
dist = Sqrt[(sqrDistance)]
Graphics`Mesh`PointPolygonDistance[]
that you might be able to use... $\endgroup$