- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

#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)

- Related Questions & Answers
- Program to find equation of a plane passing through 3 points in C++
- How to create a line that passes through specified points in an R plot?
- Program to find out is a point is reachable from the current position through given points in Python
- Maximum distinct lines passing through a single point in C
- How to find the difference between regression line and the points in R?
- Program to find out the number of integral coordinates on a straight line between two points in Python
- Program to count number of points that lie on a line in Python
- Max Points on a Line in C++
- Program to find Reordered Power of 2 in Python
- Program to find slope of a line in C++
- Program to find minimum cost to connect all points in Python
- Passing mouse clicks through an overlaying HTML element <div>
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++
- Program to check whether list of points form a straight line or not in Python
- C++ Program to Find Number of Articulation points in a Graph