Two or more elements overlap when they partially or totally cover one another.
Two or more elements overlap when they partially or totally cover one another.
The way to find if the elements overlap or not is to test if one elements begins before the second one ends, while the second one begins before the first one ends.
For example, here are all of the ways two lines can overlap:
1.
s1|--------|e1
s2|--------|e2
2.
s1|-------|e1
s2|--------|e2
3.
s1|--------|e1
s2|--------|e2
4.
s1|-------------------|e1
s2|--------|e2
5.
s1|--------|e1
s2|-------------------|e2
Note that s1
is always smaller than e2
, while s2
is always smaller than e1
.
This is not the case when the two lines do not overlap:
1.
s1|--------|e1
s2|--------|e2
2.
s1|--------|e1
s2|--------|e2
Note that either s1
is bigger then e2
or s2
is bigger then e1
.
The actual data type of the elements is completely irrelevant as long as it's comparable.
Therefore, to check if two elements overlap each other, all you need to do is this: (pseudo code)
If a.Start < b.End AND b.Start < a.End Then
Overlap
Else
No overlap