function t_solution_plot ( x, filename, label, grid_word, grid_index, ...
  tile_num, tile_word )

%*****************************************************************************80
%
%% t_solution_plot() plots a T puzzle solution.
%
%  Discussion:
%
%    This function assumes:
%    * there was an LP file generated;
%    * the linear system defined by the LP file was solved;
%    * the binary (0/1) solution vector has been extracted as X.
%
%    This function then produces a plot of the tiling of the region
%    as defined by the solution vector.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    24 January 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer X(*), the solution file.  Each 1 selects a configuration,
%    encoding the rotation, reflection and translation of a tile.
%
%    string FILENAME: the name of the plotfile.
%
%    string LABEL: a title to print with the plot.
%
%    character GRID_WORD:
%
%    integer GRID_INDEX, chooses one of 4 possible grids.
%    1: the T
%    2: the arrow
%    3: the rhomb
%    4: the fat arrow
%
%    TILE_NUM(): a function that returns the number of tiles.
%
%    TILE_WORD(): a function that returns the boundary word and
%    base point for a tile.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 't_solution_plot():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Plot a T-puzzle solution.\n' );
%
%  Now go through the motions of generating every possible tile configuration.
%  When you have generated the word corresponding to a tile which is part of
%  the solution, add that to the plot.
%
  color = 0;
%
%  Specify the number of tiles.
%
  tile_count = tile_num ( );
%
%  Define the region R.
%    grid_w: boundary word for R.
%    grid_p: (I,J) coordinates of 30-degree node of triangle #1 of R.
%
  [ grid_w, grid_p ] = grid_word ( grid_index );
  figure ( );
  t_word_plot ( grid_w, grid_p );
  title ( label );
%
%  Analyze the region R.
%    TK_R: for each triangle in R, the indices of the 30, 60, 90 degree nodes.
%    TT_R: for each triangle in R, the triangle type, between -6 and -1, or 1 and 6.
%    WIJ_R: (I,J) coordinates of the nodes of R.
%
% [ tk_r, tt_r, wij_r ] = triangle_k ( grid_w, grid_p );
  [ grid_tri, grid_type, grid_xy ] = object_elements ( grid_w, grid_p );

  grid_element_num = size ( grid_tri, 1 );
%
%  Allocate arrays:
%    A for the EQN_NUM x VAR_NUM linear system.
%    b for the EQN_NUM right hand side.
%    EQN_NUM = number of regions to cover and number of tiles to use.
%    VAR_NUM has to be determined, by generating all distinct tile configurations.
%
  eqn_num = grid_element_num + tile_count;
  nz_num = 0;
  var_num = 0;
%
%  Loop over tiles.
%
  for tile_index = 1 : tile_count
%
%  Start a constraint row for this tile.
%
    eqn_cons = grid_element_num + tile_index;
%
%  Consider all counterclockwise tile rotations that are multiples of 90 degrees:
%
    for r = 0 : 3
%
%  Consider a tile or its reflection.
%
      for reflect = 0 : 1
%
%  Get a fresh copy of the word and base point.
%
        [ tile_w, tile_p ] = tile_word ( tile_index );
%
%  Perform R counterclockwise rotations of 90 degrees.
%
        [ tile_w, tile_p ] = t_rotate_word ( tile_w, tile_p, r );
%
%  Perform REFLECT reflections across X axis.
%
        if ( reflect == 1 )
          [ tile_w, tile_p ] = t_reflect_word ( tile_w, tile_p );
        end
%
%  Get the TYPE of the triangles that are the tile elements.
%    TK_S: for each triangle in S, the indices of the 30, 60, 90 degree nodes.
%    TT_S: for each triangle in S, the triangle type, between -6 and -1, or 1 and 6.
%    WIJ_S: (I,J) coordinates of the nodes of S.
%
        [ tile_tri, tile_type, tile_xy ] = object_elements ( tile_w, tile_p );
%
%  Get tile (i,j) coordinates of 30 degree node of tile triangle #1.
%
        tile_node1 = tile_tri(1,1);
%
%  Key is the type of element #1.
%  We can only try to place this tile configuration starting at 
%  region elements of the same type as KEY.
%
        key = tile_type(1);
%
%  Consider triangle J in region R.
%
        for j = 1 : grid_element_num
%
%  Does it match the type of triangle #1 of S?
%
          if ( grid_type(j) == key )
%
%  Determine the translation that would map triangle #1 of S to triangle J of R.
%
            grid_node1 = grid_tri(j,1);
            tile_p(1,1:2) = grid_xy(grid_node1,1:2) - tile_xy(tile_node1,1:2);
%
%  Does this configuration remain in the region R?
%
            [ inside, element ] = region_contains_tile ( grid_w, grid_p, tile_w, tile_p );
%
%  The configuration constitutes a new variable.
%  Update the equation of each triangle of R covered by the configuration.
%  Update the constraint equation for the tile.
%
            if ( inside )

              var_num = var_num + 1;

              if ( x(var_num) == 1 )
                fprintf ( 1, '  Tile = %d, VAR = %d,', tile_index, var_num );
                fprintf ( 1, '  Rotate = %d, reflect = %d,', r, reflect );
                fprintf ( 1, '  type = %d  pij = (%d,%d)\n', key, tile_p );
                color = color + 1;
                t_tile_overlay_plot ( tile_w, tile_p, color );
              end

            end % end configuration remains in region.

          end % end key match

        end  % end element search

      end  % end reflection check

    end  % end rotation check

  end  % end tile check

  axis ( 'off' );
  axis ( 'equal' );

  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 't_solution_plot():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end
