Does this achieve what you are trying to do?
With my code you have to define the center and max height and width.
import numpy as np
def pad_array(original_array, center_coordinates, max_width, max_height):
w1, h1 = original_array.shape
cx, cy = center_coordinates
# calculate padding on each side first
pad_left = max(0, max_width // 2 - cx)
pad_right = max(0, max_width - pad_left - w1)
pad_top = max(0, max_height // 2 - cy)
pad_bottom = max(0, max_height - pad_top - h1)
# then pad the original array
padded_array = np.pad(original_array, ((pad_top, pad_bottom), (pad_left, pad_right)), mode="constant", constant_values=-1)
return padded_array
original_array = np.array([[-1, 0, 5], [0, 0, 0], [5, 5, 5]])
center_coordinates = (0, 0)
max_width = 5
max_height = 5
padded_array = pad_array(original_array, center_coordinates, max_width, max_height)
print(padded_array)
center_coordinates = (1, 1)
max_width = 5
max_height = 5
padded_array = pad_array(original_array, center_coordinates, max_width, max_height)
print(padded_array)
My outputs for center 0,0 and 1,1 are as follows:
[[-1 -1 -1 -1 -1]
[-1 -1 -1 -1 -1]
[-1 -1 -1 0 5]
[-1 -1 0 0 0]
[-1 -1 5 5 5]]
[[-1 -1 -1 -1 -1]
[-1 -1 0 5 -1]
[-1 0 0 0 -1]
[-1 5 5 5 -1]
[-1 -1 -1 -1 -1]]
You can always make the width and height to the share the variable like size as well like this
import numpy as np
def pad_array(original_array, center_coordinates, max_size):
w1, h1 = original_array.shape
cx, cy = center_coordinates
# calculate padding on each side first
pad_left = max(0, max_size // 2 - cx)
pad_right = max(0, max_size - pad_left - w1)
pad_top = max(0, max_size // 2 - cy)
pad_bottom = max(0, max_size - pad_top - h1)
# then pad the original array
padded_array = np.pad(original_array, ((pad_top, pad_bottom), (pad_left, pad_right)), mode="constant", constant_values=-1)
return padded_array
original_array = np.array([[-1, 0, 5], [0, 0, 0], [5, 5, 5]])
center_coordinates = (0, 0)
max_size = 5
padded_array = pad_array(original_array, center_coordinates, max_size)
print(padded_array)
Then you can just get your agent to change the center. Just leave the max_size as a constant 5 like this.
original_array = np.array([[-1, 0, 5], [0, 0, 0], [5, 5, 5]])
center_coordinates = (0, 0)
padded_array = pad_array(original_array, center_coordinates, 5)