Area of intersection of two Circles

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 insects, 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.
C++
// C++ code to implement the approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to return area of intersectionlong long intintersectionArea(long double X1, long double Y1,                 long double R1, long double X2,                 long double Y2, long double R2){    long double Pi = 3.14;    long double d, alpha, beta, a1, a2;    long long int ans;Â
    // Calculate the euclidean distance    // between the two points    d = sqrt((X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1));Â
    if (d > R1 + R2)        ans = 0;Â
    else if (d <= (R1 - R2) && R1 >= R2)        ans = floor(Pi * R2 * R2);Â
    else if (d <= (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;}Â
// Driver Codeint main(){Â Â Â Â int X1 = 0, Y1 = 0, R1 = 4;Â Â Â Â int X2 = 6, Y2 = 0, R2 = 4;Â
    // Function Call    cout << intersectionArea(X1, Y1, R1, X2, Y2, R2);Â
    return 0;} |
Java
// Java code to implement the approachÂ
import java.io.*;Â
class GFG {Â
    // Function to return area of intersection    static int intersectionArea(int X1, int Y1, int R1,                                int X2, int Y2, int R2)    {        double Pi = 3.14;        double d, alpha, beta, a1, a2;        int ans;Â
        // Calculate the euclidean distance        // between the two points        d = Math.sqrt((X2 - X1) * (X2 - X1)                      + (Y2 - Y1) * (Y2 - Y1));Â
        if (d > R1 + R2)            ans = 0;Â
        else if (d <= (R1 - R2) && R1 >= R2)            ans = (int)Math.floor(Pi * (double)R2                                  * (double)R2);Â
        else if (d <= (R2 - R1) && R2 >= R1)            ans = (int)Math.floor(Pi * (double)R1                                  * (double)R1);Â
        else {            alpha = Math.acos((R1 * R1 + d * d - R2 * R2)                              / (2 * R1 * d))                    * 2;            beta = Math.acos((R2 * R2 + d * d - R1 * R1)                             / (2 * R2 * d))                   * 2;            a1 = 0.5 * beta * R2 * R2                 - 0.5 * R2 * R2 * Math.sin(beta);            a2 = 0.5 * alpha * R1 * R1                 - 0.5 * R1 * R1 * Math.sin(alpha);            ans = (int)Math.floor(a1 + a2);        }Â
        return ans;    }Â
    public static void main(String[] args)    {        int X1 = 0, Y1 = 0, R1 = 4;        int X2 = 6, Y2 = 0, R2 = 4;Â
        // Function Call        System.out.print(            intersectionArea(X1, Y1, R1, X2, Y2, R2));    }}Â
// This code is contributed by lokeshmvs21. |
Python3
# Python3 code to implement the approachÂ
from math import sqrt, acos, floor, sinÂ
# Function to return area of intersectiondef intersectionArea(X1, Y1, R1, X2, Y2, R2) :Â
    Pi = 3.14;         # Calculate the euclidean distance    # between the two points    d = sqrt(((X2 - X1) * (X2 - X1)) + ((Y2 - Y1) * (Y2 - Y1)));Â
    if (d > R1 + R2) :        ans = 0;Â
    elif (d <= (R1 - R2) and R1 >= R2) :        ans = floor(Pi * R2 * R2);Â
    elif (d <= (R2 - R1) and 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;Â
# Driver Codeif __name__ == "__main__" :Â
    X1 = 0; Y1 = 0; R1 = 4;    X2 = 6; Y2 = 0; R2 = 4;Â
    # Function Call    print(intersectionArea(X1, Y1, R1, X2, Y2, R2));         # This code is contributed by AnkThon |
C#
// C# code to implement the approachusing System;Â
public class GFG{Â
  // Function to return area of intersection  static int intersectionArea(int X1, int Y1, int R1,                              int X2, int Y2, int R2)  {    double Pi = 3.14;    double d, alpha, beta, a1, a2;    int ans;Â
    // Calculate the euclidean distance    // between the two points    d = Math.Sqrt((X2 - X1) * (X2 - X1)                  + (Y2 - Y1) * (Y2 - Y1));Â
    if (d > R1 + R2)      ans = 0;Â
    else if (d <= (R1 - R2) && R1 >= R2)      ans = (int)Math.Floor(Pi * (double)R2                            * (double)R2);Â
    else if (d <= (R2 - R1) && R2 >= R1)      ans = (int)Math.Floor(Pi * (double)R1                            * (double)R1);Â
    else {      alpha = Math.Acos((R1 * R1 + d * d - R2 * R2)                        / (2 * R1 * d))        * 2;      beta = Math.Acos((R2 * R2 + d * d - R1 * R1)                       / (2 * R2 * d))        * 2;      a1 = 0.5 * beta * R2 * R2        - 0.5 * R2 * R2 * Math.Sin(beta);      a2 = 0.5 * alpha * R1 * R1        - 0.5 * R1 * R1 * Math.Sin(alpha);      ans = (int)Math.Floor(a1 + a2);    }Â
    return ans;  }Â
  static public void Main (){    int X1 = 0, Y1 = 0, R1 = 4;    int X2 = 6, Y2 = 0, R2 = 4;Â
    // Function Call    Console.WriteLine(      intersectionArea(X1, Y1, R1, X2, Y2, R2));  }}Â
// This code is contributed by Pushpesh Raj. |
Javascript
// JS code to implement the approachÂ
// Function to return area of intersectionfunctionintersectionArea(X1,Y1,                 R1, X2,                 Y2, R2){    let Pi = 3.14;    let d, alpha, beta, a1, a2;    let ans;Â
    // Calculate the euclidean distance    // between the two points    d = Math.sqrt((X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1));Â
    if (d > R1 + R2)        ans = 0;Â
    else if (d <= (R1 - R2) && R1 >= R2)        ans = Math.floor(Pi * R2 * R2);Â
    else if (d <= (R2 - R1) && R2 >= R1)        ans = Math.floor(Pi * R1 * R1);Â
    else {        alpha = Math.acos((R1 * R1 + d * d - R2 * R2)                     / (2 * R1 * d))                * 2;        beta = Math.acos((R2 * R2 + d * d - R1 * R1)                    / (2 * R2 * d))               * 2;        a1 = 0.5 * beta * R2 * R2             - 0.5 * R2 * R2 * Math.sin(beta);        a2 = 0.5 * alpha * R1 * R1             - 0.5 * R1 * R1 * Math.sin(alpha);        ans = Math.floor(a1 + a2);    }Â
    return ans;}Â
// Driver CodeÂ
    let X1 = 0, Y1 = 0, R1 = 4;    let X2 = 6, Y2 = 0, R2 = 4;Â
    // Function Call    console.log(intersectionArea(X1, Y1, R1, X2, Y2, R2));     //this code is contributed by ksam24000 |
Output
7
Time Complexity: O(log N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



