function [lambda,faces] = generate_barycentric_lattice(nDiagonals)
% [lambda,faces] = generate_barycentric_lattice(nDiagonals)
% This function generates the barycentric coordinates placed on a lattice
% with nDiagonals, such as (when nDiagonals = 2)
%
% *
% |\
% *-*
% | \
% *-*-*
%
% The function also generates a triangular faces structure as required by the
% patch command, which facilitates plotting functions over triangular cells.
% Initialize the output
lambda = zeros(3,0);
faces = zeros(0,3);
k = 0;
% Loop over and along the diagonals
for i = 0:nDiagonals
for j = 0:i
lambda = [lambda, [0; j/nDiagonals; (i-j)/nDiagonals]];
k = k + 1;
% Create a face
if (i > 0 & j < i)
faces = [faces; k, k+1, k-i];
if (j < i-1)
faces = [faces; k, k-i+1, k-i];
end
end
end % for j = 0:i
end % for i = 0:nDiagonals
% Create the missing barycentric coordinate
lambda(1,:) = 1 - lambda(2,:) - lambda(3,:);