Can anyone explain the algorithm to solve area of union of circles problem( given n circles + centre and radius of each)
Thankyou
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
Name |
---|
The main idea of counting the area of union of some figures is that we draw vertical lines throug the "interesting" points ans solve the problem in each of the stripes between two adjacent lines.
The interesting points here will be the most left and right points on each circle and all the intersection points. There will be O(n2) interesting points.
Inside each stripe there will be some disjoint arcs, so we can sort all the arcs by their y-coordinates and count the area of union in O(n) complexity using, may be, stack (that arcs will form correct parthenesis sequence) and some integrals.
So we have the O(n3 log n) algorithm. It can be reduced to O(n2 log n) by looking on the coefficients in the integral and understanding how they change from strip to strip, so we can use some data structure to update them fast.
Do you know how to find the length of the union of the segments on the line in O(nlogn)? Here is the same: when we process opening arc and the balance is 0 we add area under this arc to the sum. When we process closing arc and balance becomes 0 (or, the same, balance is currentlty 1) we substract the area under this arc from sum.
UPD: The balance is the difference between opening arcs and closing arcs. Oblivious that it is non-negative at all the time.
That was brainsmashing. Hours of debugging...
Advice: if you decide to write it, insert lots of asserts to check wheter variable under square root/arctan/denumerators of rationals are all correct. It will save your time.
This problem literally asks you to eliminate the circles that are inside some other circle and add up the areas of the dominating circles.
You can check if a circle is inside another using sweepline.
Main idea:
Sweep from left to right. Insert circles at time cx - r and erase them at cx + r (break ties by r decreasingly).
The sweepline status will be the set of active circles sorted by their cy coordinate.
Before inserting any circle, check it against its immediate predecessor and successor (they are the only candidates for inclusion). If you find that it's included, simply do not insert it into the set. Else, add its area to the answer and insert it into the set.
This can be solved using Green's Theorem, with a complexity of n^2log(n). If you're not familiar with the Green's Theorem and want to know more, here is the video and notes from Khan Academy. But for the sake of our problem, I think my description will be enough. The general equation of Green's Theorem is
If I put L and M such that
then the RHS is simply the area of the Region R and can be obtained by solving the closed integral or LHS and this is exactly what we're going to do.
So Integrating along the path in the anticlockwise gives us the Area of the region and integrating along the clockwise gives us negative of the Area. So
AreaOfUnion = (Integration along red arcs in anticlockwise direction + Integration along blue arcs in clockwise direction)
But the cool trick is if for each circle if we integrate the arcs which are not inside any other circle we get our required area i.e. we get integration in an anticlockwise direction along all red arcs and integration along all blue arcs along the clockwise direction. JOB DONE!!!
Here is the GitHub link to my C++ Code
This code is not passing Area of Union of Circles on SphereOnlineJudge
Problem Links:
Any uncovered corner cases?
I used the same approach and my solutions worked in both cases. There must some other problem in your code! Have you excluded the circles having the same center and radius? If some circles have the same centre and radius take only one of them.
could you please explain how your code works?