Does 2 one-dimensional intervals touch?

65 Views Asked by At

As the title mentions I have 2 one-dimensional intervals given like so:

$[a, a-b]$
$[x, x-y]$

where $a$ and $x$ are the start points, and $b$ and $y$ are the length of the intervals.

The intervals are given in integers (and within the interval 0-9). What I need to find out is the best way to making sure $[x, x-y]$ does not even touch $[a, a-b]$. This includes the integers at both ends which implies that:

$$a\neq x$$ $$a\neq x-y$$ $$x\neq a-b$$ $$x-y\neq a-b$$

A graphical way of showing it:
Should be true Good intervals

Should be false Bad intervals

1

There are 1 best solutions below

0
On BEST ANSWER

I will use the notation $[a,b]$ for the interval that includes all the numbers between $a$ and $b$, inclusive. (I think you are using $[a, a-b]$ for the same thing.)

It's easy to see that if the intervals $[a,b]$ and $[x,y]$ do not overlap then one of them must be entirely to the left of the other. For this to happen, one must have either $b < x$ (if the first interval is on the left) or $y < a$ (if the second is on the left).

So the intervals do not overlap if

$$b < x\text{ or } y < a$$

and therefore they do overlap if

$$b \ge x \text { and } y \ge a.$$