function [ x, y ] = ijk_to_xy ( ijk )

%*****************************************************************************80
%
%% ijk_to_xy() returns the (x,y) coordinates of a triangle in the ijk system.
%
%  Discussion:
%
%    The (i,j) coordinates refer to a grid whose basic cell
%    is the unit square outlined by points (A,B,C,D):
%      A = (0,0) --> B = (1,0) --> C = (1,1) --> D = (0,1).
%
%      D-----C
%      |     |
%      |     |
%      |  E  |
%      |     |
%      |     |
%      A-----B
%
%    Each cell is divided into triangles with index k = 0, 1, 2, 3:
%
%      D-----C
%      |\ 2 /|
%      | \ / |
%      |3 E 1|
%      | / \ |
%      |/ 0 \|
%      A-----B
%
%    Triangles are identified by a related (i,j,k) coordinate based 
%    on the (i,j) coordinates of its vertices:
%      k = 0: A, B, E.
%      k = 1: B, C, E.
%      k = 2: C, D, E
%      k = 3: D, A, E
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 December 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer IJK(3), the coordinates of a triangle.
%
%  Output:
%
%    real X(3), Y(3): the Cartesian coordinates of the vertices.
%
  x = zeros ( 3, 1 );
  y = zeros ( 3, 1 );

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

  im = i - 0.5;
  jm = j - 0.5;

  if ( k == 0 )
    x = [ i-1, i,   im ];
    y = [ j-1, j-1, jm];
  elseif ( k == 1 )
    x = [ i,   i,   im];
    y = [ j-1, j,   jm];
  elseif ( k == 2 )
    x = [ i,   i-1, im];
    y = [ j,   j,   jm];
  elseif ( k == 3 )
    x = [ i-1, i-1, im];
    y = [ j,   j-1, jm];
  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
 
