function [ rk, rt ] = rectangle_elements ( nx, ny )

%*****************************************************************************80
%
%% rectangle_elements(): list triangle elements in a rectangle.
%
%  Discussion:
%
%    The subsquare i <= I <= i+1, j <= J <= J+1 is divided into
%    four triangle elements:
%
%   N2------N5
%    |\  3 /|
%    | \  / |
%    |  \/  |
%    |4 N3 2|
%    |  /\  |
%    | /  \ |
%    |/ 1  \|
%   N1------N4
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 December 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer NX, the width of the grid.
%
%    integer NY, the height of the grid.
%
%  Output:
%
%    integer rk(rn,3): rk(I,J) indexes the J-th node of the I-th triangle.
%
%    integer rt(rn,1), the type of the triangle, 1, 2, 3 or 4.
%
  rn = 4 * nx * ny;
  rk = zeros ( rn, 3 );
  rt = zeros ( rn, 1 );
%
%  Index of the current triangle.
%
  ri = 0;
%
%  Work on the next big column.
%
  for col = 1 : nx
%
%  Work on the next big row.
%
    for row = 1 : ny
%
%  Index the 5 nodes in the current square of four triangles.
%
      if ( col == 1 && row == 1 )
        n1 = 1;
      elseif ( 1 < col && row == 1 )
        n1 = ( col - 1 ) * ( 2 * ny + 1 ) + 1;
      else
        n1 = n2;
      end

      n2 = n1 + 1;
      n3 = n1 + ny + 1;
      n4 = n1 + 2 * ny + 1;
      n5 = n1 + 2 * ny + 2;

      ri = ri + 1;
      rk(ri,1:3)  = [ n1, n4, n3 ];
      rt(ri,1)    = 1;

      ri = ri + 1;
      rk(ri,1:3)  = [ n4, n5, n3 ];
      rt(ri,1)    = 2;

      ri = ri + 1;
      rk(ri,1:3)  = [ n5, n2, n3 ];
      rt(ri,1)    = 3;

      ri = ri + 1;
      rk(ri,1:3)  = [ n2, n1, n3 ];
      rt(ri,1)    = 4;
     
    end
  end

  return
end

