function x = triangle_sample ( v, n )

%*****************************************************************************80
%
%% triangle_sample() uniformly samples a triangle.
%
%  Discussion:
%
%    The triangle is defined by three vertices.  This routine
%    uses Turk's rule #2.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    20 February 2019
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Greg Turk,
%    Generating Random Points in a Triangle,
%    in Graphics Gems,
%    edited by Andrew Glassner,
%    AP Professional, 1990, pages 24-28.
%
%  Input:
%
%    real V(3,2), the X and Y coordinates of the vertices.
%
%    integer N, the number of points desired.
%
%  Output:
%
%    real X(N,2), the points.
%
  r = zeros ( n, 3 );
  r(1:n,1:2) = rand ( n, 2 );
  i = find ( 1.0 < r(:,1) + r(:,2) );
  r(i,1:2) = 1.0 - r(i,1:2);
  r(1:n,3) = 1.0 - r(1:n,1) - r(1:n,2);

  x = r * v;

  return
end
