Skip to content

Commit ed27477

Browse files
committed
Added circle circle intersection area
1 parent 28c2cbf commit ed27477

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

content/geometry/CircleCircleArea.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Author: Takanori MAEHARA, chilli
3+
* Date: 2019-11-03
4+
* License: CC0
5+
* Source: https://github.com/spaghetti-source/algorithm/blob/master/geometry/_geom.cc#L729
6+
* Description: Calculates the area of the intersection of 2 circles
7+
* Status:
8+
*/
9+
10+
template<class P>
11+
double intersection_area(P c, double cr, P d, double dr) {
12+
if (cr < dr) swap(c, d);
13+
auto A = [&](double r, double h) {
14+
return r*r*acos(h/r)-h*sqrt(r*r-h*h);
15+
};
16+
auto l = (c - d).dist(), a = (l*l + cr*cr - dr*dr)/(2*l);
17+
if (sgn(l - cr - dr) >= 0) return 0; // far away
18+
if (sgn(l - cr + dr) <= 0) return M_PI*dr*dr;
19+
if (sgn(l - cr) >= 0) return A(cr, a) + A(dr, l-a);
20+
else return A(cr, a) + M_PI*dr*dr - A(dr, a-l);
21+
}

0 commit comments

Comments
 (0)