Triangular Elements. Barycentric Coordinates.
Barycentric Coordinates
For each point
, in this triangle one can define the barycentric coordinades of this point using the value of the 2D shape functions defined for this triangle
: satisfying
. This way, one can express any point using three coordinates (barycentric)
. In particular
. The barycentric coordinates satisfy
. Moreover
unless the point is outside of the triangle.
Compute Barycentric Coordinates
Given a triangle defined by vertices
, compute the barycentric coordinates of point
. Verify that
. vertices=[1,1; %define the triangle vertex coordinates
vplot=[vertices;vertices(1,:)]; %just to plot a closed triangle, we add the first point at the end
plot(vplot(:,1),vplot(:,2), LineWidth=1.5);
Compute shape functions
Psi1 = @(x,y) c1(1)+c1(2)*x+c1(3)*y;
Psi2 = @(x,y) c2(1)+c2(2)*x+c2(3)*y;
Psi3 = @(x,y) c3(1)+c3(2)*x+c3(3)*y;
% Compute barycentric coordinates
alpha1 = Psi1(p(1),p(2));
alpha2 = Psi2(p(1),p(2));
alpha3 = Psi3(p(1),p(2));
[alpha1,alpha2,alpha3] %results
alpha1+alpha2+alpha3-1 %check error
p-(alpha1*vertices(1,:)+alpha2*vertices(2,:)+alpha3*vertices(3,:))
Exercise 1: Build baryCoord function
Build a function file that returns the barycentric coordinates of a point with respect to a triangle. It must return a flag value isInside with value 1 if the point is inside the triangle and 0 if it is outside.
Use the previous example to check if it works.
Exemple: Correspondence with the Reference Triangle
Usually the reference triangular element,
, is the triangle defined by the vertices We can use barycentric coordinates to make a correspondence between the reference triangle and every other triangle. Let's consider the preivous exemple triangle
Assuming that each vertex has its correspondence
, using the barycentric coordinates we obtain the point correspondences:
. verticesTR=[0,0; %define the reference triangle (TR) vertex coordinates
plot(vplot(:,1),vplot(:,2),LineWidth=1.5);
%compute the corresponding point on the reference triangle
pTR= alpha1*verticesTR(1,:)+alpha2*verticesTR(2,:)+alpha3*verticesTR(3,:)
vplotTR=[verticesTR;verticesTR(1,:)]; %just to plot a closed triangle, we add the first point at the end
plot(vplotTR(:,1),vplotTR(:,2),LineWidth=1.5);
plot(pTR(1,1),pTR(1,2),'s');
linePlot=[p; pTR]
2.0000 2.0000
0.4000 0.2000
plot(linePlot(:,1),linePlot(:,2),'-.',LineWidth=1);
Exercise 1.1: Point correspondence
Consider now the obtained point on the Reference Triangle pTR=(0.4,0.2) and compute the associated point on the Original Triangle. Observe that the barycentric coordinates are the same and the point obtained is (2,2)
Exercise 2: Several Triangles
Let's consider now four triangles adding a central vertex (as it is shown in the figure). We can define these polygons by the coordinates of the vertices and the triangles defined by these vertices.
plotElements(vertex,triang,1); %find this file at *Numerical Factory*
Exercise 2.1: Several Triangles
Use the function build in the previous exercise
to decide if the point
belongs to each triangle Solution= It belongs to triangle 2.
ShapeFunctions2D Pract: Interpolate Temperature in a Triangle Mesh
A triangle mesh is defined by a set of nodes (the vertices coordinates) and a set of elements (the triangles) defined by the indices corresponding to the vertices belonging to each triangle (connectivity matrix).
The usual format is
nodes=[ x1,y1; x2,y2; ......; xN,yN]; a Nx2 matrix for N vertices
elements =[ i1,j1,k1; i2,j2,k2; .....; iM,jM,kM]; a Mx3 matrix for M triangles
Compute the temperature at the point 
Get the meshFiles file from Numerical Factory
eval('meshHole'); %load data from the file mesHole.m: nodes and elements
numElem = size(elem,1) %number of mesh elements
numNodes = size(nodes,1) %number of mesh nodes
plotElements(nodes,elem,0);
assing a simple temperature value for each vertex. For now Temp=numNode this way the first nodes are cooler than the last ones.
% assign temperatute equal to the number of the node
uTemp=1:numNodes; %we assume that node 1 has temp=1, node 2 has temp=2, etc.
Final step: Internal point interpolation
Finally find the triangle where the point p belongs to (using the baryCoord function) and interpolate the temperature value.
The idea of the code can be:
for all the elements
compute the barycentric coordinates
if (isInside >= 1), pick the element number; break; end;
end
Take this element and perform the interpolation using the temperatures of the nodes and barycentric coord (see theory presentation).
Solution: Temp= 104.0420; element= 189; nodes=[109, 112, 92];
% Look for the element the point p belongs to:
for e = 1:numElem % for each element
n1 = elem(e,1); % number of the 1st node of the element
n2 = elem(e,2); % number of the 2nd node of the element
n3 = elem(e,3); % number of the 3th node of the element
v1 = nodes(n1,:); % coordinates of the 1st node
v2 = nodes(n2,:); % coordinates of the 2nd node
v3 = nodes(n3,:); % coordinates of the 3th node
% Obs.- In short we can write:
% vertices = nodes(elem(e,:),:)
[alphas, isInside] = baryCoordTriang(vertices, p);
if (isInside == 1) % Check if p is inside, compute the results and break the FOR loop
elemP = e; %save the present element
nodElemP = [n1,n2,n3]; %save the number nodes
alphasP = alphas; %save the barycentric coordinates
break %exit from the for loop
% use barycentric coordinates to interpolate vertices values
uPointInterp = alphasP(1)*uTemp(n1) + alphasP(2)*uTemp(n2) + alphasP(3)*uTemp(n3);
% in short: uPInterpol = dot(alphasP,u(nodElemP))
fprintf('The point belongs to element %d;\n', elemP);
fprintf('Their node numbers are %d, %d, and %d.\n', nodElemP);
fprintf('The interpolated value is uPInterpol = %6.4f\n',uPointInterp);
(c)Numerical Factory 2025