Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combined area of overlapping circles

I recently came across a problem where I had four circles (midpoints and radius) and had to calculate the area of the union of these circles.

Example image:

For two circles it's quite easy,

I can just calculate the fraction of the each circles area that is not within the triangles and then calculate the area of the triangles.

But is there a clever algorithm I can use when there is more than two circles?

like image 723
Anton Hansson Avatar asked Nov 03 '09 13:11

Anton Hansson


People also ask

What are two overlapping circles called?

A Venn diagram consists of multiple overlapping closed curves, usually circles, each representing a set.


10 Answers

Find all circle intersections on the outer perimeter (e.g. B,D,F,H on the following diagram). Connect them together with the centres of the corresponding circles to form a polygon. The area of the union of the circles is the area of the polygon + the area of the circle slices defined by consecutive intersection points and the circle center in between them. You'll need to also account for any holes.

circle overlap

like image 189
Ants Aasma Avatar answered Sep 24 '22 19:09

Ants Aasma


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.
like image 41
High Performance Mark Avatar answered Sep 26 '22 19:09

High Performance Mark


Ants Aasma's answer gave the basic idea, but I wanted to make it a little more concrete. Take a look at the five circles below and the way they've been decomposed.

Example

  • The blue dots are circle centers.
  • The red dots are circle boundary intersections.
  • The red dots with white interior are circle boundary intersections that are not contained in any other circles.

Identifying these 3 types of dots is easy. Now construct a graph data structure where the nodes are the blue dots and the red dots with white interior. For every circle, put an edge between the circle middle (blue dot) and each of its intersections (red dots with white interior) on its boundary.

This decomposes the circle union into a set of polygons (shaded blue) and circular pie pieces (shaded green) that are pairwise disjoint and cover the original union (that is, a partition). Since each piece here is something that's easy to compute the area of, you can compute the area of the union by summing the pieces' areas.

like image 25
Timothy Shields Avatar answered Sep 24 '22 19:09

Timothy Shields


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). Each 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 it's 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.

like image 24
fa. Avatar answered Sep 24 '22 19:09

fa.


I love the approach to the case of 2 intersecting circles -- here's how i'd use a slight variation of the same approach for the more complex example.

It might give better insight into generalising the algorithm for larger numbers of semi-overlapping circles.

The difference here is that i start by linking the centres (so there's a vertice between the centre of the circles, rather than between the places where the circles intersect) I think this lets it generalise better.

(in practice, maybe the monte-carlo method is worthwhile)

alt text
(source: secretGeek.net)

like image 33
Leon Bambrick Avatar answered Sep 27 '22 19:09

Leon Bambrick


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.

like image 32
David R Tribble Avatar answered Sep 24 '22 19:09

David R Tribble


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.

like image 22
Justin Avatar answered Sep 28 '22 19:09

Justin


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.

(source: 2from.com)

like image 3
user213660 Avatar answered Sep 24 '22 19:09

user213660


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.

like image 3
Steve Thomas Avatar answered Sep 28 '22 19:09

Steve Thomas


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.

like image 2
PeterAllenWebb Avatar answered Sep 25 '22 19:09

PeterAllenWebb