# basic version def boundingBox(geometry): x, y = [], [] for point in geometry: x.append(point[0]) # X coordinate y.append(point[1]) # Y coordinate minX = min(x) maxX = max(x) minY = min(y) maxY = max(y) # only lower left and upper right corner coordinates are needed, they are also easy to remember – lower left corner has minimum X and Y coordinates, while upper right has maximum X and Y # another two good possibilities for bbox format: # [minX, minY, maxX, maxY] – boundaries, no point coordinates # [[minX, minY], width, height] – mainly useful outside of GIS return [[minX, minY], [maxX, maxY]] # short version – the same as previous function, just done in less code def boundingBox(geometry): x, y = [p[0] for p in geometry], [p[1] for p in geometry] return [[min(x), min(y)], [max(x), max(y)]] print boundingBox([[1, 0], [3, 4], [7, 2]]) # [[1, 0], [7, 4]] → lower left, upper right corner coordinates def bboxOverlap(bbox1, bbox2): # some variables only for reference, use this handy way of variable definition to get the values quickly [[minX1, minY1], [maxX1, maxY1]] = bbox1 [[minX2, minY2], [maxX2, maxY2]] = bbox2 # again, setting up conditions to describe bbox2 position relative to bbox1 – "left" means wether bbox2 is to the left from bbox2, etc. left = maxX2 < minX1 right = minX2 > maxX1 above = minY2 > maxY1 below = maxY2 < minY1 # if bbox2 is to the left, right, above or below bbox1, it means they don't overlap if (left or right or above or below): return False # if bbox2 is neither to the left, right, above or below bbox1, the opposite is true – bounding boxes do overlap! else: return True