Re: interval intersection



bob@xxxxxxxxxxxxxx (26 Oct 2005 22:17:22 -0700):
> what is the fastest way to find the intersection of two 1-dimensional
> intervals?
>
> here's an example:
>
> interval 1: 0 to 50
> interval 2: 25 to 80
>
> answer: 25 to 50

Pseudo-code generating intersection [x, y] of [a, b] and [c, d]:

x = max(a, c);
y = min(b, d);
if (y < x) return NO_INTERSECTION;

This code assumes that b >= a and d >= c. Graphical explanation:

a----------b
c------------d
x------y

There is an intersection (y >= x).

a-------b
c--------d
y--x

There is no intersection (y < x).

Regards.


-----
Public key "Ertugrul Soeylemez <never@xxxxxxxxxxxxxx>" (id: CE402012)
Fingerprint: 0F12 0912 DFC8 2FC5 E2B8 A23E 6BAC 998E CE40 2012

HKP: hkp://subkeys.pgp.net/
LDAP: ldap://keyserver.pgp.com/
HTTP: http://www.keyserver.de/

Attachment:pgpzibF6AB33u.pgp
Description: PGP signature