def rectangle_intersection_area(rect1, rect2):
x1, y1, w1, h1 = rect1
x2, y2, w2, h2 = rect2
x_overlap = max(0, min(x1 + w1, x2 + w2) - max(x1, x2))
y_overlap = max(0, min(y1 + h1, y2 + h2) - max(y1, y2))
return x_overlap * y_overlap
def dice_coefficient(rect1, rect2):
intersection_area = rectangle_intersection_area(rect1, rect2)
rect1_area = rect1[2] * rect1[3]
rect2_area = rect2[2] * rect2[3]
return (2 * intersection_area) / (rect1_area + rect2_area)
def highest_dice_coefficient(reference_rect, rectangles):
highest_dice = 0
for rect in rectangles:
dice = dice_coefficient(reference_rect, rect)
if dice > highest_dice:
highest_dice = dice
return highest_dice
# Example usage:
reference_rectangle = (50, 50, 100, 100)
rectangles = [
(60, 60, 80, 80),
(100, 100, 150, 150),
(10, 10, 30, 30),
]
highest_dice = highest_dice_coefficient(reference_rectangle, rectangles)
print("Highest Dice Coefficient:", highest_dice)