function segment = p09_boundary_segment ( segment_index, m, segment_length )

%*****************************************************************************80
%
%% p09_boundary_segment returns a boundary segment in problem 09.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    12 April 2009
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Per-Olof Persson and Gilbert Strang,
%    A Simple Mesh Generator in MATLAB,
%    SIAM Review,
%    Volume 46, Number 2, June 2004, pages 329-345.
%
%  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:
%
%    SEGMENT(M,SEGMENT_length), points on the boundary segment.
%
  center1 = [ 0.50, 0.50 ];
  center2 = [ 0.25, 0.75 ];
  center3 = [ 0.60, 0.40 ];
  r1 = 0.5;
  r2 = 0.1;
  r3 = 0.1;

  v1(1:2,1:4) = [ ...
    center1(1) - r1, center1(2) - r1; ...
    center1(1) + r1, center1(2) - r1; ...
    center1(1) + r1, center1(2) + r1; ...
    center1(1) - r1, center1(2) + r1 ]';
%
%  Ninny MATLAB error.
%
  for j = 1 : 6
    angle = ( ( j - 1 ) * 2 ) * pi / 6.0;
    v2(1,j) = center2(1) + r2 * cos ( angle );
    v2(2,j) = center2(2) + r2 * sin ( angle );
  end

  for j = 1 : 6
    angle = ( ( j - 1 ) * 2 ) * pi / 6.0;
    v3(1,j) = center3(1) + r3 * cos ( angle );
    v3(2,j) = center3(2) + r3 * sin ( angle );
  end

  if ( segment_index == 1 )

    n1 = round (       ( segment_length - 1 )   / 4 );
    n2 = round ( ( 2 * ( segment_length - 1 ) ) / 4 ) - n1;
    n3 = round ( ( 3 * ( segment_length - 1 ) ) / 4 ) - n1 - n2;
    n4 = segment_length - 1 - n1 - n2 - n3;

    j = 0;

    for i = 1 : n1
      j = j + 1;
      segment(1:2,j) = ( ( n1 - i + 1 ) * v1(1:2,1)   ...
                       + (      i - 1 ) * v1(1:2,2) ) ...
                       / ( n1         );
    end

    for i = 1 : n2
      j = j + 1;
      segment(1:2,j) = ( ( n2 - i + 1 ) * v1(1:2,2)   ...
                       + (      i - 1 ) * v1(1:2,3) ) ...
                       / ( n2         );
    end

    for i = 1 : n3
      j = j + 1;
      segment(1:2,j) = ( ( n3 - i + 1 ) * v1(1:2,3)   ...
                       + (      i - 1 ) * v1(1:2,4) ) ...
                       / ( n3         );
    end

    for i = 1 : n4
      j = j + 1;
      segment(1:2,j) = ( ( n4 - i + 1 ) * v1(1:2,4)   ...
                       + (      i - 1 ) * v1(1:2,1) ) ...
                       / ( n4         );
    end

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

  elseif ( segment_index == 2 )

    n1 = round (       ( segment_length - 1 )   / 6 );
    n2 = round ( ( 2 * ( segment_length - 1 ) ) / 6 ) - n1;
    n3 = round ( ( 3 * ( segment_length - 1 ) ) / 6 ) - n1 - n2;
    n4 = round ( ( 4 * ( segment_length - 1 ) ) / 6 ) - n1 - n2 - n3;
    n5 = round ( ( 5 * ( segment_length - 1 ) ) / 6 ) - n1 - n2 - n3 - n4;
    n6 = segment_length - 1 - n1 - n2 - n3 - n4 - n5;

    j = 0;

    for i = 1 : n1
      j = j + 1;
      segment(1:2,j) = ( ( n1 - i + 1 ) * v2(1:2,1)   ...
                       + (      i - 1 ) * v2(1:2,2) ) ...
                       / ( n1         );
    end

    for i = 1 : n2
      j = j + 1;
      segment(1:2,j) = ( ( n2 - i + 1 ) * v2(1:2,2)   ...
                       + (      i - 1 ) * v2(1:2,3) ) ...
                       / ( n2         );
    end

    for i = 1 : n3
      j = j + 1;
      segment(1:2,j) = ( ( n3 - i + 1 ) * v2(1:2,3)   ...
                       + (      i - 1 ) * v2(1:2,4) ) ...
                       / ( n3         );
    end

    for i = 1 : n4
      j = j + 1;
      segment(1:2,j) = ( ( n4 - i + 1 ) * v2(1:2,4)   ...
                       + (      i - 1 ) * v2(1:2,5) ) ...
                       / ( n4         );
    end

    for i = 1 : n5
      j = j + 1;
      segment(1:2,j) = ( ( n5 - i + 1 ) * v2(1:2,5)   ...
                       + (      i - 1 ) * v2(1:2,6) ) ...
                       / ( n5         );
    end

    for i = 1 : n6
      j = j + 1;
      segment(1:2,j) = ( ( n6 - i + 1 ) * v2(1:2,6)   ...
                       + (      i - 1 ) * v2(1:2,1) ) ...
                       / ( n6         );
    end

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

  elseif ( segment_index == 3 )

    n1 = round (       ( segment_length - 1 )   / 6 );
    n2 = round ( ( 2 * ( segment_length - 1 ) ) / 6 ) - n1;
    n3 = round ( ( 3 * ( segment_length - 1 ) ) / 6 ) - n1 - n2;
    n4 = round ( ( 4 * ( segment_length - 1 ) ) / 6 ) - n1 - n2 - n3;
    n5 = round ( ( 5 * ( segment_length - 1 ) ) / 6 ) - n1 - n2 - n3 - n4;
    n6 = segment_length - 1 - n1 - n2 - n3 - n4 - n5;

    j = 0;

    for i = 1 : n1
      j = j + 1;
      segment(1:2,j) = ( ( n1 - i + 1 ) * v3(1:2,1)   ...
                       + (      i - 1 ) * v3(1:2,2) ) ...
                       / ( n1         );
    end

    for i = 1 : n2
      j = j + 1;
      segment(1:2,j) = ( ( n2 - i + 1 ) * v3(1:2,2)   ...
                       + (      i - 1 ) * v3(1:2,3) ) ...
                       / ( n2         );
    end

    for i = 1 : n3
      j = j + 1;
      segment(1:2,j) = ( ( n3 - i + 1 ) * v3(1:2,3)   ...
                       + (      i - 1 ) * v3(1:2,4) ) ...
                       / ( n3         );
    end

    for i = 1 : n4
      j = j + 1;
      segment(1:2,j) = ( ( n4 - i + 1 ) * v3(1:2,4)   ...
                       + (      i - 1 ) * v3(1:2,5) ) ...
                       / ( n4         );
    end

    for i = 1 : n5
      j = j + 1;
      segment(1:2,j) = ( ( n5 - i + 1 ) * v3(1:2,5)   ...
                       + (      i - 1 ) * v3(1:2,6) ) ...
                       / ( n5         );
    end

    for i = 1 : n6
      j = j + 1;
      segment(1:2,j) = ( ( n6 - i + 1 ) * v3(1:2,6)   ...
                       + (      i - 1 ) * v3(1:2,1) ) ...
                       / ( n6         );
    end

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

  else

    fprintf ( 1, '\n' );
    fprintf ( 1, 'P09_boundary_segment - Fatal error!\n' );
    fprintf ( 1, '  Illegal SEGMENT_INDEX = %d\n', segment_index );
    error ( 'P09_boundary_segment - Fatal error!' );

  end

  return
end
