-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbc_from_tri.m
More file actions
36 lines (28 loc) · 818 Bytes
/
bc_from_tri.m
File metadata and controls
36 lines (28 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function bc = bc_from_tri(tri,x)
% get the barycentric coordinates of a given point
% tri - the three points, which are represented by column vectors
% x - the given point
% bc - (output) the barycentric coordinates
if size(tri, 1) ~= size(x, 1)
error('different dimensions!');
end
if size(tri, 1) ~= 2 && size(tri, 1) ~= 3
error('The dimension must be 2 or 3!');
end
if size(tri, 1) == 2
tri(end+1,:) = 1;
end
if size(x, 1) == 2
x(end+1,:) = 1;
end
BA = tri(:, 2) - tri(:, 1);
CA = tri(:, 3) - tri(:, 1);
N = cross(BA, CA);
SNN = sum(N.^2);
NA = cross(tri(:, 3) - tri(:, 2), x - tri(:, 2));
NB = cross(tri(:, 1) - tri(:, 3), x - tri(:, 3));
% NC = cross(tri(:, 2) - tri(:, 1), x - tri(:, 1));
alpha = N' * NA / SNN;
beta = N' * NB / SNN;
gamma = 1 - alpha - beta;
bc = [alpha; beta; gamma];