Determining Cross Over of Numbers
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开发者_StackOverflow中文版 the array index, how could I tell if my two arrays had crossed at one point.
Thanks.
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.
精彩评论