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.
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)
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 email@example.com. 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.
- Check whether a given point lies inside a triangle or not
- How to check if a given point lies inside or outside a polygon?
- Given n line segments, find if any two segments intersect
- Find if two rectangles overlap
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Find Simple Closed Path for a given set of points
- Count Integral points inside a Triangle
- Triangle with no point inside
- Non-crossing lines to connect points in a circle
- Optimum location of point to minimize total distance
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Find perimeter of shapes formed with 1s in binary matrix
- Queries on count of points lie inside a circle
- Check whether a point exists in circle sector or not.
- Find an Integer point on a line segment with given two ends