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

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

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) {
int a = points - points;
int b = points - points; int c = a*points + b*points;
cout<<"("<<a<<"x) + ("<<b<<"y) = ("<<c<<
}
int main() {
int points = {{5, 9}, {1, 4}}; cout<<"The equation of line is "; findLine(points);
return 0;
}

## Output

The equation of line is (-5x) + (4y) = (11)