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 the array index, how could I tell if my two arrays had crossed at one point.

Thanks.

Answers


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.


Need Your Help

ng-repeat only objects with specific property value - custom filter?

angularjs angularjs-ng-repeat

So say I have a JSON object 'user' with some basic properties like: 'name', 'address', 'role', etc.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.