Skip to content

Instantly share code, notes, and snippets.

@kaspermunch
Last active April 11, 2020 12:23
Show Gist options
  • Save kaspermunch/8b1fb397c6a06fe57c717ca774074642 to your computer and use it in GitHub Desktop.
Save kaspermunch/8b1fb397c6a06fe57c717ca774074642 to your computer and use it in GitHub Desktop.
Test if two half-open intervals overlap using two comparisons instead of eight.
overlap = (start1 <= start2 < end1 or
start1 <= end2 < end1 or
start2 <= start1 < end2 or
start2 <= end1 < end2)
# same as:
overlap = not (end1 <= start2 or end2 <= start1)
# same as:
overlap = not (end1 <= start2) and not (end2 <= start1)
# same as (by De Morgan's laws):
overlap = end1 > start2 and end2 > start1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment