It will containĪll active line Segments ordered by y coordinate.
Sort Points from left to right (according to x coordinate)Ģ. The following pseudocode doesn’t use heap. Building a min heap takes O(n) time and every extract min operation takes O(Logn) time (See this). With a Self-Balancing BST, we can do all of the above operations in O(Logn) time.Īlso, in step 1, instead of sorting, we can use min heap data structure. The obvious choice for above operations is Self-Balancing Binary Search Tree like AVL Tree, Red Black Tree. We need to do following operations efficiently:Ĭ) Find predecessor and successor according to y coordinate values In step 2, we need to store all active line segments. What data structures should be used for efficient implementation? The Sweep Line technique is useful in many other geometric algorithms like calculating the 2D Voronoi diagram That is why this algorithm is called Sweep Line Algorithm. The step 2 is like passing a vertical line from all points starting from the leftmost point to the rightmost point. b) If the current point is a right point, remove its line segment from active list and check whether its two active neighbors (points just above and below) intersect with each other. Note that we consider only those neighbors which are still active. And add its line to active line segments (line segments for which left end point is seen, but right end point is not seen yet). a) If the current point is a left point of its line segment, check for intersection of its line segment with the segments just above and below it. While sorting maintain a flag to indicate whether this point is left point of its line or right point.Ģ) Start from the leftmost point. Sort all points according to x coordinates. There must be 2n end points to represent the n lines. Following are detailed steps.ġ) Let there be n given lines. The algorithm first sorts the end points along the x axis from left to right, then it passes a vertical line through all points from left to right and checks for intersections. Sweep Line Algorithm: We can solve this problem in O(nLogn) time using Sweep Line Algorithm. We can check two line segments in O(1) time. Naive Algorithm A naive solution to solve this problem is to check every pair of lines and check if the pair intersects or not. Here we are given n line segments and we need to find out if any two line segments intersect or not. We have discussed the problem to detect if two given line segments intersect or not.
#Coda 2 shows red line at end of code line how to
How to check if a given point lies inside or outside a polygon?.How to check if two given line segments intersect?.Given n line segments, find if any two segments intersect.
ISRO CS Syllabus for Scientist/Engineer Exam.ISRO CS Original Papers and Official Keys.GATE CS Original Papers and Official Keys.