给定两个半径给定的圆,它们在外部相互接触。任务是找到圆之间的直接公切线的长度。
例子:
Input: r1 = 5, r2 = 9
Output: 13.4164
Input: r1 = 11, r2 = 13
Output: 23.9165
方法
- 让半径分别为r1和r2 。
- 画一条线OR平行于PQ
- 角度 OPQ = 90 度
角度 O’QP = 90 度
{ 连接圆心和接触点的线与切线成 90 度角 } - 角度 OPQ + 角度 O’QP = 180
操作||二维码 - 由于对边平行且内角为90°,所以OPQR是一个矩形。
- 所以OP = QR = r1和PQ = OR = r1+r2
- 在三角形OO’R
角度 ORO’ = 90
根据毕达哥拉斯定理
OR^2 + O’R^2 = OO’^2
OO’^2 = (r1+r2)^2 + (r1-r2)^2 - 所以, OO’ = 2√(r1*r2)
下面是上述方法的实现:
C++
// C++ program to find the length of the direct
// common tangent between two circles
// which externally touch each other
#include
using namespace std;
// Function to find the length
// of the direct common tangent
void lengtang(double r1, double r2)
{
cout << "The length of the "
<< "direct common tangent is "
<< 2 * sqrt(r1 * r2) << endl;
}
// Driver code
int main()
{
double r1 = 5, r2 = 9;
lengtang(r1, r2);
return 0;
}
Java
// Java program to find the length of the direct
// common tangent between two circles
// which externally touch each other
class GFG
{
// Function to find the length
// of the direct common tangent
static void lengtang(double r1, double r2)
{
System.out.println("The length of the "
+ "direct common tangent is "
+ (2 * Math.sqrt(r1 * r2)));
}
// Driver code
public static void main(String[] args)
{
double r1 = 5, r2 = 9;
lengtang(r1, r2);
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 program to find the length
# of the direct common tangent
# between two circles which
# externally touch each other
# Function to find the length
# of the direct common tangent
def lengtang(r1, r2):
print("The length of the direct",
"common tangent is",
2 * (r1 * r2)**(1 / 2));
# Driver code
r1 = 5; r2 = 9;
lengtang(r1, r2);
# This code contributed
# by PrinciRaj1992
C#
// C# program to find the length of the direct
// common tangent between two circles
// which externally touch each other
using System;
class GFG
{
// Function to find the length
// of the direct common tangent
static void lengtang(double r1, double r2)
{
Console.WriteLine("The length of the "
+ "direct common tangent is "
+ (2 * Math.Sqrt(r1 * r2)));
}
// Driver code
static public void Main ()
{
double r1 = 5, r2 = 9;
lengtang(r1, r2);
}
}
// This code contributed by ajit.
PHP
Javascript
输出:
The length of the direct common tangent is 13.4164
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。