Skip to content

Instantly share code, notes, and snippets.

@kayibal
Created June 26, 2019 11:38
Show Gist options
  • Save kayibal/8b488c41fd6ece1344cf55d8f650f600 to your computer and use it in GitHub Desktop.
Save kayibal/8b488c41fd6ece1344cf55d8f650f600 to your computer and use it in GitHub Desktop.
Computes overlap between two ranges
import numpy as np
def compute_overlap(x1, x2, y1, y2):
if x2 >= y1 and y2 >= x1:
if x1 < y1 < x2 and x2 < y2:
return x2 - y1
elif y1 < x1 < y2 and y2 < x2:
return y2 - x1
elif x1 < y1 and y2 < x2:
return y2 - y1
elif y1 < x1 and x2 < y2:
return x2 - x1
else:
return np.nan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment