Leon
Leon

Reputation: 339

Intersection of 2 lines - numerical analysis

I have 2 two-dimensional arrays that represent 2 lines/curves, something like:

(y1,t1) (y2,t2) ... (yN, tN)

and

(Y1, t1) (Y2, t2) ... (YN, tN)

My goal is to find the last point from the beginning of the array just before the intersection of 2 lines/curves.

I tried comparing

if ( (y1 > Y1 && y2 < Y2) || (y1 < Y1 && y2 > Y2) || (y1 == Y2) )

in order t0 find intersection but it does not work or verifying result is very difficult due to large number of points.

Is there nay elegant solution in C# using open source numerical analysis library that all I need to do is feed the 2 arrays into the function and get intersection?

Upvotes: 0

Views: 188

Answers (1)

Yaroslav Bres
Yaroslav Bres

Reputation: 1327

On c# your algorithm looks like:

public void FindFirstIntersection(int[,] curve1, int[,] curve2)
    {
        for (var i = 0; i < curve1.Length - 1; i++)
        {
            var curve1Point = curve1[i, 0];
            var curve2Point = curve2[i, 0];

            var curve1NextPoint = curve1[i + 1, 0];
            var curve2NextPoint = curve2[i + 1, 0];

            if (curve1Point > curve2Point && curve1NextPoint < curve2NextPoint ||
                curve1Point < curve2Point && curve1NextPoint > curve2NextPoint ||
                curve1Point == curve2Point)
            {
                //Do smth
            }
        }
    }

Upvotes: 1

Related Questions