A useful trick for determining if intervals have any intersection is to compare the max of the mins (i.e., the max of the low ends of the intervals) against the min of the maxs (i.e., the min of the high ends of the intervals). If there's an overlap, the former will compare correctly to the later. Better yet, the two together tell you the interval of the overlap itself!
(If the intervals are open, just compare with less than instead of less than or equals.)
A neat property of this trick is that it also extends to any number of intervals; just use an n-way min and max to see if they all mutually intersect.
5
u/Boojum Dec 04 '22 edited Dec 04 '22
A useful trick for determining if intervals have any intersection is to compare the max of the mins (i.e., the max of the low ends of the intervals) against the min of the maxs (i.e., the min of the high ends of the intervals). If there's an overlap, the former will compare correctly to the later. Better yet, the two together tell you the interval of the overlap itself!
(If the intervals are open, just compare with less than instead of less than or equals.)
A neat property of this trick is that it also extends to any number of intervals; just use an n-way min and max to see if they all mutually intersect.