function pdf = triangle_equilateral_distance_pdf ( d )

%*****************************************************************************80
%
%% triangle_equilateral_distance_pdf: PDF for equilateral triangle distance..
%
%  Discussion:
%
%    This calculation is for an equilateral triangle with side 1.
%    PDF(D) is zero if D is less than 0 or greater than 1.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    21 February 2019
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Uwe Baesel,
%    Random chords and point distances in regular polygons,
%    Acta Mathematica Universitatis Comenianae,
%    Volume LXXXII, Number 1, pages 1-18, 2014.
%
%  Parameters:
%
%    Input, real D, the distance.
%
%    Output, real PDF, the probability density function for that distance.
%

%
%  s is the length of a side.
%
  s = 1.0;
%
%  r is the radius of the circumscribed circle.
%
  r = s / sqrt ( 3.0 );

  u = 3.0 * sqrt ( 3.0 ) * r;
  A = ( 3.0 / 4.0 ) * sqrt ( 3.0 ) * r^2;

  d = linspace ( 0.0, s, 101 );

  phi = ( 3.0 * sqrt ( 3.0 ) + 2.0 * pi ) * d.^2 / ( 36.0 * r ) ...
    .* ( 0 < d ) .* ( d <= 1.5 * r ) ...
    + ( 1.5 * ( d .* sqrt ( 1.0 - ( 1.5 * r ./ d ).^2 ) - 0.5 * pi * r ) ...
      + ( 1.0 / 4.0 / sqrt ( 3.0 ) - pi / 9.0 ) * d.^2 / r ...
      + ( 1.5 * r + d.^2 / 3.0 / r ) .* asin ( 1.5 * r ./ d ) ) ...
    .* ( 1.5 * r < d ) .* ( d <= s );

  pdf = 2.0 * d / A .* ( pi + ( u / A ) * ( phi - d ) );

  return
end

