Determine the relative position of two charts using LINQ

I have two arrays of points: double[] minusand double[] plus, for example:

double[] minus = new[]
{
    24.043414306636713,
    26.521399902043807,
    23.049167719142361,
    24.473177606966754,
    18.238281854192408,
};

double[] plus = new[]
{
    8.31219054269323,
    9.5909890877229582,
    11.066525870449567,
    22.769068312057193,
    24.733540360065991,
};

I need to build 2 diagrams based on this number and determine their positions relative to each other: is there an intersection, and which one is under the other?

How can i do this? TIA

(please feel free to ask a question or change the subject, I'm not sure about the correct mathematical terminology)

Edit: Here is an Excel chart:

http://i.stack.imgur.com/NEhcs.png

It is easy to determine what is above and below.

How to determine that red (plus) has an intersection with blue (minus)? Perhaps LINQ?

+3
6

:

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

alt text

EDIT:

, (.. ), , , , :

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;
});

, , .

+4

, , [i] [i] - , , "" i.

, , . , .

Edit

, :

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

- , ( ) .

LINQ, . :

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");

:

MinusAbove
MinusAbove
MinusAbove
Intersection

( , PlusAbove , , , Intersection. , , .)

, "" , - , (, ). OO.

+3

:

for i=1...n
    if minus[i] > plus[i]
         return "Crossed over at index i"
+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;
}

, :

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);
}
+1

minus plus - :

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);
+1

Source: https://habr.com/ru/post/1778368/


All Articles