开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