Open In App

Area of intersection of two Circles

Last Updated : 23 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 intersection
long long int
intersectionArea(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 Code
int 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 intersection
def 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 Code
if __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 approach
using 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 intersection
function
intersectionArea(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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads