function [ x, y ] = ijk_to_xy ( i, j, k )

%*****************************************************************************80
%
%% ijk_to_xy returns the (x,y) coordinates of a triangle in the ijk system.
%
%  Discussion:
%
%    The (i,j,k) system refer to a parallelogram whose basic cell
%    is the unit parallelogram outlined by points with (i,j)
%    coordinates (0,0) --> (1,0) --> (1+c,s) --> (c,s).
%
%         *-----*
%        /     /
%       /     /
%      *-----*
%
%    where c = cos(60) = 0.5 and s = sin(60) = sqrt(3)/2.
%
%    Each cell is divided into k=0 and k=1 triangles
%
%         *-----*
%        / \ 1 /
%       / 0 \ /
%      *-----*
%
%    Triangle are identified by a related (i,j,k) coordinate.
%
%    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).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 August 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer I, J, K, the parallelogram coordinates of a triangle.
%
%  Output:
%
%    real X(3), Y(3): the Cartesian coordinates of the vertices.
%
  x = zeros ( 3, 1 );
  y = zeros ( 3, 1 );

  if ( k == 0 )
    [ x(1), y(1) ] = ij_to_xy ( i - 1, j - 1 );
    [ x(2), y(2) ] = ij_to_xy ( i,     j - 1 );
    [ x(3), y(3) ] = ij_to_xy ( i - 1, j     );
  elseif ( k == 1 )
    [ x(1), y(1) ] = ij_to_xy ( i,     j     );
    [ x(2), y(2) ] = ij_to_xy ( i - 1, j     );
    [ x(3), y(3) ] = ij_to_xy ( i,     j - 1 );
  else
    fprintf ( 1, '\n' );
    fprintf ( 1, 'ijk_to_xy - Fatal error!' );
    fprintf ( 1, '  Triangle has illegal value k = %d\n', k );
    error ( 'ijk_to_xy - Fatal error!' );
  end

  return
end

