# Program to find line passing through 2 Points in C++

C++Server Side ProgrammingProgramming

In this problem, we are given coordinates two points A and B on the coordinate plane. Our task is to create a program to find line passing through 2 Points in C++.

## Problem Description

To find the line, we need to use the equation of the line and put the solution using the coordinates.

Let’s take an example to understand the problem

Input: A = (3, 3) B = (6, 1)

Output: 2x + 3y = 15

## Solution Approach

To find the equation of the line, we will use the general equation of line −

ax + by = c

This has to be satisfied by both the points A(x1, y1) and B(x2, y2)

The will make the equations,

ax1 + by1 = c

ax2 + by2 = c

Now, as c is common for both the equations we have

ax1 + by1 = ax2 + by2

=> ax1 - ax2 = by2 - by1

On equating we get,

$$a = (y2 - y1)$$ $$b = (x1 - x2)$$

c will be found using the equation,

$$ax1 + by1 = c$$

So, this is the equation for the equation of the line,

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

## Example

Live Demo

#include <iostream> using namespace std;
void findLine(int points[2][2]) {
int a = points[1][1] - points[0][1];
int b = points[0][0] - points[1][0]; int c = a*points[0][0] + b*points[0][1];
cout<<"("<<a<<"x) + ("<<b<<"y) = ("<<c<<
}
int main() {
int points[2][2] = {{5, 9}, {1, 4}}; cout<<"The equation of line is "; findLine(points);
return 0;
}

## Output

The equation of line is (-5x) + (4y) = (11)
Published on 04-May-2020 08:00:29