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 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).
%
%    The (x,y) coordinates refer to the standard Cartesian coordinate
%    system, so that the unit parallelogram vertices have
%    (x,y) coordinates:
%      A = (0,0) --> B = (1,0) --> C = (1+c,s) --> D = (c,s),
%    where c = cos(60) = 0.5 and s = sin(60) = sqrt(3)/2.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    16 October 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer IJK(3), 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 );

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

  if ( k == 0 )
    q1 = ij_to_xy ( [ i - 1, j - 1 ] );
    q2 = ij_to_xy ( [ i,     j - 1 ] );
    q3 = ij_to_xy ( [ i - 1, j     ] );
  elseif ( k == 1 )
    q1 = ij_to_xy ( [ i,     j     ] );
    q2 = ij_to_xy ( [ i - 1, j     ] );
    q3 = 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

  x = [ q1(1); q2(1); q3(1) ];
  y = [ q1(2); q2(2); q3(2) ];

  return
end

