views:

3630

answers:

11
+1  A: 

I found this link which may be useful. There does not seem to be a definitive answer though. Google answers. Another reference for three circles is Haruki's theorem. There is a paper there as well.

Greg Reynolds
+17  A: 

Hi

I'm sure there is a clever algorithm, but here's a dumb one to save having to look for it;

-- put a bounding box around the circles;

-- generate random points within the bounding box;

-- figure out whether the random point is inside one of the circles;

-- compute the area by some simple addition and division (proportion_of_points_inside*area_of_bounding_box).

Sure it's dumb, but:

-- you can get as accurate an answer as you want, just generate more points;

-- it will work for any shapes for which you can calculate the inside/outside distinction;

-- it will parallelise beautifully so you can use all your cores.

Regards

Mark

High Performance Mark
This will work, but Monte-Carlo methods like this one, based simply on uniform sampling, generally don't have the best convergence rates.
ShreevatsaR
Sorry, but even though I appreciate your effort and think your solution is "practically usable", I consider your approach to be very wrong. This is a problem than can and should be solved by means of math, not brute force. Wasting energy and cores on problems like this is wasteful and lavish.
mafutrct
You're right, I'm ashamed of myself, but I've got a cluster with 12,000 cores, I can afford to be lavish. And I can't figure out how to make the elegant mathematical solution scale to that many processors.
High Performance Mark
There's nothing inherently wrong with a Monte-Carlo (or any randomized) approach, provided it gives the required degree of accuracy and does so in a reasonable amount of time.
MAK
+9  A: 
Leon Bambrick
I think doing the kind of polygon division suggested by your image would probably be a very good approach. There are a lot of details to work out to code it up. How would it handle a chain of twenty circles, each of which overlaps only the last and next in the chain? Easy to figure out by hand, but what is your algorithm?
PeterAllenWebb
+2  A: 

