Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>These kind of questions are largely answered by some standard <code>Computational Geometry</code> algorithms. I can think of a <code>vertical sweep line</code> algorithm for this particular problem.</p> <p>Assuming a rectangle is represented by a pair of points <code>(p1, p2)</code>, where <code>p1</code> is the upper left corner and <code>p2</code> is the bottom right corner. And a point has two attributes (can be accessed as <code>p.x</code> and <code>p.y</code>).</p> <p>Here is the algorithm.</p> <ol> <li>Sort all the pairs of points - <code>O(n log n)</code></li> <li>Initialize a list called <code>sweep line status</code>. This will hold all the rectangles that are encountered till now, that are <code>alive</code>. Also initialize another list called <code>event queue</code> that holds upcoming events. This <code>event queue</code> currently holds starting points of all the rectagles.</li> <li>Process the events, start from the first element in the <code>event queue</code>. <ul> <li>If the event is a <code>start point</code>, then add that rectangle to <code>sweep line status</code> (in sorted order by y-coordinate) (in <code>O(log n)</code> time) and add its bottom-right point to the <code>event queue</code> at the appropriate position (sorted by the points) (again in <code>O(log n)</code> time). When you add it to <code>sweep line status</code>, you just need to check if this point lies in the rectangle alive just above it in the <code>sweep line status</code>. If it does lie inside, this is not your rectangle, otherwise, add this to your list of required rectangles.</li> <li>If the event is an end point, just remove the correspoinding rectangle from the <code>sweep line status</code>.</li> </ul></li> </ol> <p>Running time (for n rectangles):</p> <ul> <li>Sorting takes <code>O(n log n)</code>.</li> <li>Number of events = 2*n = <code>O(n)</code></li> <li>Each event takes <code>O(log n)</code> time (for insertions in <code>event queue</code> as well as <code>sweep line status</code>. So total is <code>O(n log n)</code>.</li> </ul> <p>Therefore, <code>O(n log n)</code>.</p> <p>For more details, please refer to <a href="http://en.wikipedia.org/wiki/Bentley%E2%80%93Ottmann_algorithm" rel="nofollow">Bentley–Ottmann algorithm</a>. The above just a simple modification of this.</p> <p>EDIT:</p> <p>Just realized that input is in terms of line segments, but since they always form rectangles (according to question), a linear traversal for a pre-process can convert them into the rectangle (pair of points) form.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload