Why don't you just iterate through the list with four counters: min_x
, min_y
, max_x
, and max_y
def bounding_box(coords):
min_x = 100000 # start with something much higher than expected min
min_y = 100000
max_x = -100000 # start with something much lower than expected max
max_y = -100000
for item in coords:
if item[0] < min_x:
min_x = item[0]
if item[0] > max_x:
max_x = item[0]
if item[1] < min_y:
min_y = item[1]
if item[1] > max_y:
max_y = item[1]
return [(min_x,min_y),(max_x,min_y),(max_x,max_y),(min_x,max_y)]
Using your example input:
bounding_box([(6, 7), (2, 4), (8, 9), (3, 7), (5, 4), (9, 9)])
>> [(2, 4), (9, 4), (9, 9), (2, 9)]