Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An efficient way of computing this area is to use a sweep algorithm. Let us assume that we sweep a vertical line L(x) through the union of rectangles U: </p> <ul> <li>first of all, you need to build an event queue Q, which is, in this case, the ordered list of all x-coordinates (left and right) of the rectangles. </li> <li>during the sweep, you should maintain a 1D datastructure, which should give you the total length of the intersection of L(x) and U. The important thing is that this length is constant between two consecutive events q and q' of Q. So, if l(q) denotes the total length of L(q+) (i.e. L just on the rightside of q) intersected with U, the area swept by L between events q and q' is exactly l(q)*(q' - q).</li> <li>you just have to sum up all these swept areas to get the total one.</li> </ul> <p>We still have to solve the 1D problem. You want a 1D structure, which computes dynamically a union of (vertical) segments. By dynamically, I mean that you sometimes add a new segment, and sometimes remove one. </p> <p>I already detailed in my answer to this <a href="https://stackoverflow.com/questions/149577/need-an-algorithm-for-collapsing-netblock-ranges-into-lists-of-superset-ranges">collapsing ranges question</a> how to do it in a static way (which is in fact a 1D sweep). So if you want something simple, you can directly apply that (by recomputing the union for each event). If you want something more efficient, you just need to adapt it a bit:</p> <ul> <li>assuming that you know the union of segments S<sub>1</sub>...S<sub>n</sub> consists of disjoints segments D<sub>1</sub>...D<sub>k</sub>. Adding S<sub>n+1</sub> is very easy, you just have to locate both ends of S<sub>n+1</sub> amongs the ends of D<sub>1</sub>...D<sub>k</sub>.</li> <li>assuming that you know the union of segments S<sub>1</sub>...S<sub>n</sub> consists of disjoints segments D<sub>1</sub>...D<sub>k</sub>, removing segment S<sub>i</sub> (assuming that S<sub>i</sub> was included in D<sub>j</sub>) means recomputing the union of segments that D<sub>j</sub> consisted of, except S<sub>i</sub> (using the static algorithm).</li> </ul> <p>This is your dynamic algorithm. Assuming that you will use sorted sets with log-time location queries to represent D<sub>1</sub>...D<sub>k</sub>, this is probably the most efficient non-specialized method you can get.</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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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