Finding Intersection Point of Two Lines Using Python

IntersectionPointFeaturedImage

In Linear Algebra, two lines are said to intersect at only one point if they are not equal or parallel. The single point of intersection is also called as the solution to two linear equations. Python can be used to find the solution of these two linear equations. The lines can be represented in many different formats. A solution to X and Y can be derived in each format. A Python function can be described wherein X and Y can be directly found using the formulae derived for each format. This article highlights two different formats of Linear Equation and how to implement a function to derive the same.

To find the Intersection of a point using two lines, the manual method goes forward with putting both the equations to be equal on one variable. It creates the equation into a single variable equation. Further on, the single variable is found using simple mathematical calculations, and its value is put in either one of the previous equation to get the second coordinate of the intersection point. It is well illustrated in the article below and will be clarified with examples and derivations.

The two formats of linear equations carried out in this article are as follows:

Format 1: y=mx+c

Format 2: ay=bx+c

It should be noted that this article does not use NumPy to implement the finding of intersection point of two lines. Here a class has been implemented individually, and a function to work upon that class. Please refer this article to learn more about NumPy Linear Algebra.

To make the topic easy to understand, graphical analysis is also used. To implement the entire code as it is, it is necessary to import Matplotlib.pyplot. It can be done as follows, before the code begins:

import matplotlib.pyplot as plt

Finding Point of Intersection With Line Equation as y=mx+c

This is the most basic format of Line Equations. It is most frequently used in Linear Algebra and Graphical Analysis. Here the m is the slope of the Equation while the c is Constant of the equation. Here two linear equations in the same format will be considered. First, the derivation of X and Y would be done. On the basis of the formulae derived, a function will be created in Python. The derivation is as follows:

First Equation In Form Of Mxc
First Equation In Form Of y=mx+c
Second Equation In Form Of Mxc
Second Equation In Form Of y=mx+c
Derviation Of X And Y From Mxc Line Equation
Derivation Of X And Y From y=mx+c Line Equation

From the above derivation, it can be seen that x variable can be easily found from two linear equations using the formula:

x=(c1-c2)/(m2-m1)

The above formula can be easily coded into a Python function. But before creating a function, it is necessary to create a Line class which encapsulates the Line in equation y=mx+c . It is demonstrated below:

class Line:
  def __init__(self,slope,const):
    self.m=slope
    self.c=const

In the above class declaration, there is also a Constructor described using the __init__() function. It takes two inputs, the equation’s slope and the equation’s constant. The class has two variables, m and c. The m variable is assigned value of the slope, and the c variable is assigned value of the constant. Now that a class has been declared, a function can be defined which takes two Lines as inputs and shows their point of intersection as output. It is demonstrated in code below:

def findSolution(L1,L2):
  x=(L1.c-L2.c)/(L2.m-L1.m)
  y=L1.m*x+L1.c
  X=[x for x in range(-10,11)]
  Y1=[(L1.m*x)+L1.c for x in X]
  Y2=[(L2.m*x)+L2.c for x in X]
  plt.plot(X,Y1,'-r',label=f'y={L1.m}x+{L1.c}')
  plt.plot(X,Y2,'-b',label=f'y={L2.m}x+{L2.c}')
  plt.legend(loc='upper right')
  plt.show()
  return (x,y)

In the above function findSolution() takes two lines as input. It first assigns the x variable according to the formula derived above. The dot (‘.’) operator in Python allows the class object to access its public variables, in this case m and c. After finding x variable, y variable is simply assigned by putting the x variable in the equation of the first line. The rest of the code plots a line graph of the two equations in the range (-10,10). This article clearly explains on how to plot the legends in the graph and different colors for different lines in the same graph. The function returns a tuple with two values, the X and Y coordinate of the point of intersection. Now, lets check the working of the function on two lines with equations y=3x+5 and y=2x+3, respectively.

L1=Line(3,5) #Equation of line y=3x+5
L2=Line(2,3) #Equation of line y=2x+3
sol=findSolution(L1,L2)
print(sol)

The following output is generated on running the above code snippet:

Output Of Function Finding Solution With Ymxc
Output Of Function Finding Solution With y=mx+c

The function above tells that the coordinates (-2,-1) is where the two lines intersect. It can be checked by putting the values in the equations. The above graph also proves the function, as it is where the two graphs intersect.

Finding Point of Intersection With Line Equation as ay=bx+c

This format of Linear Equations is generally used in Linear Algebra. Here the y-variable has a coefficient as well. In this case the slope of the Equation becomes b/a, and the constant becomes c/a . These values can be put in the formula of x derived earlier, or a new derivation can be held out for this format of equations. The new derivation would be as follows:

