Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmAngle of intersection of two circles having their centers D distance apart

Angle of intersection of two circles having their centers D distance apart

Given two positive integers R1 and R2 representing the radius of two intersecting circles having a distance D between their centers, the task is to find the cosine of the angle of intersection between the two circles.

Examples:

Input: R1 = 3, R2 = 4, D = 5
Output: 0

Input: R1 = 7, R2 = 3, D = 6
Output: 0.52381

Approach: The given problem can be solved by using the Geometric Algorithm as illustrated below:

From the above image and using the Pythagoras Theorem, the cosine of the angle of intersection of the two circles can be found using the formula:

cos \theta = \frac{R1^2 + R2^2 - D^2}{2 * R1 * R2}

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the cosine of the
// angle of the intersection of two
// circles with radius R1 and R2
float angle(float R1, float R2, float D)
{
    float ans = (R1 * R1 + R2 * R2 - D * D)
                / (2 * R1 * R2);
 
    // Return the cosine of the angle
    return ans;
}
 
// Driver Code
int main()
{
    float R1 = 3, R2 = 4;
    float D = 5;
    cout << angle(R1, R2, D);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the cosine of the
// angle of the intersection of two
// circles with radius R1 and R2
static float angle(float R1, float R2, float D)
{
    float ans = (R1 * R1 + R2 * R2 - D * D) /
               (2 * R1 * R2);
                
    // Return the cosine of the angle
    return ans;
}
 
// Driver Code
public static void main (String[] args)
{
    float R1 = 3, R2 = 4;
    float D = 5;
     
    System.out.println(angle(R1, R2, D));
}
}
 
// This code is contributed by Ankita saini


Python3




# Python3 program for the above approach
 
# Function to find the cosine of the
# angle of the intersection of two
# circles with radius R1 and R2
def angle(R1, R2, D):
     
    ans = ((R1 * R1 + R2 * R2 - D * D) /
            (2 * R1 * R2))
 
    # Return the cosine of the angle
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    R1 = 3
    R2 = 4
    D = 5
     
    print(angle(R1, R2, D))
     
# This code is contributed by ipg2016107


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the cosine of the
// angle of the intersection of two
// circles with radius R1 and R2
static float angle(float R1, float R2, float D)
{
    float ans = (R1 * R1 + R2 * R2 - D * D) /
               (2 * R1 * R2);
                
    // Return the cosine of the angle
    return ans;
}
 
// Driver Code
public static void Main(string[] args)
{
    float R1 = 3, R2 = 4;
    float D = 5;
     
    Console.Write(angle(R1, R2, D));
}
}
 
// This code is contributed by rutvik_56.


Javascript




<script>
 
// Javascript program for the above approach 
 
// Function to find the cosine of the
// angle of the intersection of two
// circles with radius R1 and R2
function angle(R1, R2, D)
{
    var ans = (R1 * R1 + R2 * R2 - D * D) /
               (2 * R1 * R2);
 
    // Return the cosine of the angle
    return ans;
}
 
// Driver Code
var R1 = 3, R2 = 4;
var D = 5;
 
document.write(angle(R1, R2, D));
 
// This code is contributed by Ankita saini
 
</script>


Output: 

0

 

Time Complexity: O(1)
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 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments