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
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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
Название |
---|
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?