给定两个正整数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 Theorm, 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;
}
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
输出:
0
时间复杂度: O(1)
辅助空间: O(1)