function cplex_solution_plot ( x )

%*****************************************************************************80
%
%% cplex_solution_plot() plots a trinity solution generated by cplex().
%
%  Discussion:
%
%    This function assumes:
%    * there was an LP file generated by trinity_lp_automatic();
%    * the linear system defined by the LP file was solved by cplex();
%    * the binary solution vector has been extracted by cplex_solution_read().
%
%    This function then produces a plot of the tiling of the trinity region
%    as defined by the solution vector.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    27 May 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer X(142), the solution file, containing 4 1's.  Each 1 occurs
%    at an index which encodes the rotation, reflection and translation
%    of one of the trinity tiles.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'cplex_solution_plot():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  cplex_solution_plot() plots a trinity solution\n' );
  fprintf ( 1, '  that was generated by the cplex() optimization package.\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_num = 4;
%
%  Define the region R.
%    W_R: boundary word for R.
%    PIJ_R: (I,J) coordinates of 30-degree node of triangle #1 of R.
%
  [ w_r, pij_r ] = trinity_word ( );
  figure ( );
  region_grid_ij_plot ( w_r, pij_r );
  label = 'Trinity';
  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 ( w_r, pij_r );

  element_num = size ( tt_r, 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 = element_num + tile_num;
  var_num = 0;
  A = zeros ( eqn_num, var_num );
  b = ones ( eqn_num, 1 );
%
%  Loop over tiles.
%
  for s = 1 : tile_num
%
%  Start a constraint row for this tile.
%
    eqn_cons = element_num + s;
%
%  Consider all counterclockwise tile rotations that are multiples of 60 degrees:
%  0, 60, 120, 180, 240, 300:
%
    for r = 0 : 5
%
%  Consider a tile or its reflection.
%
      for reflect = 0 : 1
%
%  Get a fresh copy of the word and base point.
%
        [ w_s, pij_s ] = trinity_tile_word ( s );
%
%  Perform R counterclockwise rotations of 60 degrees.
%
        [ w_s, pij_s ] = word_rotate_ij ( w_s, pij_s, r );
%
%  Perform REFLECT reflections across X axis.
%
        if ( reflect == 1 )
          [ w_s, pij_s ] = word_reflect_ij ( w_s, pij_s );
        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.
%
        [ tk_s, tt_s, wij_s ] = triangle_k ( w_s, pij_s );
%
%  Get tile (i,j) coordinates of 30 degree node of tile triangle #1.
%
        jack_s = tk_s(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 = tt_s(1);

%       fprintf ( 1, '  Tile = %d, Rotate = %d, reflect = %d, element #1 type = %d\n', ...
%         s, r, reflect, key );
%
%  Consider triangle J in region R.
%
        for j = 1 : element_num
%
%  Does it match the type of triangle #1 of S?
%
          if ( tt_r(j) == key )
%
%  Determine the translation that would map triangle #1 of S to triangle J of R.
%
            jack_r = tk_r(j,1);
            pij_s = wij_r(jack_r,1:2) - wij_s(jack_s,1:2);
%
%  Does this configuration remain in the region R?
%
            [ inside, element ] = region_contains_tile ( w_r, pij_r, w_s, pij_s );
%
%  The configuration constitutes a new variable.
%  Update the equation of each triangle of R that is covered by the configuration.
%  Update the constraint equation for the tile.
%
            if ( inside )

              var_num = var_num + 1;

            fprintf ( 1, '  Tile = %d, V = %d, Rotate = %d, reflect = %d, element #1 type = %d  pij = (%d,%d)\n', ...
          s, var_num, r, reflect, key, pij_s );

              if ( x(var_num) == 1 )
                fprintf ( 1, '  Configuration %d was used in the solution.\n', var_num );
                color = color + 1;
                tile_overlay_ij_plot ( w_s, pij_s, color );
                label2 = sprintf ( '+ V%d', var_num );
                label = [ label, label2 ];
                title ( label );
              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

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

  return
end
