Last active
April 11, 2020 12:23
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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