function p = triangle_sample ( t, n )

%*****************************************************************************80
%
%% triangle_sample() returns random points in a triangle.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 July 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real T(3,2), the triangle vertices.
%
%    integer N, the number of points to generate.
%
%  Output:
%
%    real P(N,2), random points in the triangle.
%
  alpha = rand ( n, 1 );
%
%  Interpret R as a percentage of the triangle's area.
%
%  Imagine a line L, parallel to side 1, so that the area between
%  vertex 1 and line L is R percent of the full triangle's area.
%
%  The line L will intersect sides 2 and 3 at a fraction
%  ALPHA = SQRT ( R ) of the distance from vertex 1 to vertices 2 and 3.
%
  alpha = sqrt ( alpha );
%
%  Determine the coordinates of the points on sides 2 and 3 intersected
%  by line L.
%
  p12 = zeros ( n, 2 );
  p13 = zeros ( n, 2 );

  for j = 1 : 2
    p12(1:n,j) = ( 1.0 - alpha(1:n) ) * t(1,j) ...
                       + alpha(1:n)   * t(2,j);

    p13(1:n,j) = ( 1.0 - alpha(1:n) ) * t(1,j) ...
                       + alpha(1:n)   * t(3,j);
  end
%
%  Now choose, uniformly at random, a point on the line L.
%
  beta = rand ( n, 1 );
  p = zeros ( n, 2 );

  for j = 1 : 2
    p(1:n,j) = ( 1.0 - beta(1:n) ) .* p12(1:n,j) ...
                     + beta(1:n)   .* p13(1:n,j);
  end

  return
end
