function [ pn, dist ] = triangle_point_near ( t, p )

%*****************************************************************************80
%
%% triangle_point_near() computes the nearest point on a triangle in 2D.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 July 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real T(3,2), the triangle vertices.
%
%    real P(1,2), the point whose nearest triangle point
%    is to be determined.
%
%  Output:
%
%    real PN(1,2), the nearest point to P.
%
%    real DIST, the distance from the point to the triangle.
%
  nside = 3;
%
%  Find the distance to each of the line segments that make up the edges
%  of the triangle.
%
  dist = Inf;
  pn(1,1:2) = 0.0;

  for i = 1 : nside

    ip1 = i4_wrap ( i + 1, 1, nside );

    [ pn2, dist2, tval ] = segment_point_near_2d ( t(i,1:2), ...
      t(ip1,1:2), p );

    if ( dist2 < dist )
      dist = dist2;
      pn(1,1:2) = pn2(1,1:2);
    end

  end

  return
end