Hmm, very interesting problem. My approach would probably be something along the lines of the following:

  • Work out a way of working out what the areas of intersection between an arbitrary number of circles is, i.e. if I have 3 circles, I need to be able to work out what the intersection between those circles is. The "Monte-Carlo" method would be a good way of approximating this (http://local.wasp.uwa.edu.au/~pbourke/geometry/circlearea/).
  • Eliminate any circles that are contained entirely in another larger circle (look at radius and the modulus of the distance between the centre of the two circles) I dont think is mandatory.
  • Choose 2 circles (call them A and B) and work out the total area using this formula:

(this is true for any shape, be it circle or otherwise)

area(A∪B) = area(A) + area(B) - area(A∩B)

Where A ∪ B means A union B and A ∩ B means A intersect B (you can work this out from the first step.

  • Now keep on adding circles and keep on working out the area added as a sum / subtraction of areas of circles and areas of intersections between circles. For example for 3 circles (call the extra circle C) we work out the area using this formula:

(This is the same as above where A has been replaced with A∪B)

area((A∪B)∪C) = area(A∪B) + area(C) - area((A∪B)∩C)

Where area(A∪B) we just worked out, and area((A∪B)∩C) can be found:

area((A∪B)nC) = area((A∩C)∪(B∩C)) = area(A∩C) + area(A∩B) - area((A∩C)∩(B∩C)) = area(A∩C) + area(A∩B) - area(A∩B∩C)

Where again you can find area(A∩B∩C) from above.

The tricky bit is the last step - the more circles get added the more complex it becomes. I believe there is an expansion for working out the area of an intersection with a finite union, or alternatively you may be able to recursively work it out.

Also with regard to using Monte-Carlo to approximate the area of itersection, I believe its possible to reduce the intersection of an arbitrary number of circles to the intersection of 4 of those circles, which can be calculated exactly (no idea how to do this however).

There is probably a better way of doing this btw - the complexity increases significantly (possibly exponentially, but I'm not sure) for each extra circle added.

Kragen
Whats up with the formatting? Also sorry about the use of n and u for intersection and union, there is probably a better way...
Kragen
added some unicode union (∪) and intersection (∩) signs. hopefully they work.
Spoike
+58  A: 
Ants Aasma
What happens when there is a hole in the center?
John Gietzen
You'll need to subtract the center connected polygon for the hole from the total and add the circle slices for that polygon to the total.
Ants Aasma
Very nice. Thank you!
Anton Hansson
+1 for a really nice visual description of the problem.
Robert Koritnik
nice but I guess this will need to a lot of implementation details to handle all the special cases ( circle inside other one, no intersection, holes , one point contact ... )
fa.
The special cases are pretty easy. Circles inside others are discarded by not having any perimeter intersections. One point contact is in effect two intersections with zero distance. Disconnected shapes can be find via connected components algorithm over the graph where two circles are connected if distance of centers is less than sum of the radii. Holes are all polygons except the one with biggest area. Perimeter intersections are all intersections that aren't strictly inside any circle.
Ants Aasma
yes, but the borders of holes are also (small) arcs. I still think this needs a lot of code to work well.
fa.
this is a nice explanation but its got some major problems like someone who pointed out the holes, and furthermore N circles can have N^2 intersections, so it doesn't give a very efficient algorithm at all.
ldog
@gmatt: It's a very efficient mathematical/geometrical algorithm. Not everything has to be programming related here; theory is just as acceptable. Keep in mind, also, that not every correct algorithm is an efficient one.
Randolpho
To reiterate: if there's a hole, do the same algorithm suggested by the Ants's picture, but inside-out! Each of its edges is an arc; draw radii from the each end of the arc to the center, and you'll have another polygon (sometimes a star shape). The area of the hole is the area of the star minus the area of the wedges.
Jason Orendorff
+1  A: 

Depending on what problem you are trying to solve it could be sufficient to get an upper and lower bound. An upper bound is easy, just the sum of all the circles. For a lower bound you can pick a single radius such that none of the circles overlap. To better that find the largest radius (up to the actual radius) for each circle so that it doesn't overlap. It should also be pretty trivial to remove any completely overlapped circles (All such circles satisfy |P_a - P_b| <= r_a) where P_a is the center of circle A, P_b is the center of circle B, and r_a is the radius of A) and this betters both the upper and lower bound. You could also get a better Upper bound if you use your pair formula on arbitrary pairs instead of just the sum of all the circles. There might be a good way to pick the "best" pairs (the pairs that result in the minimal total area.

Given an upper and lower bound you might be able to better tune a Monte-carlo approach, but nothing specific comes to mind. Another option (again depending on your application) is to rasterize the circles and count pixels. It is basically the Monte-carlo approach with a fixed distribution.

fryguybob
+10  A: 

For a different solution from the previous one you could produce an estimation with an arbitrary precision using a quadtree.

This also works for any shape union if you can tell if a square is inside or outside or intersects the shape.

Each cell has one of the states : empty , full , partial

The algorithm consists in "drawing" the circles in the quadtree starting with a low resolution ( 4 cells for instance marked as empty). It cell is either :

  • inside at least one circle, then mark the cell as full,
  • outside all circles, mark the cell as empty,
  • else mark the cell as partial.

When its done, you can compute an estimation of the area : the full cells give the lower bound, the empty cells give the higher bound, the partial cells give the max area error.

If the error is too big for you, you refine the partial cells until you get the right precision.

I think this will be easier to implement than the geometric method which may require to handle a lot of special cases.

fa.
My **guess** is that this will converge more rapidly than the monte carlo inside/outside point algorithm too.
Frank Krueger
yes it should spend time only on "complex" areas.
fa.
This does seem a lot easier to implement. Definitely the best brute force method suggested. Thanks!
Anton Hansson
brute force here is called squeeze theorem
fa.
That's the kind of algorithm you use in interval arithmetic. http://en.wikipedia.org/wiki/Interval_arithmetic
rjmunro
Interesting link, thanks
fa.
+2  A: 

If you want a discrete (as opposed to a continuous) answer, you could do something similar to a pixel painting algorithm.

Draw the circles on a grid, and then color each cell of the grid if it's mostly contained within a cirle (i.e., at least 50% of its area is inside one of the circles). Do this for the entire grid (where the grid spans all of the area covered by the circles), then count the number of colored cells in the grid.

Loadmaster
+1  A: 

Here's an algorithm that should be easy to implement in practice, and could be adjusted to produce arbitrarily small error:

  1. Approximate each circle by a regular polygon centered at the same point
  2. Calculate the polygon which is the union of the approximated circles
  3. Calculate the area of the merged polygon

Steps 2 and 3 can be carried out using standard, easy-to-find algorithms from computational geometry.

Obviously, the more sides you use for each approximating polygon, the closer to exact your answer would be. You could approximate using inscribed and circumscribed polygons to get bounds on the exact answer.

PeterAllenWebb
+1  A: 

I have been working on a problem of simulating overlapping star fields, attempting to estimate the true star counts from the actual disk areas in dense fields, where the larger bright stars can mask fainter ones. I too had hoped to be able to do this by rigorous formal analysis, but was unable to find an algorithm for the task. I solved it by generating the star fields on a blue background as green disks, whose diameter was determined by a probability algorithm. A simple routine can pair them to see if there's an overlap (turning the star pair yellow); then a pixel count of the colours generates the observed area to compare to the theoretical area. This then generates a probability curve for the true counts. Brute force maybe, but it seems to work OK.

A: 

There are efficient solutions to this problem using what are known as power diagrams. This is really heavy math though and not something that I would want to tackle offhand. For an "easy" solution, look up line-sweep algorithms. The basic principle here is that that you divide the figure up into strips, where calculating the area in each strip is relatively easy.

So, on the figure containing all of the circles with nothing rubbed out, draw a horizontal line at each position which is either the top of a circle, the bottom of a circle or the intersection of 2 circles. Notice that inside these strips, all of the areas you need to calculate look the same: a "trapezium" with two sides replaced by circular segments. So if you can work out how to calculate such a shape, you just do it for all the individual shapes and add them together. The complexity of this naive approach is O(N^3), where N is the number of circles in the figure. With some clever data structure use, you could improve this line-sweep method to O(N^2 * log(N)), but unless you really need to, it's probably not worth the trouble.

Steve Thomas