Darryl Burke wrote:Iterate over the List and update the Set<Circle> of each.
This is the hard part. Suppose you have a series of circles where 1 overlaps 2, 2 overlaps 3, 3 overlaps 4, 4 overlaps 5, and so on, but there aren't any other overlaps. You want 1 to be in the same set as 5, but you have to do four separate overlap tests before you can find that out.
(From the graph theory point of view: the problem is to decompose a graph into its connected components. I googled for this but I couldn't find anything which specifically discussed that problem. Apparently the graph theorists must think it's too trivial to bother with because everything I did find was for more complicated problems.)
Here's my rough cut at the problem:
1. Start with the first circle. (This assumes you have the circles in an ordered list.) Assign it a colour (say red).
2. Next see which other circles overlap the circles which have already been assigned that colour. Assign them the same colour, and keep track of whether this changes anything. Repeat that until it doesn't change anything, after which you have identified all the circles in the red blob.
3. Now get the next circle which doesn't have a colour yet. (If there isn't any such circle then you're finished.) Assign it a different colour and repeat step 2 for that colour. Repeat this until you're finished.
This should turn into a loop inside a loop once you notice that step 3 is the same as step 1, just a special case.
I'm sure it isn't the fastest way to solve the problem, but the other one I have in mind involves recursive processing and would be harder to explain. A slow method is probably good enough if you don't have thousands of circles to deal with.