It is necessary to find the area of intersection of two circles (I took the formulas here ):
S = S 1 + S 2
Where,
- R 1 is the radius of the first circle;
- R 2 is the radius of the second circle;
- D is the distance between the centers of the circles.
Code:
float find_area(float x1, float y1, float r1, float x2, float y2, float r2) { // Расстояние между центрами окружностей float distance = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); float f1, f2; float s1, s2; if(distance >= r1 + r2) { // Если не пересекаются (можно дополнить вычислением площади внутренней окружности) return 0; }else if(distance <= fabs(r1 - r2)) { // Если окружность внутри другой return 0; } else { // Если пересекаются f1 = 2 * acos((r1*r1 - r2*r2 - distance*distance) / (2 * r1 * distance)); f2 = 2 * acos((r2*r2 - r1*r1 - distance*distance) / (2 * r2 * distance)); s1 = (r1*r1 * (f1 - sin(f1))) / 2; s2 = (r2*r2 * (f2 - sin(f2))) / 2; return s1 + s2; } return 0; } Area of intersection of circles
(x 1 = 0; y 1 = 0; r 1 = 2) ∩ (x 2 = 3; y 2 = 0; r 2 = 4)
It should be about 9.57019, but I get something incomprehensible:
-1. # IND00




+D^2, and you have a minus. - Yura Ivanovdoubleinstead offloatin such calculations, if there are no special restrictions on this account. - Zealint