Given 'n' circles (each defined by center and radius) Write an algorithm to detect if given circles intersect with any other circles in the same plane
Better than O(n^2) complexity
# | User | Rating |
---|---|---|
1 | tourist | 3947 |
2 | jiangly | 3734 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3612 |
7 | ecnerwala | 3581 |
8 | Geothermal | 3569 |
8 | cnnfls_csy | 3569 |
10 | ksun48 | 3479 |
# | User | Contrib. |
---|---|---|
1 | awoo | 162 |
2 | maomao90 | 160 |
3 | nor | 156 |
4 | cry | 155 |
4 | adamant | 155 |
4 | atcoder_official | 155 |
4 | -is-this-fft- | 155 |
8 | maroonrk | 153 |
9 | SecondThread | 147 |
10 | Petr | 146 |
Given 'n' circles (each defined by center and radius) Write an algorithm to detect if given circles intersect with any other circles in the same plane
Better than O(n^2) complexity
Name |
---|
If I got the problem correctly, for each circle you just need to loop though all others and compare the distance between two centers versus the sum of two radiuses. Two circles intersect if the former is not greater than the latter.
The time complexity for the above approach would be O(n^2) ______
Updated 1: Sorry the time complexity is not good enough
Yes, but a better approach O(NlogN) is reqd.
Sweep line, make a vertical line sweep from x=-inf to x=+inf, whenever it touch a circle from left insert the Y coordinates of its center to a
std::set
and check if this circle intersects with the two circles which become immediately before and after this circle in thestd::set
, whenever the line touches from a circle from right remove that circle from set now check if the two circles which was before and after the removed circle in thestd::set
intersect with each other.UPD: I considered that intersecting of two circles means having a common area
Can u explain using some example.
A brief example would be very useful.
Give me an example you want me to explain
Any example of choice where I can visualise the algo?
Maybe 4 circles like this Pic where
1 intersects 2, 3, 4
2 intersects 3, 4
3 intersects 4
or any other to get intuitive explanation.
if you have 4 circles such that all pairs intersect with each other then here's how the algorithm will work:
the sweeping line will move until it touches the first circle from the left, you will insert its Y coordinates to
std::set
now the line will continue sweeping until it touches another circle from left and you will add its Y coordinates tostd::set
, since there was only one circle in thestd::set
the newly added circle will come either after or before it so you have to check if those two circles intersect and they indeed intersect because all circles intersect with each other so you just return that there's intersection and terminate the algorithm.This link may help you
Do I have to say whether there are 2 circles that intersect or the pairs of circles that intersect?