The Wayback Machine - https://web.archive.org/web/20240629111407/https://www.geeksforgeeks.org/program-find-line-passing-2-points/
Open In App

Program to find line passing through 2 Points

Last Updated : 08 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given two points P and Q in the coordinate plane, find the equation of the line passing through both points.
This kind of conversion is very useful in many geometric algorithms like intersection of lines, finding the circumcenter of a triangle, finding the incenter of a triangle and many more…

Examples: 

Input : P(3, 2)
        Q(2, 6)
Output : 4x + 1y = 14

Input : P(0, 1)
        Q(2, 4)
Output : 3x + -2y = -2
Recommended Practice

Let the given two points be P(x1, y1) and Q(x2, y2). Now, we find the equation of line formed by these points.
Any line can be represented as, 
ax + by = c 
Let the two points satisfy the given line. So, we have, 
ax1 + by1 = c 
ax2 + by2 = c 

We can set the following values so that all the equations hold true,  

a = y2 - y1
b = x1 - x2
c = ax1 + by1

These can be derived by first getting the slope directly and then finding the intercept of the line. OR these can also be derived cleverly by a simple observation as under:

Derivation : 

ax1 + by1 = c ...(i)
ax2 + by2 = c ...(ii)
Equating (i) and (ii),
ax1 + by1 = ax2 + by2
=> a(x1 - x2) = b(y2 - y1)
Thus, for equating LHS and RHS, we can simply have,
a = (y2 - y1)
AND
b = (x1 - x2)
so that we have,
(y2 - y1)(x1 - x2) = (x1 - x2)(y2 - y1)
AND
Putting these values in (i), we get,
c = ax1 + by1 

Thus, we now have the values of a, b, and c which means that we have the line in the coordinate plane.

C++




// C++ Implementation to find the line passing
// through two points
#include <iostream>
using namespace std;
 
// This pair is used to store the X and Y
// coordinate of a point respectively
#define pdd pair<double, double>
 
// Function to find the line given two points
void lineFromPoints(pdd P, pdd Q)
{
    double a = Q.second - P.second;
    double b = P.first - Q.first;
    double c = a * (P.first) + b * (P.second);
 
    if (b < 0) {
        cout << "The line passing through points P and Q "
                "is: "
             << a << "x - " << b << "y = " << c << endl;
    }
    else {
        cout << "The line passing through points P and Q "
                "is: "
             << a << "x + " << b << "y = " << c << endl;
    }
}
 
// Driver code
int main()
{
    pdd P = make_pair(3, 2);
    pdd Q = make_pair(2, 6);
    lineFromPoints(P, Q);
    return 0;
}


Java




// Java Implementation to find the line passing
// through two points
import java.io.*;
public class GFG {
 
    // This pair is used to store the X and Y
    // coordinate of a point respectively
    static class Pair {
        int first, second;
 
        public Pair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }
    }
 
    // Function to find the line given two points
    static void lineFromPoints(Pair P, Pair Q)
    {
        int a = Q.second - P.second;
        int b = P.first - Q.first;
        int c = a * (P.first) + b * (P.second);
 
        if (b < 0) {
            System.out.println(
                "The line passing through points P and Q is: "
                + a + "x - " + b + "y = " + c);
        }
        else {
            System.out.println(
                "The line passing through points P and Q is: "
                + a + "x + " + b + "y = " + c);
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        Pair P = new Pair(3, 2);
        Pair Q = new Pair(2, 6);
        lineFromPoints(P, Q);
    }
}
 
// This code is contributed by Princi Singh


Python3




# Python3 Implementation to find the line passing
# through two points
 
# This pair is used to store the X and Y
# coordinate of a point respectively
# define pdd pair<double, double>
 
# Function to find the line given two points
 
 
def lineFromPoints(P, Q):
 
    a = Q[1] - P[1]
    b = P[0] - Q[0]
    c = a*(P[0]) + b*(P[1])
 
    if(b < 0):
        print("The line passing through points P and Q is:",
              a, "x - ", b, "y = ", c, "\n")
    else:
        print("The line passing through points P and Q is: ",
              a, "x + ", b, "y = ", c, "\n")
 
 
# Driver code
if __name__ == '__main__':
    P = [3, 2]
    Q = [2, 6]
    lineFromPoints(P, Q)
 
# This code is contributed by ash264


C#




// C# Implementation to find the line passing
// through two points
using System;
 
class GFG {
 
    // This pair is used to store the X and Y
    // coordinate of a point respectively
    public class Pair {
        public int first, second;
 
