mathgraphnvd3.jslinechartapexcharts

Find intersection of given two lines each passing through more than two points


enter image description here

Challenge:

We have two lines.

Blue line passes through points : (20,100), (25,44.44), (30,30), (35,20), (40,0), (45,0), (50,0), (55,0), (60,0)

Pink line passes through points : (20,00), (25,0), (30,0), (35,0), (40,20), (45,33.33), (50,64.44), (55,100), (60,100)

Without any manual intervention, I want to know the point where they intersect. For example, the point of intersection as shown in figure is (37.5,10)

Remember: The only input of the program is set of points of two lines. Output needed is intersection point of both the line.


Solution

  • Things that did not work out:

    1. I tried to find library of Python that can generate intersection point of two lines(both passing through more than two points). Couldn't find it.

    2. I tried to find a mathematical equation generator for lines passing through more than two points. Then I thought I'll be able to find the intersection of those two equations. Got very complex with lots of polynomial equations!

    3. Most of the places I searched in internet were able to give me intersection of two straight lines that passed through two points only. However as seen in the above image, the both the line passes through about 9 points.

    Correct solution:

    Well, call it a bummer or any but the solution wasn't as complex as I thought. Now if you carefully look at the image, you can interpret it in this way:

    Instead of calling it a line that passes from lots of points and not just two points, call it a line that is passing through two points. Then it is passing through another two points. Then another two points.. so on..

    There are many solutions available online for finding intersection of lines that passes through 2 points. The problem here was only because of the interpretation that both the lines are passing through N number of points (N>2).

    Hence, the answer is calculated this way

    Step 1: Check for intersection of line1 with first two points [(20,100),(25,44.44)] and line2 with first two points[(20,0.0),(25,0.0)]

    Step 2: Check for intersection of line1 with next two points [(25,44.44),(30,30)] and line2 with first two points[(25,0.0),(30,0)]

    Step 3: Repeat this process for the whole loop of points till the last point is reached.

    Output: The program was able to yield (37.5,10) correctly!

    PS: I can share the code of this if need be.