function xsi = triangle_xy_to_xsi ( t, p )

%*****************************************************************************80
%
%% triangle_xy_to_xsi() converts from XY to barycentric in 2D.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    05 December 2010
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real T(3,2), the triangle vertices.
%
%    real P(1,2), the XY coordinates of a point.
%
%  Output:
%
%    real XSI(1,3), the barycentric coordinates of the point.
%    XSI1 + XSI2 + XSI3 should equal 1.
%

  det = ( t(1,1) - t(3,1) ) * ( t(2,2) - t(3,2) ) ...
      - ( t(2,1) - t(3,1) ) * ( t(1,2) - t(3,2) );

  xsi = zeros ( 1, 3 );

  xsi(1,1) = (   ( t(2,2) - t(3,2) ) * ( p(1,1) - t(3,1) ) ...
               - ( t(2,1) - t(3,1) ) * ( p(1,2) - t(3,2) ) ) / det;

  xsi(1,2) = ( - ( t(1,2) - t(3,2) ) * ( p(1,1) - t(3,1) ) ...
               + ( t(1,1) - t(3,1) ) * ( p(1,2) - t(3,2) ) ) / det;

  xsi(1,3) = 1.0 - xsi(1,1) - xsi(1,2);

  return
end
