function inside = triangle_contains_point ( t, p )

%*****************************************************************************80
%
%% triangle_contains_point() finds if a point is inside a triangle in 2D.
%
%  Discussion:
%
%    The routine assumes that the vertices are given in counter-clockwise
%    order.  If the triangle vertices are actually given in clockwise
%    order, this routine will behave as though the triangle contains
%    no points whatsoever!
%
%    The routine determines if a point P is "to the right of" each of the lines
%    that bound the triangle.  It does this by computing the cross product
%    of vectors from a vertex to its next vertex, and to P.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 July 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real T(3,2), the triangle vertices.
%    The vertices should be given in counter clockwise order.
%
%    real P(1,2), the point to be checked.
%
%  Output:
%
%    logical INSIDE, TRUE if the point is inside the triangle.
%
  inside = true;

  for i = 1 : 3

    k = mod ( i, 3 ) + 1;

    if ( 0.0 < ( p(1) - t(i,1) ) * ( t(k,2) - t(i,2) ) ...
             - ( p(2) - t(i,2) ) * ( t(k,1) - t(i,1) ) )
      inside = false;
      return
    end

  end

  return
end
