#!/usr/bin/python
import random
from Numeric import *
#-----------------------------------------------------------
def main():
"""Create Grid Patern"""
img_cols = 4
img_rows = 4
grid_cols = 20
grid_rows = 7
grid = zeros( ( grid_rows, grid_cols ) )
print grid
x = 0
y = 0
while 1:
shape_cols = random.randint( 1, img_cols )
if shape_cols == 1:
shape_rows = random.randint( 2, img_rows )
else:
shape_rows = random.randint( 1, img_rows )
x, y = add_img(
grid, x, y, shape_cols, shape_rows )
if ( y >= grid_rows ):
break
fill_remaining_zeroes( grid )
print "\n-- Final Pattern --\n"
print grid
#-----------------------------------------------------------
def fill_remaining_zeroes( grid ):
"""Fill Remaining Zeroes with 1 x 1"""
for row, l in enumerate( grid ):
for col, i in enumerate( l ):
if i == 0:
grid[ row, col ] = 11
#-----------------------------------------------------------
def add_img( grid, x, y, shape_cols, shape_rows ):
"""Add image to the Grid"""
grid_rows, grid_cols = shape(grid)
while 1:
# guard
if shape_cols < 1 \
or shape_rows < 1:
return grid_cols, grid_rows
id = int( str( shape_cols ) + str( shape_rows ) )
print id
x2 = x + shape_cols
y2 = y + shape_rows
if x2 > grid_cols:
# reduce columns by 1
shape_cols -= 1
if y2 > grid_rows:
# reduce rows by 1
shape_rows -= 1
if x2 > grid_cols or y2 > grid_rows:
# shape was too large
continue
if sum( sum( grid[ y : y2, x : x2 ] ) ) == 0:
# valid shape
grid[ y : y2, x : x2 ] = id
print grid
# find next zero coord
y2 = grid_rows
for row, l in enumerate( grid ):
for col, i in enumerate( l ):
if i == 0:
y2 = row
x2 = col
return x2, y2
if shape_cols > 1:
shape_cols -= 1
else:
shape_rows -= 1
#-----------------------------------------------------------
if __name__ == "__main__":
main()
#-----------------------------------------------------------