Geometric Line Sweep Algorithms

 
GEOMETRIC LINE SWEEP
 
WHAT IS IT?
 
We move an imaginary line over a set of points
We stop at every point to do geometric operations
Once the line has ‘swept’ over every point, the program is done (In some
situations, sweeping more than once is required)
This can be used to find:
The minimum distance between any 2 points on a plane
The point where a certain amount of rectangles overlap on a plane
The total area covered by the union of some rectangles on a plane (not shown
in this presentation)
All intersections of line segments on a plane (not shown in this presentation)
 
finding closest pair of points in O(n log n) time
 
HOW DOES FINDING THE MINIMUM DISTANCE WORK?
 
Sort all of the points by their x value
Store the current smallest distance between points (h,  h = INF originally)
Store a list of all elements whose x coordinate is within h of the current x
coordinate (and then order the elements in this list by their y coordinate)
When we move to a new element, remove all elements whose x value is
now greater than h from the current point.
Run through all elements in that list whose y value is within h of the current y
coordinate
If the distance between the current point and that point is less than h,
update h
The solution can be brute forced in O(n
2
) by comparing every point, but the
line sweep works in O(nlogn), making it much more efficient
 
 
SETUP CODE:
 
SOLVING CODE:
 
finding closest pair of points in O(n log n) time
 
CODEFORCE 1028C - RECTANGLES
 
Given: the bottom left and top right coordinate of n rectangles on a plane
Find a point that is on the boundary of or is inside n-1 or n rectangles (there
can be more than one answer)
Brute force is too slow
 
finding closest pair of points in O(n log n) time
 
CODEFORCE 1028C - RECTANGLES
 
     
SOLUTION
Run a vertical line through the starting point of every rectangle, store the x value of
the ending point of every new rectangle *
When we move to a new rectangle, remove all rectangles that are now too far
away (using their endpoint)
If the line is covering n-1 or n rectangles, store that x
Repeat with a horizontal line to find the y values
Go through every combination of those x and y values to see if any work (this is not
optimal, but there will be at most 2 of each, so it doesn’t matter here)
Another solution is to do a horizontal line sweep every time the vertical line is
covering n-1 or n rectangles, but only line sweep through the rectangles that are
being covered by the vertical lines.
 
* As multiple rectangles can have the same x value at the end point, a multiset must
be used over a set, unlike in the previous example
 
CODEFORCE 1028C - RECTANGLES
 
Eg:
The vertical line only finds x = 1
The horizontal line finds y = 0 and 1
 
Test (1,0) and (1,1) to find that (1,1) works
 
CODE
 
SETUP
 
MAIN CLASS
 
SOLVING METHOD SETUP
 
CODE
 
VERTICAL SWEEP
 
HORIZONTAL SWEEP
 
RUNNING THROUGH POSSIBLE SQUARES
 
While this code does work for the
codeforce problem, it can be easily
optimised by exiting the outer for-loop
once two rectangles have been removed
Slide Note
Embed
Share

Geometric Line Sweep is a powerful technique where an imaginary line sweeps over points, performing geometric operations at each point. This method can find minimum distances between points, overlapping rectangles, and more. By sorting points and efficiently processing them, it can enhance performance significantly compared to brute force solutions. Explore how this technique works and its applications in solving geometric problems.

  • Algorithm
  • Geometric
  • Line Sweep
  • Distance
  • Rectangles

Uploaded on Aug 31, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. GEOMETRIC LINE SWEEP

  2. WHAT IS IT? We move an imaginary line over a set of points We stop at every point to do geometric operations Once the line has swept over every point, the program is done (In some situations, sweeping more than once is required) This can be used to find: The minimum distance between any 2 points on a plane The point where a certain amount of rectangles overlap on a plane The total area covered by the union of some rectangles on a plane (not shown in this presentation) All intersections of line segments on a plane (not shown in this presentation)

  3. HOW DOES FINDING THE MINIMUM DISTANCE WORK? Sort all of the points by their x value Store the current smallest distance between points (h, h = INF originally) Store a list of all elements whose x coordinate is within h of the current x coordinate (and then order the elements in this list by their y coordinate) When we move to a new element, remove all elements whose x value is now greater than h from the current point. Run through all elements in that list whose y value is within h of the current y coordinate If the distance between the current point and that point is less than h, update h The solution can be brute forced in O(n2) by comparing every point, but the line sweep works in O(nlogn), making it much more efficient finding closest pair of points in O(n log n) time

  4. SETUP CODE:

  5. SOLVING CODE:

  6. CODEFORCE 1028C - RECTANGLES Given: the bottom left and top right coordinate of n rectangles on a plane Find a point that is on the boundary of or is inside n-1 or n rectangles (there can be more than one answer) Brute force is too slow finding closest pair of points in O(n log n) time

  7. CODEFORCE 1028C - RECTANGLES SOLUTION Run a vertical line through the starting point of every rectangle, store the x value of the ending point of every new rectangle * When we move to a new rectangle, remove all rectangles that are now too far away (using their endpoint) If the line is covering n-1 or n rectangles, store that x Repeat with a horizontal line to find the y values Go through every combination of those x and y values to see if any work (this is not optimal, but there will be at most 2 of each, so it doesn t matter here) Another solution is to do a horizontal line sweep every time the vertical line is covering n-1 or n rectangles, but only line sweep through the rectangles that are being covered by the vertical lines. finding closest pair of points in O(n log n) time * As multiple rectangles can have the same x value at the end point, a multiset must be used over a set, unlike in the previous example

  8. CODEFORCE 1028C - RECTANGLES Eg: The vertical line only finds x = 1 The horizontal line finds y = 0 and 1 Test (1,0) and (1,1) to find that (1,1) works

  9. MAIN CLASS SETUP CODE SOLVING METHOD SETUP

  10. VERTICAL SWEEP HORIZONTAL SWEEP CODE While this code does work for the codeforce problem, it can be easily optimised by exiting the outer for-loop once two rectangles have been removed RUNNING THROUGH POSSIBLE SQUARES

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#