Area of intersection of two circles | Practice | GeeksforGeeks

Given the coordinates of the centres of two circles (X1, Y1) and (X2, Y2) as well as the radii of the respective circles R1 and R2.Find the floor of the area of their intersection.
Note: Use the value of Pi as 3.14
Example 1:
Input:
X1=0,Y1=0,R1=4
X2=6,Y2=0,R2=4
Output:
7
Explanation:
The intersecting area equals 7.25298806.
So,Answer is 7.
Example 2:
Input:
X1=0,Y1=0,R1=5
X2=11,Y2=0,R2=5
Output:
0
Explanation:
The circles don't intersect.

Your Task:
You don't need to read input or print anything. Your task is to complete the function intersectionArea() which takes the coordinates of the centers as well as the radii(X1, Y1, R1, X2, Y2, R2) as input parameters and returns the area of intersection of the two circles.

Expected Time Complexity:O(LogN)
Expected Auxillary Space:O(1)

Constraints:
-109<=X1,Y1,X2,Y2<=109
1<=R1,R2<=109
-4
Naveen Kotha8 months ago
Naveen Kotha
long double Pi = 3.14;        long double d, alpha, beta, a1, a2;        long long int ans;        d = sqrt((X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1));
        if (d > R1 + R2) ans = 0;        else if (d <= abs(R2 - R1) && R1 >= R2)             ans = floor(Pi * R2 * R2);
        else if (d <= abs(R2 - R1) && R2 >= R1)            ans = floor(Pi * R1 * R1);
        else {            alpha = acos((R1 * R1 + d * d - R2 * R2) / (2 * R1 * d)) * 2;            beta = acos((R2 * R2 + d * d - R1 * R1) / (2 * R2 * d)) * 2;            a1 = 0.5 * beta * R2 * R2 - 0.5 * R2 * R2 * sin(beta);            a2 = 0.5 * alpha * R1 * R1 - 0.5 * R1 * R1 * sin(alpha);            ans = floor(a1 + a2);          }     return ans;
-1
Hazem5 years ago
Hazem
There are three more cases not mentioned in the solution here http://code.geeksforgeeks.o... (and by the way, most solutions I found online). They are:
- d is less than one radius but larger than the other- d is less than both radii
In either case, one circle is almost inside the other. The intersection area is then the area of the smaller circle minus the difference of the circular segments (i.e. minus area(r1,r2,d)-area(r2,r1,d))
0
Quandray5 years ago
Quandray
Input:-11786939 388749051 844435993 -11696460 388789113 844535886
Its Correct output is:2240182216213578196
And Your Output is:2240182216213578283
For this test case, the smaller circle is inside the larger, so the overlap is the area of the smaller circle.   844435993*844435993*pi.I make that 2240182216213578283.58.
In my code I have the lineconst long double pi=3.1415926535897932384L;With that line, I get the answer 2240182216213578283If I remove the L from the end of that line, pi is shortened and I get the expected, but wrong, answer 2240182216213578196.Please can someone correct this.
-2
张昆玮6 years ago
张昆玮
I think my solution is correct. http://code.geeksforgeeks.o... . How can you show the expected output is more accurate than my solution?
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.