# Program to find line passing through 2 Points

Given two points P and Q in the coordinate plane, find the equation of the line passing through both the 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

Let the given two points be P(x_{1}, y_{1}) and Q(x_{2}, y_{2}). 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,

ax_{1} + by_{1} = c

ax_{2} + by_{2} = c

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

a = y_{2}- y_{1}b = x_{1}- x_{2}c = ax_{1}+ by_{1}

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 : **

ax_{1}+ by_{1}= c ...(i) ax_{2}+ by_{2}= c ...(ii) Equating (i) and (ii), ax_{1}+ by_{1}= ax_{2}+ by_{2}=> a(x_{1}- x_{2}) = b(y_{2}- y_{1}) Thus, for equating LHS and RHS, we can simply have, a = (y_{2}- y_{1}) AND b = (x_{1}- x_{2}) so that we have, (y_{2}- y_{1})(x_{1}- x_{2}) = (x_{1}- x_{2})(y_{2}- y_{1}) AND Putting these values in (i), we get, c = ax_{1}+ by_{1}

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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Implementation to find the line passing ` `// through two points ` `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 ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output:**

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

This article is contributed by **Aanya Jindal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Program to find equation of a plane passing through 3 points
- Find points at a given distance on a line of given slope
- Program to find the mid-point of a line
- Program to find slope of a line
- Represent a given set of points by the best possible straight line
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Count maximum points on same line
- Number of horizontal or vertical line segments to connect 3 points
- Number of ordered points pair satisfying line equation
- Program for distance between two points on earth
- Program to check if three points are collinear
- Program to check whether 4 points in a 3-D plane are Coplanar
- Find Corners of Rectangle using mid points
- Find K Closest Points to the Origin
- Find the other end point of a line with given one end and mid