给定两个正整数R1和R2,代表两个相交圆的半径,它们的中心之间的距离为D ,任务是找到两个圆之间相交角的余弦。
例子:
Input: R1 = 3, R2 = 4, D = 5
Output: 0
Input: R1 = 7, R2 = 3, D = 6
Output: 0.52381
方法:给定的问题可以通过使用几何算法来解决,如下图所示:
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:
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
0
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。