# Program for Point of Intersection of Two Lines

Given points A and B corresponding to line AB and points P and Q corresponding to line PQ, find the point of intersection of these lines. The points are given in 2D Plane with their X and Y Coordinates.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

Input : A = (1, 1), B = (4, 4) C = (1, 8), D = (2, 4) Output : The intersection of the given lines AB and CD is: (2.4, 2.4) Input : A = (0, 1), B = (0, 4) C = (1, 8), D = (1, 4) Output : The given lines AB and CD are parallel.

First of all, let us assume that we have two points (x_{1}, y_{1}) and (x_{2}, y_{2}). Now, we find the equation of line formed by these points.

Let the given lines be :

- a
_{1}x + b_{1}y = c_{1} - a
_{2}x + b_{2}y = c_{2}

We have to now solve these 2 equations to find the point of intersection. To solve, we multiply 1. by b_{2} and 2 by b_{1}

This gives us,

a_{1}b_{2}x + b_{1}b_{2}y = c_{1}b_{2}

a_{2}b_{1}x + b_{2}b_{1}y = c_{2}b_{1}

Subtracting these we get,

(a_{1}b_{2} – a_{2}b_{1}) x = c_{1}b_{2} – c_{2}b_{1}

This gives us the value of x. Similarly, we can find the value of y. (x, y) gives us the point of intersection.

**Note:** This gives the point of intersection of two lines, but if we are given line segments instead of lines, we have to also recheck that the point so computed actually lies on both the line segments.

If the line segment is specified by points (x_{1}, y_{1}) and (x_{2}, y_{2}), then to check if (x, y) is on the segment we have to just check that

- min (x
_{1}, x_{2}) <= x <= max (x_{1}, x_{2}) - min (y
_{1}, y_{2}) <= y <= max (y_{1}, y_{2})

The pseudo code for the above implementation:

