function [ more, i1, j1, i2, j2, i3, j3 ] = subtriangle_next ( n, more, ...
  i1, j1, i2, j2, i3, j3 )

%*****************************************************************************80
%
%% subtriangle_next() computes the next subtriangle of a triangle.
%
%  Discussion:
%
%    The three sides of a triangle have been subdivided into N segments,
%    inducing a natural subdivision of the triangle into N*N subtriangles.
%    It is desired to consider each subtriangle, one at a time, in some
%    definite order.  This routine can produce information defining each 
%    of the subtriangles, one after another.
%
%    The subtriangles are described in terms of the integer coordinates 
%    (I,J) of their vertices.  These coordinates both range from 0 to N,
%    with the additional restriction that I + J <= N.
%
%    The vertices of each triangle are listed in counterclockwise order.
%
%  Example:
%
%    N = 4
%
%    4  *
%       |\
%       16\
%    3  *--*
%       |14|\
%       13\15\
%    2  *--*--*
%       |\9|11|\
%       |8\10\12\
%    1  *--*--*--*
%       |\2|\4|\6|\
%       |1\|3\|5\|7\
%   0   *--*--*--*--*
%
%       0  1  2  3  4
%
%    Rank  I1 J1  I2 J2  I3 J3
%    ----  -----  -----  ----- 
%       1   0  0   1  0   0  1
%       2   1  1   0  1   1  0
%       3   1  0   2  0   1  1
%       4   2  1   1  1   2  0
%       5   2  0   3  0   2  1
%       6   3  1   1  1   3  0
%       7   3  0   4  0   3  1
%       8   0  1   1  1   0  2
%       9   1  2   0  2   1  1
%      10   1  1   2  1   1  2
%      11   2  2   1  2   2  1
%      12   2  1   3  1   2  2
%      13   0  2   1  2   0  3
%      14   1  3   0  3   1  2
%      15   1  2   2  2   1  3
%      16   0  3   1  3   0  4
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    27 March 2007
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, indicates the number of subdivisions of each side
%    of the original triangle.
%
%    logical MORE.  On first call, set MORE to FALSE.  Thereafter, 
%    the input value of MORE should be the output value from the previous
%    call.
%
%    integer I1, J1, I2, J2, I3, J3, the indices of the vertices 
%    of the subtriangle as computed on the previous call.  On the first call
%    with MORE set to FALSE, set these values to 0.
%
%  Output:
%
%    logical MORE, the output value of MORE will be TRUE if there 
%    are more subtriangles that can be generated by further calls.  However, 
%    if MORE is returned as FALSE, the accompanying subtriangle information 
%    refers to the last subtriangle that can be generated.
%
%    integer I1, J1, I2, J2, I3, J3, the indices of the 
%    vertices of the subtriangle.
%
  if ( n < 1 )
    more = 0;
    return;
  end

  if ( ~ more )

    i1 = 0;
    j1 = 0;
    i2 = 1;
    j2 = 0;
    i3 = 0;
    j3 = 1;

    if ( n == 1 )
      more = 0;
    else
      more = 1;
    end
%
%  We last generated a triangle like:
%
%    2---1
%     \  |
%      \ |
%       \|
%        3
%
  elseif ( i2 < i3 )

    i1 = i3;
    j1 = j3;
    i2 = i1 + 1;
    j2 = j1;
    i3 = i1;
    j3 = j1 + 1;
%
%  We last generated a triangle like
%
%    3
%    |\
%    | \
%    |  \
%    1---2
%
  elseif ( i1 + 1 + j1 + 1 <= n )

    i1 = i1 + 1;
    j1 = j1 + 1;
    i2 = i1 - 1;
    j2 = j1;
    i3 = i1;
    j3 = j1 - 1;
%
%  We must be at the end of a row.
%
  else

    i1 = 0;
    j1 = j1 + 1;
    i2 = i1 + 1;
    j2 = j1;
    i3 = i1;
    j3 = j1 + 1;

    if ( n <= j1 + 1 )
      more = 0;
    end

  end

  return
end
