Trying to build a function that will return the total overlapping distance between 2 line segments, denoted by start and end ints.
Currently I have this: That I got off the internet somewhere,
def overlap(min1, max1, min2, max2):
"""returns the overlap between two lines that are 1D"""
result = None
if min1 >= max2 or min2 >= max1: result = 0
elif min1 <= min2:
result = max1 - min2
else: result = max2 - min1
return result
This works however for the case of 0 100, 0,20 it returns 100. And that's clearly wrong. Is there a simple way of calculating this that will return the correct values?