Equation of line passing through two points – Python Program to Find Line Passing Through 2 Points

Equation of line passing through two points: In the previous article, we have discussed Python Program to Find N’th Pentagonal Number
Given two points P, Q in the coordinate plane and the task is to find the equation of the line passing through both the given points.

This type of conversion is very useful in many geometric algorithms such as line intersection, finding the circumcentre of a triangle, finding the incenter of a triangle, etc.

Let P(x1, y1) and Q(x2, y2) be the given two points. We can now find the line equation python formed by these points.

Any line can be written as

ax + by = c

Let the two points form a straight line. As a result,

ax1 + by1 = c
ax2 + by2 = c

Formulas:

Line passing through two points: We can change the following values to ensure that all of the equations hold true:

a = y2 – y1
b = x1 – x2

c = ax1 + by1

Examples:

Example1:

Input:

Given First Point = ( 4 , 2 ) 
Given Second Point = ( 3 , 8 )

Output:

The Equation of line passing through the given two points is:  6 x +  1 y =  26

Example2:

Input:

Given First Point = ( 6 , 3 ) 
Given Second Point = ( 1 , 4 )

Output:

The Equation of line passing through the given two points is: 1 x + 5 y = 21

Program to Find Line Passing Through 2 Points in Python

Find the equation of the line passing through two points: Below are the ways to find the equation of the line passing through both the given points in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the first point as static input and store it in two variables.
  • Give the second point as static input and store it in another two variables.
  • Subtract b1 from b2 and store it in another variable say p.
  • Subtract a2 from b1 and store it in another variable say q.
  • Calculate the value of p*(a1) + q*(b1) and store it in a variable say r.
  • Check if the value of q is less than 0 using the if conditional statement.
  • If it is true, then print the respective line equation using the variables p,q,r.
  • Else, print the respective line equation using the variables p,q,r.
  • The Exit of the Program.

Below is the implementation:

# Give the first point as static input and store it in two variables.
a1 = 4
b1 = 2
# Give the second point as static input and store it in another two variables.
a2 = 3
b2 = 8
# Subtract b1 from b2 and store it in another variable say p.
p = b2 - b1
# Subtract a2 from b1 and store it in another variable say q.
q = a1 - a2
# Calculate the value of p*(a1) + q*(b1) and store it in a variable say r.
r = p*(a1) + q*(b1)
# Check if the value of q is less than 0 using the if conditional statement.
if(q < 0):
  # If it is true, then print the respective line equation using the variables p,q,r.
    print("The Equation of line passing through the given two points is:",
          p, "x ", q, "y = ", r)
else:
  # Else, print the respective line equation using the variables p,q,r.
    print("The Equation of line passing through the given two points is: ",
          p, "x + ", q, "y = ", r)
#include <iostream>

using namespace std;

int main ( ) {
  int a1 = 4;
  int b1 = 2;
  int a2 = 3;
  int b2 = 8;
  double p = b2 - b1;
  double q = a1 - a2;
  double r = p * ( a1 ) + q * ( b1 );
  if ( ( q < 0 ) && ( r < 0 ) ) {
    cout << "The Equation of line passing through the given two points is:" << p << "x " << q << "y = " << r << endl;
  }
  else {
    cout << "The Equation of line passing through the given two points is: " << p << "x + " << q << "y = " << r << endl;
  }
  return 0;
}

Output:

The Equation of line passing through the given two points is:  6 x +  1 y =  26

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the first point as user input using map(),int(),split() functions and store it in two variables.
  • Give the second point as user input using map(),int(),split() functions and store it in two variables.
  • Subtract b1 from b2 and store it in another variable say p.
  • Subtract a2 from b1 and store it in another variable say q.
  • Calculate the value of p*(a1) + q*(b1) and store it in a variable say r.
  • Check if the value of q is less than 0 using the if conditional statement.
  • If it is true, then print the respective line equation using the variables p,q,r.
  • Else, print the respective line equation using the variables p,q,r.
  • The Exit of the Program.

Below is the implementation:

# Give the first point as user input using map(),int(),split() functions
# and store it in two variables.
a1, b1 = map(int, input(
    'Enter some random first point values separated by spaces = ').split())
# Give the second point as user input using map(),int(),split() functions
# and store it in two variables.
a2, b2 = map(int, input(
    'Enter some random second point values separated by spaces = ').split())
# Subtract b1 from b2 and store it in another variable say p.
p = b2 - b1
# Subtract a2 from b1 and store it in another variable say q.
q = a1 - a2
# Calculate the value of p*(a1) + q*(b1) and store it in a variable say r.
r = p*(a1) + q*(b1)
# Check if the value of q is less than 0 using the if conditional statement.
if(q < 0):
  # If it is true, then print the respective line equation using the variables p,q,r.
    print("The Equation of line passing through the given two points is:",
          p, "x ", q, "y = ", r)
else:
  # Else, print the respective line equation using the variables p,q,r.
    print("The Equation of line passing through the given two points is: ",
          p, "x + ", q, "y = ", r)

Output:

Enter some random first point values separated by spaces = 6 3
Enter some random second point values separated by spaces = 1 4
The Equation of line passing through the given two points is: 1 x + 5 y = 21

Enhance your coding skills with our list of Python Basic Programs provided and become a pro in the general-purpose programming language Python in no time.

Do yourself:

  1. Find Line Passing Through 2 Points?
  2. Find The Equation Of A Line Passing Through Two Points?
  3. How To Find The Line Passing Through Two Points?
  4. Equation Of A Line Passing Through Two Given Points?
  5. Equation Of Straight Line Passing Through Two Points?
  6. Line Equation From Two Points Python?
  7. Python Line Equation From Two Points?
  8. Python Get Line Equation From Two Points?
  9. Get Line Equation From Two Points Python?
  10. Python Line Between Two Points?

Related Posts On: