-
Notifications
You must be signed in to change notification settings - Fork 775
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added circle circle intersection area
- Loading branch information
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Author: Takanori MAEHARA, chilli | ||
* Date: 2019-11-03 | ||
* License: CC0 | ||
* Source: https://github.com/spaghetti-source/algorithm/blob/master/geometry/_geom.cc#L729 | ||
* Description: Calculates the area of the intersection of 2 circles | ||
* Status: | ||
*/ | ||
|
||
template<class P> | ||
double intersection_area(P c, double cr, P d, double dr) { | ||
if (cr < dr) swap(c, d); | ||
auto A = [&](double r, double h) { | ||
return r*r*acos(h/r)-h*sqrt(r*r-h*h); | ||
}; | ||
auto l = (c - d).dist(), a = (l*l + cr*cr - dr*dr)/(2*l); | ||
if (sgn(l - cr - dr) >= 0) return 0; // far away | ||
if (sgn(l - cr + dr) <= 0) return M_PI*dr*dr; | ||
if (sgn(l - cr) >= 0) return A(cr, a) + A(dr, l-a); | ||
else return A(cr, a) + M_PI*dr*dr - A(dr, a-l); | ||
} |