
Calculating the intersection area of 3 or more circles - madflame991
http://www.benfrederickson.com/calculating-the-intersection-of-3-or-more-circles/
======
benfrederickson
Author here. I wrote this post a while back, but never submitted to HN. Its
one of those posts that I wasn't sure if it would really resonate with anyone
(like my most recent one on venn diagram layout algorithms).

Anyways, If you have any questions ask away =).

~~~
shuzchen
I notice that when you're trying to calculate whether a point (p) is in a
circle (c) you use euclidean distance:

    
    
      Math.sqrt((p.x - c.x) ^ 2 + (p.y - c.y) ^ 2) > c.radius
    

If you're doing this test many, many times (especially as in a Monte Carlo
simulation), it'd be more efficient to use distanceSquared:

    
    
      (p.x - c.x) ^ 2 + (p.y - c.y) ^ 2 > c.radius ^ 2
    

Not having to calculate square roots will cut the compute time drastically.

~~~
benfrederickson
good point! I actually used this technique recently, to speed up a numerical
optimization for laying out venn diagrams:
[http://www.benfrederickson.com/better-venn-
diagrams/](http://www.benfrederickson.com/better-venn-diagrams/)

I didn't think of it when I was writing this post though =(

------
jheriko
there is a lesson here. think about the problem before researching it.

i've seen a lot of terrible algorithms come from people reading papers which
provide solutions that are worse than they could come up with themselves. they
may even assume that the bad algorithm must have some purpose to its
badness... like its being done for a complicated reason they didn't think of.

The truth is that a paper might not even be written for practical
implementation, but to tick some academic boxes.

reinventing wheels is bad, but copying bad wheels is considerably worse.

------
jestar_jokin
I actually appreciate this post, for showing that people other than myself
also over-think solutions. When most of my software development work deals
with managing code complexity, vs actual algorithmic work, I often feel silly
when I spend too long working on a problem with a simple solution. Luckily,
analysis and learning is part of the job description!

------
enedil
The method used here is quite obvious for a good-at-maths primary school
student.

~~~
function_seven
Agreed, though only in retrospect for me. When I first saw the problem—before
I looked at any of the solutions—I found myself quickly mired down thinking
about how to formulate an integral to calculate the area.

The polygon-plus-border-segments method requires no calculus.
[http://www.mathopenref.com/segmentarea.html](http://www.mathopenref.com/segmentarea.html)

~~~
benfrederickson
Once I figured out the solution, I was kicking myself for not figuring it our
earlier =)

I think I went wrong in 2 ways. My first attempt was to solve using calculus,
and though I came up with a solution for the 2 circle case - I totally failed
at finding the integral for 3+ circles.

My bigger mistake was to google for papers after I failed initially. I looked
for other peoples venn diagram solutions, and found that they were using
approximation techniques to calculate. Since the people writing these papers
have a much better math pedigree than I do, it made me think I should use an
approximation too.

While I did waste a bit of extra time on this, most of the time was spent in
writing javascript code. Since the whole point of the venn diagram library in
the first place was to learn javascript, I can’t really say it was a total
waste. Also I used the quad tree estimate to verify the exact solution.

~~~
darkmighty
In your defense, the quadtree method is more easily generalizeable to general
figures :)

But indeed I believe there's a very good generalization of the intersection
method (the exact one). If you have a general curve and an algebraic
description, you can find the intersections roughly using something akin to
the quadtree method and then use something like Newton's method to find the
precise intersections. It's root finding essentially (basic numerical
analysis), you can find very general and good methods on wiki. And then once
you have the intersections, you only have to compute some internal polygons
and then calculate some integrals. Again, this integrals can be calculated
using standard methods like Runge-Kutta. Altogether this gives a very general
method with much faster convergence and lower memory (quadratic convergence
versus linear convergence for quadtree, provided your boundary satisfies some
not too strict conditions).

------
chrisbennet
Great post! Am I the only one whom, when seeing such "simple" problems, thinks
"Thank goodness I didn't have to answer this in an interview, on a white
board."

------
osoba
This is just out of the top of my head so could be wrong but isnt't there a
simple analytic solution by using Riemann sums:

Pick any point in the intersection A (can be on border too). Divide the
interval from 0 to 2pi into many small chunks of length delta-theta.

1/2 * Sum for n from 0 to 2pi in delta-theta chunks of min(distance from A to
each circle in n angle direction)*delta-theta should be the area of
intersection.