determinant = a_{1}b_{2}- a_{2}b_{1}if (determinant == 0) { // Lines are parallel } else { x = (c_{1}b_{2}- c_{2}b_{1})/determinant y = (a_{1}c_{2}- a_{2}c_{1})/determinant }

These can be derived by first getting the slope directly and then finding the intercept of the line.

## C++

`// C++ Implementation. To find the point of` `// intersection of two lines` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// This pair is used to store the X and Y` `// coordinates of a point respectively` `#define pdd pair<double, double>` ` ` `// Function used to display X and Y coordinates` `// of a point` `void` `displayPoint(pdd P)` `{` ` ` `cout << ` `"("` `<< P.first << ` `", "` `<< P.second` ` ` `<< ` `")"` `<< endl;` `}` ` ` `pdd lineLineIntersection(pdd A, pdd B, pdd C, pdd D)` `{` ` ` `// Line AB represented as a1x + b1y = c1` ` ` `double` `a1 = B.second - A.second;` ` ` `double` `b1 = A.first - B.first;` ` ` `double` `c1 = a1*(A.first) + b1*(A.second);` ` ` ` ` `// Line CD represented as a2x + b2y = c2` ` ` `double` `a2 = D.second - C.second;` ` ` `double` `b2 = C.first - D.first;` ` ` `double` `c2 = a2*(C.first)+ b2*(C.second);` ` ` ` ` `double` `determinant = a1*b2 - a2*b1;` ` ` ` ` `if` `(determinant == 0)` ` ` `{` ` ` `// The lines are parallel. This is simplified` ` ` `// by returning a pair of FLT_MAX` ` ` `return` `make_pair(FLT_MAX, FLT_MAX);` ` ` `}` ` ` `else` ` ` `{` ` ` `double` `x = (b2*c1 - b1*c2)/determinant;` ` ` `double` `y = (a1*c2 - a2*c1)/determinant;` ` ` `return` `make_pair(x, y);` ` ` `}` `}` ` ` `// Driver code` `int` `main()` `{` ` ` `pdd A = make_pair(1, 1);` ` ` `pdd B = make_pair(4, 4);` ` ` `pdd C = make_pair(1, 8);` ` ` `pdd D = make_pair(2, 4);` ` ` ` ` `pdd intersection = lineLineIntersection(A, B, C, D);` ` ` ` ` `if` `(intersection.first == FLT_MAX &&` ` ` `intersection.second==FLT_MAX)` ` ` `{` ` ` `cout << ` `"The given lines AB and CD are parallel.\n"` `;` ` ` `}` ` ` ` ` `else` ` ` `{` ` ` `// NOTE: Further check can be applied in case` ` ` `// of line segments. Here, we have considered AB` ` ` `// and CD as lines` ` ` `cout << ` `"The intersection of the given lines AB "` ` ` `"and CD is: "` `;` ` ` `displayPoint(intersection);` ` ` `}` ` ` ` ` `return` `0;` `}` |

## Java

`// Java Implementation. To find the point of` `// intersection of two lines` ` ` `// Class used to used to store the X and Y` `// coordinates of a point respectively` `class` `Point` `{` ` ` `double` `x,y;` ` ` ` ` `public` `Point(` `double` `x, ` `double` `y) ` ` ` `{` ` ` `this` `.x = x;` ` ` `this` `.y = y;` ` ` `}` ` ` ` ` `// Method used to display X and Y coordinates` ` ` `// of a point` ` ` `static` `void` `displayPoint(Point p)` ` ` `{` ` ` `System.out.println(` `"("` `+ p.x + ` `", "` `+ p.y + ` `")"` `);` ` ` `}` `}` ` ` `class` `Test` `{ ` ` ` `static` `Point lineLineIntersection(Point A, Point B, Point C, Point D)` ` ` `{` ` ` `// Line AB represented as a1x + b1y = c1` ` ` `double` `a1 = B.y - A.y;` ` ` `double` `b1 = A.x - B.x;` ` ` `double` `c1 = a1*(A.x) + b1*(A.y);` ` ` ` ` `// Line CD represented as a2x + b2y = c2` ` ` `double` `a2 = D.y - C.y;` ` ` `double` `b2 = C.x - D.x;` ` ` `double` `c2 = a2*(C.x)+ b2*(C.y);` ` ` ` ` `double` `determinant = a1*b2 - a2*b1;` ` ` ` ` `if` `(determinant == ` `0` `)` ` ` `{` ` ` `// The lines are parallel. This is simplified` ` ` `// by returning a pair of FLT_MAX` ` ` `return` `new` `Point(Double.MAX_VALUE, Double.MAX_VALUE);` ` ` `}` ` ` `else` ` ` `{` ` ` `double` `x = (b2*c1 - b1*c2)/determinant;` ` ` `double` `y = (a1*c2 - a2*c1)/determinant;` ` ` `return` `new` `Point(x, y);` ` ` `}` ` ` `}` ` ` ` ` `// Driver method` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `Point A = ` `new` `Point(` `1` `, ` `1` `);` ` ` `Point B = ` `new` `Point(` `4` `, ` `4` `);` ` ` `Point C = ` `new` `Point(` `1` `, ` `8` `);` ` ` `Point D = ` `new` `Point(` `2` `, ` `4` `);` ` ` ` ` `Point intersection = lineLineIntersection(A, B, C, D);` ` ` ` ` `if` `(intersection.x == Double.MAX_VALUE &&` ` ` `intersection.y == Double.MAX_VALUE)` ` ` `{` ` ` `System.out.println(` `"The given lines AB and CD are parallel."` `);` ` ` `}` ` ` ` ` `else` ` ` `{` ` ` `// NOTE: Further check can be applied in case` ` ` `// of line segments. Here, we have considered AB` ` ` `// and CD as lines` ` ` `System.out.print(` `"The intersection of the given lines AB "` `+ ` ` ` `"and CD is: "` `);` ` ` `Point.displayPoint(intersection);` ` ` `}` ` ` `}` `}` |

## C#

`using` `System;` ` ` `// C# Implementation. To find the point of ` `// intersection of two lines ` ` ` `// Class used to used to store the X and Y ` `// coordinates of a point respectively ` `public` `class` `Point` `{` ` ` `public` `double` `x, y;` ` ` ` ` `public` `Point(` `double` `x, ` `double` `y)` ` ` `{` ` ` `this` `.x = x;` ` ` `this` `.y = y;` ` ` `}` ` ` ` ` `// Method used to display X and Y coordinates ` ` ` `// of a point ` ` ` `public` `static` `void` `displayPoint(Point p)` ` ` `{` ` ` `Console.WriteLine(` `"("` `+ p.x + ` `", "` `+ p.y + ` `")"` `);` ` ` `}` `}` ` ` `public` `class` `Test` `{` ` ` `public` `static` `Point lineLineIntersection(Point A, Point B, Point C, Point D)` ` ` `{` ` ` `// Line AB represented as a1x + b1y = c1 ` ` ` `double` `a1 = B.y - A.y;` ` ` `double` `b1 = A.x - B.x;` ` ` `double` `c1 = a1 * (A.x) + b1 * (A.y);` ` ` ` ` `// Line CD represented as a2x + b2y = c2 ` ` ` `double` `a2 = D.y - C.y;` ` ` `double` `b2 = C.x - D.x;` ` ` `double` `c2 = a2 * (C.x) + b2 * (C.y);` ` ` ` ` `double` `determinant = a1 * b2 - a2 * b1;` ` ` ` ` `if` `(determinant == 0)` ` ` `{` ` ` `// The lines are parallel. This is simplified ` ` ` `// by returning a pair of FLT_MAX ` ` ` `return` `new` `Point(` `double` `.MaxValue, ` `double` `.MaxValue);` ` ` `}` ` ` `else` ` ` `{` ` ` `double` `x = (b2 * c1 - b1 * c2) / determinant;` ` ` `double` `y = (a1 * c2 - a2 * c1) / determinant;` ` ` `return` `new` `Point(x, y);` ` ` `}` ` ` `}` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `Main(` `string` `[] args)` ` ` `{` ` ` `Point A = ` `new` `Point(1, 1);` ` ` `Point B = ` `new` `Point(4, 4);` ` ` `Point C = ` `new` `Point(1, 8);` ` ` `Point D = ` `new` `Point(2, 4);` ` ` ` ` `Point intersection = lineLineIntersection(A, B, C, D);` ` ` ` ` `if` `(intersection.x == ` `double` `.MaxValue && intersection.y == ` `double` `.MaxValue)` ` ` `{` ` ` `Console.WriteLine(` `"The given lines AB and CD are parallel."` `);` ` ` `}` ` ` ` ` `else` ` ` `{` ` ` `// NOTE: Further check can be applied in case ` ` ` `// of line segments. Here, we have considered AB ` ` ` `// and CD as lines ` ` ` `Console.Write(` `"The intersection of the given lines AB "` `+ ` `"and CD is: "` `);` ` ` `Point.displayPoint(intersection);` ` ` `}` ` ` `}` `}` ` ` ` ` `// This code is contributed by Shrikant13` |

**Output:**

The intersection of the given lines AB and CD is: (2.4, 2.4)

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.