Question

Lets say I have this data set...

var a = [5,6,7]; var b = [9,8,6];

Imagine that those values were plotted the y in a (x,y) coordinate pair, and the x was the array index, how could I tell if my two arrays had crossed at one point.

Thanks.

Was it helpful?

Solution

Try this:

if ((a[0] < b[0]) == (a[1] > b[1]) ||
    (a[1] < b[1]) == (a[2] > b[2]))
{
    // crossed
}

The important point is that for some index i the values of a[i] is (greater|less) than b[i], and the relationship between a[i + 1] and b[i + 1] is the opposite.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top