MATLAB: How to compute the area of a 2D triangle specified using an object of TriRep class in MATLAB 7.8 (R2009a)

2dareaMATLABtriangletrirep

I have an object of class TriRep which consists of 2 triangles:
X=[0; 0; 1; 1]; % vertex coordinates X
Y=[0; 1; 0; 1]; % vertex coordinates Y
TRI=[1 2 4; 1 3 4]; % specifying the triangles defined by indices into the column vector of vertex coordinates (X, Y)
TR = TriRep(TRI, X, Y) % creating 2-D triangulation representation
I would like to compute an area of one of the triangles from the 'TR' object.

Best Answer

To compute the area of the triangles from a triangulation representation specified by an object of TriRep class, you can use the POLYAREA function in MATLAB:
A = polyarea(X,Y)
returns the area of the polygon specified by the vertices in the vectors X and Y.
To compute the area of the first triangle from TR execute the following in the MATLAB command prompt:
ind1=TR.Triangulation(1,:); % indices into TR.X for the 1st triangle
tri1=TR.X(ind1,:); % coordinates of the vertices of the 1st triangle
A = polyarea(tri1(:,1),tri1(:,2)) % the area of the triangle
For more information about the POLYAREA function, please refer to the documentation by executing the following in the MATLAB command prompt:
doc polyarea