# Find intersection point of lines inside a section

Given N lines in two-dimensional space in y = mx + b form and a vertical section. We need to find out whether there is an intersection point inside the given section or not.

Examples:

In below diagram four lines are there, L1 : y = x + 2 L2 : y = -x + 7 L3 : y = -3 L4 : y = 2x – 7 and vertical section is given from x = 2 to x = 4 We can see that in above diagram, the intersection point of line L1 and L2 lies between the section.

We can solve this problem using sorting. First, we will calculate intersection point of each line with both the boundaries of vertical section and store that as a pair. We just need to store y-coordinates of intersections as a pair because x-coordinates are equal to boundary itself. Now we will sort these pairs on the basis of their intersection with left boundary. After that, we will loop over these pairs one by one and if for any two consecutive pairs, the second value of the current pair is less than that of the second value of the previous pair then there must be an intersection in the given vertical section.

The possible orientation of two consecutive pairs can be seen in above diagram for L1 and L2. We can see that when the second value is less, intersection lies in vertical section.

Total time complexity of solution will be O(n logn)

`// C++ program to check an intersection point ` `// inside a given vertical section ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// structure to represent a line ` `struct` `line { ` ` ` `int` `m, b; ` ` ` `line() { } ` ` ` `line(` `int` `m, ` `int` `b) : m(m), b(b) { } ` `}; ` ` ` `// Utility method to get Y-cordinate ` `// corresponding to x in line l ` `int` `getYFromLine(line l, ` `int` `x) ` `{ ` ` ` `return` `(l.m * x + l.b); ` `} ` ` ` `// method returns true if two line cross ` `// each other between xL and xR range ` `bool` `isIntersectionPointInsideSection(line lines[], ` ` ` `int` `xL, ` `int` `xR, ` `int` `N) ` `{ ` ` ` `pair<` `int` `, ` `int` `> yBoundary[N]; ` ` ` ` ` `// first calculating y-values and putting ` ` ` `// in boundary pair ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `yBoundary[i] = make_pair(getYFromLine(lines[i], xL), ` ` ` `getYFromLine(lines[i], xR)); ` ` ` ` ` ` ` `// sorting the pair on the basis of first ` ` ` `// boundary intersection ` ` ` `sort(yBoundary, yBoundary + N); ` ` ` ` ` `// looping over sorted pairs for comparison ` ` ` `for` `(` `int` `i = 1; i < N; i++) { ` ` ` ` ` `// if current pair's second value is smaller ` ` ` `// than previous pair's then return true ` ` ` `if` `(yBoundary[i].second < yBoundary[i - 1].second) ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` `int` `m[] = { 1, -1, 0, 2 }; ` ` ` `int` `b[] = { 2, 7, -3, -7 }; ` ` ` ` ` `// copy values in line struct ` ` ` `line lines[N]; ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `lines[i] = line(m[i], b[i]); ` ` ` ` ` `int` `xL = 2; ` ` ` `int` `xR = 4; ` ` ` ` ` `if` `(isIntersectionPointInsideSection(lines, xL, xR, N)) { ` ` ` `cout << ` `"Intersection point lies between "` ` ` `<< xL << ` `" and "` `<< xR << endl; ` ` ` `} ` `else` `{ ` ` ` `cout << ` `"No Intersection point lies between "` ` ` `<< xL << ` `" and "` `<< xR << endl; ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

Output:

Intersection point lies between 2 and 4

This article is contributed by **Utkarsh Trivedi**. 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Program for Point of Intersection of Two Lines
- Find a point that lies inside exactly K given squares
- Find if a point lies inside, outside or on the circumcircle of three points A, B, C
- Find minimum radius such that atleast k point lie inside the circle
- Maximum points of intersection n lines
- Number of pairs of lines having integer intersection points
- Section formula (Point that divides a line in given ratio)
- Count number of pairs of lines intersecting at a Point
- Maximum distinct lines passing through a single point
- Triangle with no point inside
- Check if a point is inside, outside or on the ellipse
- Check if a point is inside, outside or on the parabola
- How to check if a given point lies inside or outside a polygon?
- Check whether a given point lies on or inside the rectangle | Set 3
- Check whether a given point lies inside a triangle or not
- Check whether a given point lies inside a rectangle or not
- Check whether a point lies inside a sphere or not
- Check if a point lies on or inside a rectangle | Set-2
- Check if the given point lies inside given N points of a Convex Polygon
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles