function neighbors = ijk_neighbors ( ijk )

%*****************************************************************************80
%
%% ijk_neighbors() returns the IJK coordinates of neighbor triangles.
%
%  Discussion:
%
%    The (i,j) coordinates refer to a parallelogram whose basic cell
%    is the unit parallelogram outlined by points (A,B,C,D)
%    with (i,j) coordinates
%      A = (0,0) --> B = (1,0) --> C = (1,1) --> D = (0,1).
%
%         D-----C
%        /     /
%       /     /
%      A-----B
%
%    Each cell is divided into k=0 and k=1 triangles:
%
%         *-----*
%        / \ 1 /
%       / 0 \ /
%      *-----*
%
%    Triangles are identified by a related (i,j,k) coordinate based 
%    on the (i,j) coordinates of its vertices:
%      If k = 0, the "ijk" triangle has vertices (i-1,j-1), (i,j-1), (i-1,j).
%      If k = 1, the "ijk" triangle has vertices (i,j),     (i-1,j), (i,j-1).
%
%    Triangle (i,j,0) has neighbors:
%      (i,j-1,1), (i,j,1), (i-1,j,1), 
%
%              3 *-------*-------*-------*
%               / \     / \     / \     /
%              /   \   /   \   /   \   /
%             /     \ /     \ /     \ /
%  ^       2 *-------*-------*-------*
%  |        / \I-1J1/ \IJ1  / \     /
%  J       /   \   /   \   /   \   /
%         /     \ / IJ0 \ /     \ /
%      1 *-------*-------*-------*
%       / \     / \IJ-11/ \     /
%      /   \   /   \   /   \   /
%     /     \ /     \ /     \ /
%  0 *-------*-------*-------*
%    0       1       2       3 
%
%                I -->
%
%    Triangle (i,j,1) has neighbors:
%      (i,j+1,0), (i,j,0), (i+1,j,0)
%
%              3 *-------*-------*-------*
%               / \     / \     / \     /
%              /   \   /   \   /   \   /
%             /     \ /IJ+10\ /     \ /
%  ^       2 *-------*-------*-------*
%  |        / \     / \ IJ1 / \     /
%  J       /   \   /   \   /   \   /
%         /     \ / IJ0 \ /I+1J0\ /
%      1 *-------*-------*-------*
%       / \     / \     / \     /
%      /   \   /   \   /   \   /
%     /     \ /     \ /     \ /
%  0 *-------*-------*-------*
%    0       1       2       3 
%
%                I -->
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    03 November 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer IJK(3), the indices of the triangle.
%
%  Output:
%
%    integer NEIGHBORS(3,3): NEIGHBORS(I,1:3) are the coordinates of
%    the I-th neighbor triangle.
%
  neighbors = zeros ( 3, 3 );

  i = ijk(1);
  j = ijk(2);
  k = ijk(3);

  if ( k == 0 )
    neighbors(1,1:3) = [ i,     j - 1, 1 ];
    neighbors(2,1:3) = [ i,     j,     1 ];
    neighbors(3,1:3) = [ i - 1, j,     1 ];
  elseif ( k == 1 )
    neighbors(1,1:3) = [ i,     j + 1, 0 ];
    neighbors(2,1:3) = [ i,     j,     0 ];
    neighbors(3,1:3) = [ i + 1, j,     0 ];
  else
    fprintf ( 1, '\n' );
    fprintf ( 1, 'ijk_neighbors - Fatal error!\n' );
    fprintf ( 1, '  Triangle has illegal k index = %d.\n', k );
    error ( 'ijk_neigbors - Fatal error!' );
  end

  return
end

