Skip to content

Instantly share code, notes, and snippets.

@cbzehner
Created March 20, 2018 01:15
Show Gist options
  • Save cbzehner/ddb62ab5d0b5ae9f667f9cd9924b4ac4 to your computer and use it in GitHub Desktop.
Save cbzehner/ddb62ab5d0b5ae9f667f9cd9924b4ac4 to your computer and use it in GitHub Desktop.
def find_love(r1, r2):
(x_start, x_end) = x_overlap(r1, r2)
(y_start, y_end) = y_overlap(r1, r2)
if x_start is None:
return "x_start there can be no love!"
if x_end is None:
return "x_end there can be no love!"
if y_start is None:
return "y_start there can be no love!"
if y_end is None:
return "y_end there can be no love!"
r3 = {
# coordinates of bottom-left corner
'left_x': x_start,
'bottom_y': y_start,
# width and height
'width': x_end - x_start,
'height': y_end - y_start,
}
return r3
def x_overlap(r1, r2):
# TODO: Handle case where the coordinates are flipped
r1_left = r1['left_x']
r1_right = r1['left_x'] + r1['width']
r2_left = r2['left_x']
r2_right = r2['left_x'] + r2['width']
overlaps = r1_right > r2_left
start = None
end = None
if overlaps:
start = max(r1_left, r2_left)
end = min(r1_right, r2_right)
return (start, end)
def y_overlap(r1, r2):
# TODO: Handle case where the coordinates are flipped
r1_bottom = r1['bottom_y'] # 5
r1_top = r1['bottom_y'] + r1['height'] # 15
r2_bottom = r2['bottom_y'] # 12
r2_top = r2['bottom_y'] + r2['height'] # 22
overlaps = r1_top > r2_bottom
start = None
end = None
if overlaps:
start = max(r1_bottom, r2_bottom)
end = min(r1_top, r2_top)
return (start, end)
r1 = {
# coordinates of bottom-left corner
'left_x': 1,
'bottom_y': 5,
# width and height
'width': 10,
'height': 10,
}
r2 = {
# coordinates of bottom-left corner
'left_x': 2,
'bottom_y': 12,
# width and height
'width': 4,
'height': 10,
}
r4 = {
# coordinates of bottom-left corner
'left_x': 2,
'bottom_y': 6,
# width and height
'width': 4,
'height': 2,
}
r5 = {
# coordinates of bottom-left corner
'left_x': 0,
'bottom_y': 6,
# width and height
'width': 1,
'height': 2,
}
print find_love(r1, r5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment