#! /usr/bin/env python3 # def cell_ij_fill ( m, i, j, color ): #*****************************************************************************80 # ## cell_ij_fill() plots a filled (I,J) cell. # # Discussion: # # We assume the data is represented in a matrix. # # In order to convert between the matrix coordinates and picture # coordinates, the (I,J) cell will be drawn with the following corners: # # (j-1,m-i+1), (j,m-i+1), (j,m-i), (j-1,m-1). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 January 2022 # # Author: # # John Burkardt # # Input: # # integer M, the maximum row index. # # integer I, J, the index of the cell. # # color COLOR, can be any of the 8 abbreviated color terms # 'r', 'g', 'b', 'c', 'm', 'y', 'w', 'k', or an RGB triple such as # [1.0,0.4,0.0]. The square is filled with this color. # import matplotlib.pyplot as plt a = j - 1 b = j c = m - ( i - 1 ) d = m - i plt.fill ( [ a, b, b, a ], [ c, c, d, d ], color ) return def cell_ij_fill_test ( ): #*****************************************************************************80 # ## cell_ij_fill_test() tests cell_ij_fill(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 January 2022 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'cell_ij_fill_test():' ) print ( ' cell_ij_fill() fills in unit cells indexed by (I,J)' ) print ( ' using matrix coordinate system.' ) mario = np.array ( [ \ [ 0, 0, 0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0 ], \ [ 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0 ], \ [ 0, 0, 6, 6, 6, 5, 5, 5, 1, 5, 0, 0, 0 ], \ [ 0, 6, 5, 6, 5, 5, 5, 5, 1, 5, 5, 5, 0 ], \ [ 0, 6, 5, 6, 6, 5, 5, 5, 5, 1, 5, 5, 5 ], \ [ 0, 6, 6, 5, 5, 5, 5, 5, 1, 1, 1, 1, 0 ], \ [ 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0 ], \ [ 0, 0, 2, 2, 3, 2, 2, 2, 2, 0, 0, 0, 0 ], \ [ 0, 2, 2, 2, 3, 2, 2, 3, 2, 2, 2, 0, 0 ], \ [ 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 0 ], \ [ 5, 5, 2, 3, 4, 3, 3, 4, 3, 2, 5, 5, 0 ], \ [ 5, 5, 5, 3, 3, 3, 3, 3, 3, 5, 5, 5, 0 ], \ [ 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 0 ], \ [ 0, 0, 3, 3, 3, 0, 0, 3, 3, 3, 0, 0, 0 ], \ [ 0, 6, 6, 6, 0, 0, 0, 0, 6, 6, 6, 0, 0 ], \ [ 6, 6, 6, 6, 0, 0, 0, 0, 6, 6, 6, 6, 0 ] ] ) dims = mario.shape m = dims[0] n = dims[1] # # 0: white # 1: black # 2: red # 3: blue # 4: yellow # 5: beige # 6: brown # plt.axis ( 'equal' ) plt.axis ( 'off' ) for i in range ( 0, m ): for j in range ( 0, n ): k = mario[i,j] # # Despite documentation assuring me it was possible, I could not seem to use # numeric RGB triples for colors. # if ( k == 0 ): color = 'white' elif ( k == 1 ): color = 'black' elif ( k == 2 ): color = 'red' elif ( k == 3 ): color = 'blue' elif ( k == 4 ): color = 'yellow' elif ( k == 5 ): color = 'bisque' elif ( k == 6 ): color = 'brown' cell_ij_fill ( m, i, j, color ) filename = 'cell_ij_fill_test.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( block = False ) plt.close ( ) return def pentomino_display ( p, label ): #*****************************************************************************80 # ## pentomino_display() displays a particular pentomino in a 5x5 grid. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 January 2022 # # Author: # # John Burkardt # # Input: # # integer P(P_M,P_N), a matrix of 0's and 1's. # 1 <= P_M, P_N <= 5. There should be exactly 5 values of one. # # string LABEL, a title for the plot. # import matplotlib.pyplot as plt import numpy as np # # The background grid. # grid_m = 5 grid_n = 5 grid = np.zeros ( [ grid_m, grid_n ] ) # # Place the pentomino on the grid, so that it is "snug" in the upper left corner. # dims = p.shape p_m = dims[0] p_n = dims[1] grid[0:p_m,0:p_n] = p[0:p_m,0:p_n] # # Display each square of the grid. # plt.axis ( 'equal' ) plt.axis ( 'off' ) for i in range ( 0, grid_m ): for j in range ( 0, grid_n ): k = grid[i,j] if ( k == 0 ): color = 'white' elif ( k == 1 ): color = 'black' cell_ij_fill ( grid_m, i, j, color ) filename = label + '.png' print ( ' Graphics saved as "' + filename + '"' ) plt.savefig ( filename ) plt.show ( block = False ) plt.close ( ) return def pentomino_display_test ( ): #*****************************************************************************80 # ## pentomino_display_test() tests pentomino_display(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 January 2022 # # Author: # # John Burkardt # print ( '' ) print ( 'pentomino_display_test():' ) print ( ' pentomino_display() displays a picture of a pentomino.' ) print ( '' ) for index in range ( 0, 12 ): name = pentomino_name ( index ) p = pentomino_matrix ( name ) pentomino_display ( p, name ) return def pentomino_matrix ( name ): #*****************************************************************************80 # ## pentomino_matrix() returns a 0/1 matrix defining a particular pentomino. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 January 2022 # # Author: # # John Burkardt # # Input: # # character NAME, is f, i, l, n, p, t, u, v, w, x, y or z. # # Output: # # integer P(P_M,P_N), a matrix of 0's and 1's that indicates # the shape of the pentomino. # import numpy as np if ( name.lower ( ) == 'f' ): p = np.array ( [ \ [ 0, 1, 1 ], \ [ 1, 1, 0 ], \ [ 0, 1, 0 ] ] ) elif ( name.lower ( ) == 'i' ): p = np.array ( [ \ [ 1 ], \ [ 1 ], \ [ 1 ], \ [ 1 ], \ [ 1 ] ] ) elif ( name.lower ( ) == 'l' ): p = np.array ( [ \ [ 1, 0 ], \ [ 1, 0 ], \ [ 1, 0 ], \ [ 1, 1 ] ] ) elif ( name.lower ( ) == 'n' ): p = np.array ( [ \ [ 1, 1, 0, 0 ], \ [ 0, 1, 1, 1 ] ] ) elif ( name.lower ( ) == 'p' ): p = np.array ( [ \ [ 1, 1 ], \ [ 1, 1 ], \ [ 1, 0 ] ] ) elif ( name.lower ( ) == 't' ): p = np.array ( [ \ [ 1, 1, 1 ], \ [ 0, 1, 0 ], \ [ 0, 1, 0 ] ] ) elif ( name.lower ( ) == 'u' ): p = np.array ( [ \ [ 1, 0, 1 ], \ [ 1, 1, 1 ] ] ) elif ( name.lower ( ) == 'v' ): p = np.array ( [ \ [ 1, 0, 0 ], \ [ 1, 0, 0 ], \ [ 1, 1, 1 ] ] ) elif ( name.lower ( ) == 'w' ): p = np.array ( [ \ [ 1, 0, 0 ], \ [ 1, 1, 0 ], \ [ 0, 1, 1 ] ] ) elif ( name.lower ( ) == 'x' ): p = np.array ( [ \ [ 0, 1, 0 ], \ [ 1, 1, 1 ], \ [ 0, 1, 0 ] ] ) elif ( name.lower ( ) == 'y' ): p = np.array ( [ \ [ 0, 0, 1, 0 ], \ [ 1, 1, 1, 1 ] ] ) elif ( name.lower ( ) == 'z' ): p = np.array ( [ \ [ 1, 1, 0 ], \ [ 0, 1, 0 ], \ [ 0, 1, 1 ] ] ) else: print ( '' ) print ( 'pentomino_matrix(): Fatal error!' ) print ( ' Illegal name = "%s"' % ( name ) ) print ( ' Legal names: f, i, l, n, p, t, u, v, w, x, y, z.' ) raise Exception ( 'pentomino_matrix(): Fatal error!' ) return p def pentomino_matrix_test ( ): #*****************************************************************************80 # ## pentomino_matrix_test() tests pentomino_matrix(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 January 2022 # # Author: # # John Burkardt # print ( '' ) print ( 'pentomino_matrix_test():' ) print ( ' pentomino_matrix() returns a 0/1 matrix representing a pentomino.' ) for index in range ( 0, 12 ): name = pentomino_name ( index ) p = pentomino_matrix ( name ) print ( '' ) print ( ' #%d %s' % ( index, name ) ) print ( p ) return def pentomino_name ( index ): #*****************************************************************************80 # ## pentomino_name() returns the name of a pentomino given its index. # # Discussion: # # There is a standard name for each pentomino, suggested by its shape. # We list them in alphabetical order: # # 1 F # 2 I # 3 L # 4 N # 5 P # 6 T # 7 U # 8 V # 9 W # 10 X # 11 Y # 12 Z # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 January 2022 # # Author: # # John Burkardt # # Input: # # integer INDEX: the pentomino index. # 0 <= INDEX < 12. # # Output: # # character NAME: the corresponding name. # import numpy as np pentomino_name = np.array ( \ [ 'F', 'I', 'L', 'N', 'P', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ] ) if ( 0 <= index and index < 12 ): name = pentomino_name[index] else: name = '?' return name def pentomino_name_test ( ): #*****************************************************************************80 # ## pentomino_name_test() tests pentomino_name(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 January 2022 # # Author: # # John Burkardt # print ( '' ) print ( 'pentomino_name_test():' ) print ( ' pentomino_name(i) returns the "name" of a pentomino #index,' ) print ( ' for 0 <= index < 12.' ) print ( '' ) print ( ' Index Name' ) print ( '' ) for index in range ( 0, 13 ): name = pentomino_name ( index ) print ( ' #%d %s' % ( index, name ) ) return def pentomino_pack ( ): #*****************************************************************************80 # ## pentomino_pack() "packs" all 12 pentominoes into a 5x4x12 array. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 January 2022 # # Author: # # John Burkardt # # Output: # # integer PACK(5,4,12), an array containing the 12 pentominoes. # import numpy as np pack = np.zeros ( [ 5, 4, 12 ] ) for index in range ( 0, 12 ): name = pentomino_name ( index ) p = pentomino_matrix ( name ) p_m, p_n = np.shape ( p ) pack[0:p_m,0:p_n,index] = p[0:p_m,0:p_n] return pack def pentomino_pack_test ( ): #*****************************************************************************80 # ## pentomino_pack_test() tests pentomino_pack(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 January 2022 # # Author: # # John Burkardt # print ( '' ) print ( 'pentomino_pack_test():' ) print ( ' pentomino_pack() packs all 12 pentominoes into a 5x4x12 array.' ) pack = pentomino_pack ( ) print ( '' ) for index in range ( 0, 12 ): name = pentomino_name ( index ) print ( ' #%d: pentomino "%s"' % ( index, name ) ) print ( pack[:,:,index] ) return def pentomino_test ( ): #*****************************************************************************80 # ## pentomino_test() tests pentomino(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 January 2022 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'pentomino_test():' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test pentomino()' ) cell_ij_fill_test ( ) pentomino_display_test ( ) pentomino_matrix_test ( ) pentomino_name_test ( ) pentomino_pack_test ( ) # # Terminate. # print ( '' ) print ( 'pentomino_test():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) pentomino_test ( ) timestamp ( )