def rect_distance(rect1, rect2):
#print "starting rect_distance with x1,", x1, x2, y1b, y2b
key = (rect1.tostring(), rect2.tostring())
if key in rect_distance_cache:
#print "F",
return rect_distance_cache[key]
#print "_",
x1, y1, x1b, y1b = rect1
x2, y2, x2b, y2b = rect2
distance = None
left = x2b < x1
right = x1b < x2
bottom = y2b < y1
top = y1b < y2
if top and left:
distance = euclidean((x1, y1b), (x2b, y2))
elif left and bottom:
distance = euclidean((x1, y1), (x2b, y2b))
elif bottom and right:
distance = euclidean((x1b, y1), (x2, y2b))
elif right and top:
distance = euclidean((x1b, y1b), (x2, y2))
elif left:
distance = x1 - x2b
elif right:
distance = x2 - x1b
elif bottom:
distance = y1 - y2b
elif top:
distance = y2 - y1b
else: # rectangles intersect
distance = 0
rect_distance_cache[key] = distance
return distance
评论列表
文章目录