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
Source: stackoverflow.com
Related Query
- Determine 2 diagrams relative position using LINQ
- Convert string[] to int[] in one line of code using LINQ
- Determine if a sequence contains all elements of another sequence using Linq
- Get List<> element position in c# using LINQ
- Determine sequence contains no element using LINQ
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- LINQ Source Code Available
- Combine entries from two lists by position using LINQ
- Using linq to determine if any element is ListA exists in ListB?
- How can I write the following code more elegantly using LINQ query syntax?
- How can I code an outer join using LINQ and EF6?
- Determine the source DataContext for a Linq to Sql query
- C# .Net 3.5 Code to replace a file extension using LINQ
- Using Linq to determine if a record does not exist in a list but does exist in a table
- Trying to understand LINQ code using c#
- Retrieve bool result by using LinQ code
- Inserting particular XElement at specific position in a XML using LINQ
- creating Linq to sqlite dbml from DbLinq source code
- read icollection data using LINQ in C# code
- Linq sub query when using a repository pattern with EF code first
- To determine if one of the Strings from a list contains the initial part of a specified string using LINQ
- Using LINQ query result for data source for GridControl c#
- Determine Duplicate data using LINQ to EF
- How to get mismatch position using Linq or lambda operation on two string array
- How to flatten a multi level XML into a single level XML using c# code LINQ
- Using Linq to build a graph class; can you make this code look better?
- How to write this code using the Linq Extension Method-Syntax?
More Query from same tag
- Can I optimize this query any further?
- Odd behaviour change with var, dynamic and linq combination
- How to query using a string in LINQ
- Multiple-field GroupBy Linq query is giving same count() value for all records
- A more refined version of this LINQ to SQL query
- How do I order the elements in a group by linq query, and pick the first?
- Conditional selection in LINQ (select instead if empty)
- Refactor LINQ method to introduce Lamba function as a parameter
- using Identity User to filter datarecords
- Selecting a new type from linq query
- Newbie issue with LINQ in vb.net
- Check record state of a database entry
- An error occurred during the pre-login handshake
- Entity Framework - get records in multiple tables using stored procedure
- How to get the value of a node inside a large XDocument
- How to parse nested elements using LINQ to XML
- Get TOP5 records from each status using Linq C#
- LINQ Where clause throws casting exception from string to Guid even though comparison only contains Guids
- Trying to get a string concatenated in Linq2Entities query
- Sum equal data in DataTable and pass them to a new DataTable C #
- Copy list1 property to list2 based oncondition
- How do I get the descendants of a named node using Linq-to-XML?
- NHIbernate (3.1) - Linq group by then order by count issue
- How to handle null values with where clause in LINQ?
- Returning empty linq expression
- How to order a list of entities by a custom order?
- Parse Nested XML Attributes with Linq
- Is there a <NotIn> LINQ method?
- C#: LINQ not returning the same result as SQL
- LINQ using conditional where generates wrong query