Calculating the intersection area of 3 or more circles
benfrederickson.com
benfrederickson.com
Anyways, If you have any questions ask away =).
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.I didn't think of it when I was writing this post though =(
Is that something you did in your research?
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.
The polygon-plus-border-segments method requires no calculus. http://www.mathopenref.com/segmentarea.html
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.
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).
https://en.wikipedia.org/wiki/Green%27s_theorem#Area_Calcula...
In fact, the algorithm for the area of the interior polygon that you are using itself (shoelace algorithm) uses green's theorem 'in stealth mode'.
In the UK chords aren't mentioned in the maths curriculum until KS4 (late secondary school, 14-16yo) - https://www.gov.uk/government/publications/national-curricul....
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.