def iou(a, b):
    """ Calculates intersection over union (IOU) over two tuples """
    (a_x1, a_y1), (a_x2, a_y2) = a
    (b_x1, b_y1), (b_x2, b_y2) = b
    a_area = (a_x2 - a_x1) * (a_y2 - a_y1)
    b_area = (b_x2 - b_x1) * (b_y2 - b_y1)
    
    dx = min(a_x2, b_x2) - max(a_x1, b_x1)
    dy = min(a_y2, b_y2) - max(a_y1, b_y1)
    if (dx>=0) and (dy>=0):
        overlap = dx * dy
        iou = overlap / (a_area + b_area - overlap)
        return iou
    return 0