function segment = p13_boundary_segment ( segment_index, m, segment_length )

%*****************************************************************************80
%
%% p13_boundary_segment returns a boundary segment in problem 13.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    12 April 2009
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer SEGMENT_INDEX, the index of the boundary segment.
%
%    integer M, the spatial dimension.
%
%    integer SEGMENT_length, the number of points in the segment.
%
%  Output:
%
%    real SEGMENT(M,SEGMENT_length), points on the boundary segment.
%
  s = [ ...
    10.000,  0.000; ...
    20.000,  0.000; ...
    80.000,  0.000; ...
    90.000,  0.000; ...
    55.000, 39.686268; ...
    55.000, 90.000; ...
    45.000, 90.000; ...
    45.000, 39.686268 ]';

  n = segment_length - 1;

  angle = acos ( 5.0 / 40.0 );

  arc1 = 30.0 * pi;
  arc2 = 10.0;
  arc3 = 40.0 * angle;
  arc4 = 90.0 - 40.0 * sin ( angle );
  arc5 = 10.0;
  arc6 = 90.0 - 40.0 * sin ( angle );
  arc7 = 40.0 * angle;
  arc8 = 10.0;

  arc_length = arc1 + arc2 + arc3 + arc4 + arc5 + arc6 + arc7 + arc8;

  n01 = round ( arc1 ...
    * ( n - 1 ) / arc_length );

  n02 = round ( ( arc1 + arc2 ) ...
    * ( n - 1 ) / arc_length ) ...
    - n01;
    
  n03 = round ( ( arc1 + arc2 + arc3 ) ...
    * ( n - 1 ) / arc_length ) ...
    - n01 - n02;

  n04 = round ( ( arc1 + arc2 + arc3 + arc4 ) ...
    * ( n - 1 ) / arc_length ) ...
    - n01 - n02 - n03;

  n05 = round ( ( arc1 + arc2 + arc3 + arc4 + arc5 ) ...
    * ( n - 1 ) / arc_length ) ...
    - n01 - n02 - n03 - n04;

  n06 = round ( ( arc1 + arc2 + arc3 + arc4 + arc5 + arc6 ) ...
    * ( n - 1 ) / arc_length ) ...
    - n01 - n02 - n03 - n04 - n05;

  n07 = round ( ( arc1 + arc2 + arc3 + arc4 + arc5 + arc6 + arc7 ) ...
    * ( n - 1 ) / arc_length ) ...
    - n01 - n02 - n03 - n04 - n05 - n06;

  n08 = n - n01 - n02 - n03 - n04 - n05 - n06 - n07;

  j = 0;
%
%  1: Bottom semicircle.
%
  for i = 1 : n01
    j = j + 1;
    angle = ( n01 - i + 1 ) * pi / n01;
    segment(1,j) = 50.0 + 30.0 * cos ( angle );
    segment(2,j) =  0.0 + 30.0 * sin ( angle );
  end
%
%  2: Right foot.
%
  for i = 1 : n02
    j = j + 1;
    segment(1:2,j) = ( ( n02 - i + 1 ) * s(1:2,3)   ...
                     + (       i - 1 ) * s(1:2,4) ) ...
                     / ( n02         );
  end
%
%  3: Top right pen-semicircle.
%
  angle1 = 0.0;
  angle2 = acos ( 5.0 / 40.0 );

  for i = 1 : n03
    j = j + 1;
    angle = ( i - 1 ) * angle2 / ( n03 );
    segment(1,j) = 50.0 + 40.0 * cos ( angle );
    segment(2,j) =  0.0 + 40.0 * sin ( angle );
  end
%
%  4: Right side of shaft.
%
  for i = 1 : n04
    j = j + 1;
    segment(1:2,j) = ( ( n04 - i + 1 ) * s(1:2,5)   ...
                     + (       i - 1 ) * s(1:2,6) ) ...
                     / ( n04         );
  end
%
%  5: Top of shaft.
%
  for i = 1 : n05
    j = j + 1;
    segment(1:2,j) = ( ( n05 - i + 1 ) * s(1:2,6)   ...
                     + (       i - 1 ) * s(1:2,7) ) ...
                     / ( n05         );
  end
%
%  6: Left side of shaft.
%
  for i = 1 : n06
    j = j + 1;
    segment(1:2,j) = ( ( n06 - i + 1 ) * s(1:2,7)   ...
                     + (       i - 1 ) * s(1:2,8) ) ...
                     / ( n06         );
  end
%
%  7: Left pen-semicircle.
%
  angle1 = pi - acos ( 5.0 / 40.0 );
  angle2 = pi;

  for i = 1 : n07
    j = j + 1;
    angle = ( ( n07 - i + 1  ) * angle1 + ...
              (       i - 1 ) * angle2 ) ...
            / ( n07         );
    segment(1,j) = 50.0 + 40.0 * cos ( angle );
    segment(2,j) =  0.0 + 40.0 * sin ( angle );
  end
%
%  8: Left foot.
%
  for i = 1 : n08
    j = j + 1;
    segment(1:2,j) = ( ( n08 - i + 1 ) * s(1:2,1)   ...
                     + (       i - 1 ) * s(1:2,2) ) ...
                     / ( n08         );
  end

  j = j + 1;
  segment(1:2,j) = s(1:2,2);

  return
end
