score:4

this code can found all collisions:

``````double[] minus = new double[]
{
3, 24, 26, 23, 25, 18, 5,  5,  1, 10,
};
double[] plus = new ![alt text][1]double[]
{
3,  8,  9, 11, 22, 25, 5,  5,  3, 7
};

var collisionsbetweenindices =
enumerable.range(1, minus.length - 1)
.where(i => ((minus[i - 1] - plus[i - 1] < 0) && (minus[i] - plus[i] > 0)) ||
((minus[i - 1] - plus[i - 1] > 0) && (minus[i] - plus[i] < 0)) ||
((minus[i - 1] - plus[i - 1] == 0) && (minus[i] - plus[i] == 0)))
.toarray();

var collisionsonindices =
enumerable.range(0, minus.length)
.where(i => minus[i] == plus[i])
.toarray();

foreach (var idx in collisionsbetweenindices)
console.writeline("collision between {0} and {1}", idx - 1, idx);

foreach (var idx in collisionsonindices)
console.writeline("collision on {0}", idx);

// results:
// collision between 4 and 5
// collision between 6 and 7
// collision between 8 and 9
// collision on 0
// collision on 6
// collision on 7
``````

edit:

i did two different methods to distinguish the type of collisions (i.e. between indices or on indices), but if your purpouse is just to detect if there's a collision or not, just do the following:

``````var collisiondetected =
enumerable.range(0, minus.length).any(i =>
{
if (minus[i] == plus[i])
return true;
if (i > 0 &&
(((minus[i - 1] - plus[i - 1] < 0) && (minus[i] - plus[i] > 0)) ||
((minus[i - 1] - plus[i - 1] > 0) && (minus[i] - plus[i] < 0)) ||
((minus[i - 1] - plus[i - 1] == 0) && (minus[i] - plus[i] == 0))))
{
return true;
}
return false;
});
``````

this code returns as soon as a collision is found, so it's generally faster the the above methods.

score:1

why can't you just do:

``````for i=1...n
if minus[i] > plus[i]
return "crossed over at index i"
``````

score:1

``````public string determinecollisioninfo(double current, double next)
{
string currentinfo =
current == 0.0 ? "plus and minus have same value" :
current < 0.0 && next > 0.0 ? "intersection occurs" :
current > 0.0 && next < 0.0 ? "intersection occurs" :
"no intersection";

string nextinfo =
next > 0.0 ? "plus will be on top" :
next < 0.0 ? "minus will be on top" :
"plus and minus will have same value";

return currentinfo + ".  " + nextinfo;
}
``````

then, later:

``````ienumerable<double> differences = enumerable
.range(0, minus.length)
.select(i => plus[i] - minus[i]);

double current = differences.first();
ienumerable<string> analysis = differences
.skip(1)
.select(next =>
{
string result = determinecollisioninfo(current, next);
current = next;
return result;
});

foreach(string info in analysis)
{
console.writeline(analysis);
}
``````

score:1

if `minus` and `plus` are list:

``````var plus1 = plus.skip(1);
var retval = minus
.skip(1)
.select((p,i) => new { index = i, value = (minus[i] > plus[i]) != (p > plus1[i])})
.where( p => !p.value);
``````

score:3

one way would be to break each series up into line-segments, and then compare corresponding (by index) segments from the two series.

since you specifically mention linq, here's one way to achieve that. it's not very pretty:

``````var minuspairs = minus.zip(minus.skip(1), (prev, next) => new { prev, next });
var pluspairs = plus.zip(plus.skip(1), (prev, next) => new { prev, next });

var positions = minuspairs.zip
(pluspairs, (mpair, ppair) =>
mpair.prev > ppair.prev
&& mpair.next > ppair.next ? "minusabove" :
mpair.prev < ppair.prev
&& mpair.next < ppair.next ? "plusabove" :
"intersection");
``````

output:

``````minusabove
minusabove
minusabove
intersection
``````

(note that you don't get a `plusabove` for the last point because the only segment it is part of represents an `intersection`. you may need to change this behaviour if desirable.)

to be honest, i would shy away from any 'cute' solution if you need to do anything even slightly more complicated than this (e.g. finding the intersection points). good oo design is needed here.

score:4

to determine which is higher, just compare minus[i] to plus[i] - whichever has the greater value is the "higher" one at i.

to determine intersections, just keep track of which one is higher. when that changes, there was an intersection.

edit

if you can't track history, then:

``````if ((minus[i-1] > plus[i-1]) != (minus[i] > plus[i])) then
// there was an intersection
else
// there was not an intersection
``````