It is necessary to find the area of ​​intersection of two circles (I took the formulas here ):

enter image description here enter image description here

enter image description here enter image description 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

  • 3
    +D^2 , and you have a minus. - Yura Ivanov
  • 2
    I recommend using double instead of float in such calculations, if there are no special restrictions on this account. - Zealint
  • four
    So wait. At first I thought that you use the word circle in the ordinary sense, and not in the mathematical sense. In the mathematical sense, any circle has zero area (this is a thin line), so any intersection with anything in any case will have zero area. So even the formulas are not needed. And I gave a comment for the word "circle". - Zealint
  • 2
    In fairness, the "intersection of the circles" may have area. They do not need to be in circles to select a section on a plane. - vp_arth
  • 2
    @vp_arth: From a set-theoretic point of view, the intersection of two sets is a subset of any of them. So the intersection of circles in a typical case - two points, and not a limited area. - VladD

2 answers 2

The comments have already discussed everything, I’d just add it would be nice to check the acos arguments on [-1...1] , otherwise you know, there are circles such that the circle does not fit in them :-) In the sense that the formula is not strictly proven on the whole range of values. By the way, thanks to the author for the code! :-)

  • Obviously, the distance between the circles must be less than or equal to the sum of the radii, otherwise there will simply be no intersection. And there is a check above - 0andriy

There is an error in the code!

 acos((r1*r1 - r2*r2 - distance*distance) 

Based on the formula, the square of the distance should be added