Skip to content

Instantly share code, notes, and snippets.

@vikaskanani
Created March 22, 2021 11:18
Show Gist options
  • Save vikaskanani/01be2b24055bfb488c501403cdf4eb61 to your computer and use it in GitHub Desktop.
Save vikaskanani/01be2b24055bfb488c501403cdf4eb61 to your computer and use it in GitHub Desktop.
Find area of two intersecting circles
//reference https://en.wikipedia.org/wiki/Lens_(geometry)
function findDelta (d, r, R) {
return (1 / 4) * Math.sqrt((-d + r + R) * (d - r + R) * (d + r - R) * (d + r + R) );
}
function area (d, r, R) {
const delta = findDelta(d, r, R);
const cosLeft = (Math.acos(((d * d) + (r * r) - (R * R)) / (2 * d * r)));
const cosRight = (Math.acos(((d * d) - (r * r) + (R * R)) / (2 * d * R)));
return (r * r) * (cosLeft) + (R * R) * cosRight - 2 * delta;
}
area(10,10,10);
area(1,10,10);
area(90,100,10);
area(20,10,10);
area(19,10,10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment