function [ r, center ] = triangle_incircle ( t )

%*****************************************************************************80
%
%% triangle_incircle() computes the inscribed circle of a triangle in 2D.
%
%  Discussion:
%
%    The inscribed circle of a triangle is the largest circle that can
%    be drawn inside the triangle.  It is tangent to all three sides,
%    and the lines from its center to the vertices bisect the angles
%    made by each vertex.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 July 2023
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Adrian Bowyer and John Woodwark,
%    A Programmer's Geometry,
%    Butterworths, 1983.
%
%  Input:
%
%    real T(3,2), the triangle vertices.
%
%  Output:
%
%    real R, CENTER(1,2), the radius and center of the
%    inscribed circle.
%

%
%  Compute the length of each side.
%
  a = norm ( t(1,1:2) - t(2,1:2) );
  b = norm ( t(2,1:2) - t(3,1:2) );
  c = norm ( t(3,1:2) - t(1,1:2) );

  perimeter = a + b + c;

  center = zeros ( 1, 2 );

  if ( perimeter == 0.0 )
    center = t(1,1:2);
    r = 0.0;
    return
  end

  center = (  ...
      b * t(1,1:2) ...
    + c * t(2,1:2) ...
    + a * t(3,1:2) ) / perimeter;

  r = 0.5 * sqrt ( ...
      ( - a + b + c )  ...
    * ( + a - b + c )  ...
    * ( + a + b - c ) / perimeter );

  return
end
