开发者

Determining Cross Over of Numbers

开发者 https://www.devze.com 2023-02-01 12:09 出处:网络
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, h

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

精彩评论

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

关注公众号