1

Given the first line from a to b where a < b and the second line from x to y where x < y, how do you calculate the intersection length of those two?

Example:

a =0, b=5, x=3, y=7

012345
|----|
   |---|
   34567

the result would be 2 since they are intersection from 3 to 5.

Is there an expression with these 4 variables to extract the result? There is no guarantee that there is an intersection nor is it guaranteed that x > a

I have looked at timespan intersection examples but they all have an if expression in them which is not possible in my situation.

Isitar
  • 1,286
  • 12
  • 29

1 Answers1

2

Logic is quite simple:

if (y<a) or (x>b):
       return  no intersection

intersection.left = max(a, x)
intersectioni.right = min(b, y)
MBo
  • 77,366
  • 5
  • 53
  • 86
  • But this isnt an linear expression. – Isitar Oct 01 '18 at 06:34
  • What do you mean by "linear expression"? – MBo Oct 01 '18 at 06:37
  • a linear equation with these variables, something like ` is <= b` ` is <= y` ` ie >=a` ` ie >=x` ` i = ie - is` ` maximise(i)` so i can use it in an ilp formulation – Isitar Oct 01 '18 at 07:20
  • 1
    Problem statement does not assume maximisation and other kinds of optimization, it has single exact solution. – MBo Oct 01 '18 at 07:47