First Equation In Form Of Aybxc
First Equation In Form Of ay=bx+c
Second Equation In Form Of Aybxc
Second Equation In Form Of ay=bx+c
Derivation Of X And Y From Equation In Form Of Aybxc
Derivation Of X And Y From Equation In Form Of ay=bx+c

The above received formula for x, it can be easily coded in Python function. But the same class definition used for earlier format will not work for this format. A new class declaration is required. It is shown in the code snippet below:

class Line:
  def __init__(self,ycoeff,xcoeff,const):
    self.a=ycoeff
    self.b=xcoeff
    self.c=const

This implementation of Class Line differs from the above given implementation. Here there are three input parameters in the constructor function. The first parameter is y-coefficient or ‘a’ in the Equation format. The second is x-coefficient or ‘b’ in the Equation format. The last is the constant or ‘c’ in the Equation format. The class consists of three variable a,b and c to form the equation of format ay=bx+c

The following function findSolution() finds a solution to two linear equations of the ay=bx+c format by using formula for x derivation and putting value of x in one linear equation to find y. It is demonstrated in the code below:

def findSolution(L1,L2):
  x=((L1.a*L2.c)-(L2.a*L1.c))/((L2.a*L1.b)-(L1.a*L2.b))
  y=(L1.b*x+L1.c)/L1.a
  X=[x for x in range(-10,11)]
  Y1=[((L1.b*x)+L1.c)/(L1.a) for x in X]
  Y2=[((L2.b*x)+L2.c)/(L2.a) for x in X]
  plt.plot(X,Y1,'-r',label=f'{L1.a}y={L1.b}x+{L1.c}')
  plt.plot(X,Y2,'-b',label=f'{L2.a}y={L2.b}x+{L2.c}')
  plt.legend(loc='upper right')
  plt.show()
  return (x,y)

Here the function takes L1 and L2 lines as input and finds the point of intersection, and returns it as tuple. It uses the derived formula for x. It also plots the two lines on a graph for better visualization and understanding. Lets check the function on two equations as 3y=4x+6 and 2y=5x+3 .

L1=Line(3,4,6) #Equation for line 3y=4x+6
L2=Line(2,5,3) #Equation for line 2y=5x+3
sol=findSolution(L1,L2)
print(sol)

The output of the above code snippet is as follows:

Output Of Function Finding Solution With Aybxc
Output Of Function Finding Solution With ay=bx+c

In the above output, the function provides the point of intersection as (0.428,2.571), satisfying the two equations given as input. The graph of the two equations as shown above, support the function’s output. ]

This article focused on simple implementation of finding Intersection of two points. For more implementations of Linear Algebra in Python, it is highly recommended to refer the NumPy Linalg documentation.

Code for Reference

Here is the entire code implementation of the problem statement.

Code for y=mx+c Line Equation:

import matplotlib.pyplot as plt
class Line:
  def __init__(self,slope,const):
    self.m=slope
    self.c=const
def findSolution(L1,L2):
  x=(L1.c-L2.c)/(L2.m-L1.m)
  y=L1.m*x+L1.c
  X=[x for x in range(-10,11)]
  Y1=[(L1.m*x)+L1.c for x in X]
  Y2=[(L2.m*x)+L2.c for x in X]
  plt.plot(X,Y1,'-r',label=f'y={L1.m}x+{L1.c}')
  plt.plot(X,Y2,'-b',label=f'y={L2.m}x+{L2.c}')
  plt.legend(loc='upper right')
  plt.show()
  return (x,y)
L1=Line(3,5) #Equation of line y=3x+5
L2=Line(2,3) #Equation of line y=2x+3
sol=findSolution(L1,L2)
print(sol)

Code for ay=bx+c Line Equation:

import matplotlib.pyplot as plt
class Line:
  def __init__(self,ycoeff,xcoeff,const):
    self.a=ycoeff
    self.b=xcoeff
    self.c=const
def findSolution(L1,L2):
  x=((L1.a*L2.c)-(L2.a*L1.c))/((L2.a*L1.b)-(L1.a*L2.b))
  y=(L1.b*x+L1.c)/L1.a
  X=[x for x in range(-10,11)]
  Y1=[((L1.b*x)+L1.c)/(L1.a) for x in X]
  Y2=[((L2.b*x)+L2.c)/(L2.a) for x in X]
  plt.plot(X,Y1,'-r',label=f'{L1.a}y={L1.b}x+{L1.c}')
  plt.plot(X,Y2,'-b',label=f'{L2.a}y={L2.b}x+{L2.c}')
  plt.legend(loc='upper right')
  plt.show()
  return (x,y)
L1=Line(3,4,6) #Equation for line 3y=4x+6
L2=Line(2,5,3) #Equation for line 2y=5x+3
sol=findSolution(L1,L2)
print(sol)