        public Pair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }
    }
 
    // Function to find the line given two points
    static void lineFromPoints(Pair P, Pair Q)
    {
        int a = Q.second - P.second;
        int b = P.first - Q.first;
        int c = a * (P.first) + b * (P.second);
 
        if (b < 0) {
            Console.WriteLine(
                "The line passing through points P and Q is: "
                + a + "x - " + b + "y = " + c);
        }
        else {
            Console.WriteLine(
                "The line passing through points P and Q is: "
                + a + "x + " + b + "y = " + c);
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        Pair P = new Pair(3, 2);
        Pair Q = new Pair(2, 6);
        lineFromPoints(P, Q);
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript




<script>
 
// Javascript implementation to find the
// line passing through two points
 
// Function to find the line given two points
function lineFromPoints(P, Q)
{
    var a = Q[1] - P[1]
    var b = P[0] - Q[0]
    var c = a*(P[0]) + b*(P[1])
 
    if (b < 0)
        document.write("The line passing through " +
                       "points P and Q is:  " + a +
                       "x - " + b + "y = " + c + "<br>")
    else
        document.write("The line passing through " +
                       "points P and Q is:  "+ a + 
                       "x + " + b + "y = " + c + "<br>")
}
 
// Driver code
var P = [ 3, 2 ]
var Q = [ 2, 6 ]
 
lineFromPoints(P, Q)
 
// This code is contributed by akshitsaxenaa09
 
</script>


Output

The line passing through points P and Q is: 4x + 1y = 14

Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.



Previous Article
Next Article

Similar Reads

Find the equation of the straight line passing through the given points
Given an array arr containing N coordinate points in a plane, the task is to check whether the coordinate points lie on a straight line or not. If they lie on a straight line then print Yes and also the equation of that line otherwise print No. Example: Input: arr[] = {{1, 1}, {2, 2}, {3, 3}} Output: Yes1x- 1y=0 Input: arr[] = {{0, 1}, {2, 0}}Outpu
6 min read
Find X and Y intercepts of a line passing through the given points
Given two points on a 2D plane, the task is to find the x - intercept and the y - intercept of a line passing through the given points.Examples: Input: points[][] = {{5, 2}, {2, 7}} Output: 6.2 10.333333333333334Input: points[][] = {{3, 2}, {2, 4}} Output: 4.0 8.0 Approach: Find the slope using the given points.Put the value of the slope in the exp
7 min read
Minimum points to be selected such that removal of line segments passing through them empties given array
Given a 2D array arr[][], where each row is of the form {start, end} representing the start and endpoints of each line segment on the X-axis. In one step, select a point on the X-axis and delete all the line segments passing through that point. The task is to find the minimum number of such points that need to be selected to delete all the line seg
7 min read
Equation of straight line passing through a given point which bisects it into two equal line segments
Given a straight line which passes through a given point (x0, y0) such that this point bisects the line segment in two equal line segments. The task is to find the equation of this straight line.Examples: Input: x0 = 4, y0 = 3 Output: 3x + 4y = 24Input: x0 = 7, y0 = 12 Output: 12x + 7y = 168 Approach: Let PQ be the line and AB be the line segment b
4 min read
Equation of a straight line passing through a point and making a given angle with a given line
Given four integers a, b, c representing coefficients of a straight line with equation (ax + by + c = 0), the task is to find the equations of the two straight lines passing through a given point [Tex](x1, y1) [/Tex]and making an angle ? with the given straight line. Examples: Input: a = 2, b = 3, c = -7, x1 = 4, y1 = 9, ? = 30Output: y = -0.49x +1
15+ min read
Program to find equation of a plane passing through 3 points
Given three points (x1, y1, z1), (x2, y2, z2), (x3, y3, z3). The task is to find the equation of the plane passing through these 3 points. Examples: Input: x1 = -1 y1 = w z1 = 1 x2 = 0 y2 = -3 z2 = 2 x3 = 1 y3 = 1 z3 = -4 Output: equation of plane is 26 x + 7 y + 9 z + 3 = 0.Input: x1 = 2, y1 = 1, z1 = -1, 1 x2 = 0, y2 = -2, z2 = 0 x3 = 1, y3 = -1,
10 min read
Find the sum of diagonals passing through given coordinates for Q query
Given a 2D matrix of size N x M and Q queries where each query represents a coordinate (x, y) of the matrix, the task is to find the sum of all elements lying in the diagonals that pass through the given point. Examples: Input: N = 4, M = 4, mat[][] = {{1, 2, 2, 1}, {2, 4, 2, 4}, {2, 2, 3, 1}, {2, 4, 2, 4}}, query = { {0, 0}, {3, 1}, {3, 3} } Outpu
12 min read
Maximum distinct lines passing through a single point
Given [Tex]N [/Tex]lines represented by two points [Tex](x1, y1) [/Tex]and [Tex](x2, y2) [/Tex]. The task is to find maximum number of lines which can pass through a single point, without superimposing (or covering) any other line. We can move any line but not rotate it.Examples: Input : Line 1 : x1 = 1, y1 = 1, x2 = 2, y2 = 2 Line 2 : x2 = 2, y1 =
6 min read
Number of cells in the right and left diagonals passing through (x, y) in a matrix
Given four integers row, col, x and y where row and col are the number of rows and columns of a 2-D Matrix and x and y are the coordinates of a cell in the same matrix, the task is to find number of cells in the left and the right diagonal which the cell (x, y) of the matrix is associated with. Examples: Input: row = 4, col = 3, x = 2, y = 2 Output
6 min read
Maximum path sum from top left to bottom right of a matrix passing through one of the given cells
Given a matrix mat[][] of dimensions N * M and a set of coordinates of cell coordinates[][] of size Q, the task is to find the maximum sum of a path from the top-left cell (1, 1) to the bottom-right cell (N, M), such that the path should contain at least one of the coordinates from the array coordinates[][2]. The only moves allowed from any cell (i
15 min read
Practice Tags :
three90RightbarBannerImg