Line Passing Through 2 Points

Last Updated : 24 Aug, 2026

Given two points P(x1, y1) and Q(x2, y2) in the coordinate plane, find the equation of the line passing through both the points. 

Examples: 

Input: x1 = 3, y1 = 2, x2 = 2, y2 = 6
Output: 4x+1y = 14
Explanation: The unique line passing through the points (3,2) and (2,6) is 4x+1y=14.

Input: x1 = 3, y1 = 2, x2 = 5, y2 = 7
Output: 5x-2y = 11
Explanation: The unique line passing through the points (3,2) and (5,7) is 5x-2y=11.

Try It Yourself
redirect icon

Let the given two points be P(x1, y1) and Q(x2, y2). Now, we find the equation of line formed by these points.   

Let the two points satisfy the line ax + by = c. 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

How does this work?

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: 

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++
#include <bits/stdc++.h>
using namespace std;

string getLine(int x1, int y1, int x2, int y2)
{

    // Calculate numerator and denominator of the slope
    int dy = y2 - y1;
    int dx = x2 - x1;

    // Using point-slope form:
    // y - y1 = (dy / dx) * (x - x1)

    // Cross multiply and rearrange:
    // dx(y - y1) = dy(x - x1)
    // dy*x - dx*y = dy*x1 - dx*y1

    int a = dy;
    int b = -dx;
    int c = dy * x1 - dx * y1;

    string res;

    // Construct the equation
    if (b >= 0)
        res = to_string(a) + "x+" + to_string(b) + "y=" + to_string(c);
    else
        res = to_string(a) + "x" + to_string(b) + "y=" + to_string(c);

    return res;
}

int main()
{
    int x1 = 3, y1 = 2, x2 = 5, y2 = 7;

    cout << getLine(x1, y1, x2, y2);

    return 0;
}
Java
public class Main {

    public static String getLine(int x1, int y1, int x2, int y2) {

        // Calculate numerator and denominator of the slope
        int dy = y2 - y1;
        int dx = x2 - x1;

        // Using point-slope form:
        // y - y1 = (dy / dx) * (x - x1)

        // Cross multiply and rearrange:
        // dx(y - y1) = dy(x - x1)
        // dy*x - dx*y = dy*x1 - dx*y1

        int a = dy;
        int b = -dx;
        int c = dy * x1 - dx * y1;

        String res;

        // Construct the equation
        if (b >= 0)
            res = a + "x+" + b + "y=" + c;
        else
            res = a + "x" + b + "y=" + c;

        return res;
    }

    public static void main(String[] args) {
        int x1 = 3, y1 = 2, x2 = 5, y2 = 7;

        System.out.println(getLine(x1, y1, x2, y2));
    }
}
Python
def getLine(x1, y1, x2, y2):

    # Calculate numerator and denominator of the slope
    dy = y2 - y1
    dx = x2 - x1

    # Using point-slope form:
    # y - y1 = (dy / dx) * (x - x1)

    # Cross multiply and rearrange:
    # dx(y - y1) = dy(x - x1)
    # dy*x - dx*y = dy*x1 - dx*y1

    a = dy
    b = -dx
    c = dy * x1 - dx * y1

    res = ""  # Construct the equation
    if b >= 0:
        res = f"{a}x+{b}y={c}"
    else:
        res = f"{a}x{b}y={c}"

    return res

x1 = 3
y1 = 2
x2 = 5
y2 = 7

print(getLine(x1, y1, x2, y2))
C#
using System;

class Program
{
    static string getLine(int x1, int y1, int x2, int y2)
    {
        // Calculate numerator and denominator of the slope
        int dy = y2 - y1;
        int dx = x2 - x1;

        // Using point-slope form:
        // y - y1 = (dy / dx) * (x - x1)

        // Cross multiply and rearrange:
        // dx(y - y1) = dy(x - x1)
        // dy*x - dx*y = dy*x1 - dx*y1

        int a = dy;
        int b = -dx;
        int c = dy * x1 - dx * y1;

        string res;

        // Construct the equation
        if (b >= 0)
            res = a.ToString() + "x+" + b.ToString() + "y=" + c.ToString();
        else
            res = a.ToString() + "x" + b.ToString() + "y=" + c.ToString();

        return res;
    }

    static void Main()
    {
        int x1 = 3, y1 = 2, x2 = 5, y2 = 7;

        Console.WriteLine(getLine(x1, y1, x2, y2));
    }
}
JavaScript
function getLine(x1, y1, x2, y2) {

    // Calculate numerator and denominator of the slope
    let dy = y2 - y1;
    let dx = x2 - x1;

    // Using point-slope form:
    // y - y1 = (dy / dx) * (x - x1)

    // Cross multiply and rearrange:
    // dx(y - y1) = dy(x - x1)
    // dy*x - dx*y = dy*x1 - dx*y1

    let a = dy;
    let b = -dx;
    let c = dy * x1 - dx * y1;

    let res;  // Construct the equation
    if (b >= 0)
        res = a + "x+" + b + "y=" + c;
    else
        res = a + "x" + b + "y=" + c;

    return res;
}

let x1 = 3, y1 = 2, x2 = 5, y2 = 7;

console.log(getLine(x1, y1, x2, y2));

Output
5x-2y=11
Comment