Given the coordinates of the centers 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
Examples:
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.
Input: X1 = 0, Y1 = 0, R1 = 5, X2 = 11, Y2 = 0, R2 = 5
Output: 0
Explanation: The circles don't intersect.
Approach: Follow the steps to solve this problem:
- Firstly calculate the euclidean distance between the two points and store it (say d).
- If, d > R1 + R2, then the circle never intersects, so, return 0.
- Else if, d ? (R1 - R2) and R1 ? R2, then circle with radius R2 is inside the circle with radius R1, so, return floor(Pi * R2 * R2).
- Else if, d ? (R2 - R1) and R2 ? R1, then circle with radius R1 is inside the circle with radius R2, so, return floor(Pi * R1 * R1).
- Else, find:
- alpha = acos((R1 * R1 + d * d - R2 * R2) / (2 * R1 * d)) * 2 [acos is inverse cosine]
- 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)
- Return, the value of floor(a1+a2).
Below is the implementation of the above approach.
Time Complexity: O(log N)
Auxiliary Space: O(1)