function [ r_label, r_color ] = variomino_tiling_print ( r_shape, p_num, ...
  p_shapes, d, x, label );

%*****************************************************************************80
%
%% variomino_tiling_print() prints a variomino tiling.
%
%  Discussion:
%
%    A polyomino is a shape formed by connecting unit squares edgewise
%
%    A variomino is a polyomino in which each square has been assigned
%    a value between 1 and v.
%
%    A variomino can be represented by an MxN matrix, whose entries are
%    between 1 and v for squares that are part of the variomino, 
%    and 0 otherwise.
%
%    You can change the internal parameter "COLOR_CHOICE" to specify how to 
%    set the numbers that identify each variomino component of the tiling.
%
%    You can change the internal parameter "SHOW_ZERO" to specify whether zero 
%    cells should be shown, or blanked out.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 May 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer r_shape(r_m,r_n): a matrix of values between 0 and V,
%    representing the region to be tiled.
%
%    integer p_num: the number of variomino shapes to be used.
%
%    integer p_shapes(r_m,r_n,p_num): a matrix representing the variominoes.  
%    The matrix is "top-left tight", that is, the I-th variomino 
%    is stored in P_SHAPES(1:r_m,1:r_n,I) with a nonzero in the first row, 
%    a nonzero in the first column, but with trailing rows and columns of 
%    zeros allowed.
%
%    integer d(p_num): the number copies of each variomino to be used.
%
%    integer x(x_m): a solution to the problem, consisting of a binary value 
%    (0 or 1) indicating whether a given possible variomino configuration
%    is to be used in this tiling.
%
%    string label: a label for the printout, such as 
%    'Solution #3 of the 2x4 Example'.
%
%  Output:
%
%    integer r_label(r_m,r_n): an array of indices that suggest
%    the structure of the tiling.  Regions of R that are not used will either
%    be blank, or marked with zero.  Regions of R corresponding to a given
%    variomino will be marked by a numeric label whose value depends on the
%    value of the internal parameter "color_choice".
%
%    integer r_color(r_m,r_n): an array of indices that indicate
%    the colors applied to the tiling.  Regions of R that are not used will 
%    either be blank, or marked with zero.  The color range depends on the
%    value of the internal parameter "color_choice".
%

%
%  COLOR_CHOICE:
%
%  white for space, and:
%  1: 2 colors, a single color for all variominoes.
%  2: NZ+1 colors, separate color for each visible variomino.
%  3: VAR_NUM+1, separate color for every variomino, visible or not.
%  4: P_NUM+1, separate color for each variomino "parent", and variants.
%
  color_choice = 4;
%
%  SHOW_ZERO
%  0: do not show zero regions.
%  1: show zero regions.
%
  show_zero = 1;
%
%  Get the linear system satisfied by A*x=b.
%
  [ A, b, parent ] = variomino_matrix ( r_shape, p_num, p_shapes, d );

  b2 = A * x;
%
%  Get the size of the linear system.
%
  [ a_m, a_n ] = size ( A );
%
%  Get the size of R.
%
  [ r_m, r_n ] = size ( r_shape );
%
%  Get the number of cells in R.
%
  r_num = variomino_area ( r_shape );
%
%  Index the nonzero cells in R.
%
  r_index = variomino_index ( r_shape );
%
%  Get the size of X.
%  Code modified so it can only handle 1 solution at a time.
%
  [ x_m, x_n ] = size ( x );

% for x_index = 1 : x_n
    x_index = 1;
%
%  Replace each nonzero X(I,*) by an increasing index.
%
    xc = zeros ( x_m, 1 );
    xi = zeros ( x_m, 1 );

    nz = 0;
    var = 0;
    par = 0;

    for i = 1 : x_m

      if ( x(i,x_index) == 0 )
        color = 0;
      else
        nz = nz + 1;
        color = nz;
        xc(i) = color;
        xi(i) = nz;
      end

    end
%
%  The first R_NUM rows of A are equations about covering each cell of R.
%
%  Multiplying this matrix times XC gives us:
%
    axc = A(1:r_num,1:a_n) * xc;
    axi = A(1:r_num,1:a_n) * xi;
%
%  R_LABEL replaces each nonzero entry of R_SHAPE by the index of the 
%  variomino variant which covers it.
%
    r_color = zeros ( r_m, r_n );
    for i = 1 : r_m
      for j = 1 : r_n
        if ( r_index(i,j) == 0 )
          r_color(i,j) = 0;
        else
          r_color(i,j) = axc(r_index(i,j));
        end
      end
    
    end

    r_label = zeros ( r_m, r_n );
    for i = 1 : r_m
      for j = 1 : r_n
        if ( r_index(i,j) == 0 )
          r_label(i,j) = 0;
        else
          r_label(i,j) = axi(r_index(i,j));
        end
      end
    
    end
%
%  Print R_LABEL.
%
    fprintf ( 1, '\n' );
    fprintf ( 1, '%s\n', label );
    fprintf ( 1, '  Numeric Labels\n' );
    fprintf ( 1, '\n' );

    smax = max ( max ( r_label ) );
  
    for i = 1 : r_m
      fprintf ( 1, ' ' );
      for j = 1 : r_n
        if ( 10 <= smax && smax < 100 )
          if ( r_label(i,j) == 0 && show_zero == 0 )
            fprintf ( 1, '   ' );
          else
            fprintf ( 1, ' %2d', r_label(i,j) );
          end
        else
          if ( r_label(i,j) == 0 && show_zero == 0 )
            fprintf ( 1, '  ' );
          else
            fprintf ( 1, ' %d', r_label(i,j) );
          end
        end
      end
      fprintf ( 1, '\n' );
    end
%
%  Print R_COLOR
%
    fprintf ( 1, '\n' );
    fprintf ( 1, '%s\n', label );
    fprintf ( 1, '  "Colors"\n' );
    fprintf ( 1, '\n' );

    smax = max ( max ( r_label ) );
  
    for i = 1 : r_m
      fprintf ( 1, ' ' );
      for j = 1 : r_n
        if ( 10 <= smax && smax < 100 )
          if ( r_label(i,j) == 0 && show_zero == 0 )
            fprintf ( 1, '   ' );
          else
            fprintf ( 1, ' %2d', r_color(i,j) );
          end
        else
          if ( r_label(i,j) == 0 && show_zero == 0 )
            fprintf ( 1, '  ' );
          else
            fprintf ( 1, ' %d', r_color(i,j) );
          end
        end
      end
      fprintf ( 1, '\n' );
    end

% end

  return
end
