📜  圆心到弦的最短距离

📅  最后修改于: 2021-10-23 08:21:42             🧑  作者: Mango

给定一个里面有弦的圆。给出弦长和圆的半径。任务是找到从弦到中心的最短距离。
例子:

Input: r = 4, d = 3 
Output: 3.7081

Input: r = 9.8, d = 7.3
Output: 9.09492

方法

  • 我们知道从弦上中心垂下的线段将弦平分。这条线是弦的垂直平分线,我们知道垂直距离是最短的距离,所以我们的任务是找到这条垂直平分线的长度。

  • 让圆的半径 = r
  • 弦长 = d
  • 所以,在三角形OBC 中
    毕达哥拉斯定理
    OB^2 + (d/2)^2 = r^2
    所以, OB = √(r^2 – d^2/4)
  • 所以, The shortest distance from the chord to centre = sqrt(r^2 - d^2/4)

下面是上述方法的实现:

C++
// C++ program to find
// the shortest distance from
// chord to the centre of circle
#include 
using namespace std;
 
// Function to find the shortest distance
void shortdis(double r, double d)
{
    cout << "The shortest distance "
         << "from the chord to centre "
         << sqrt((r * r) - ((d * d) / 4))
         << endl;
}
 
// Driver code
int main()
{
    double r = 4, d = 3;
    shortdis(r, d);
    return 0;
}


Java
// Java program to find
// the shortest distance from
// chord to the centre of circle
class GFG
{
     
// Function to find the shortest distance
static void shortdis(double r, double d)
{
    System.out.println("The shortest distance "
        + "from the chord to centre "
        + (Math.sqrt((r * r) - ((d * d) / 4))));
}
 
// Driver code
public static void main(String[] args)
{
    double r = 4, d = 3;
    shortdis(r, d);
}
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python program to find
# the shortest distance from
# chord to the centre of circle
 
# Function to find the shortest distance
def shortdis(r, d):
    print("The shortest distance ",end="");
    print("from the chord to centre ",end="");
    print(((r * r) - ((d * d) / 4))**(1/2));
 
# Driver code
r = 4;
d = 3;
shortdis(r, d);
 
# This code has been contributed by 29AjayKumar


C#
// C# program to find
// the shortest distance from
// chord to the centre of circle
using System;
 
class GFG
{
     
    // Function to find the shortest distance
    static void shortdis(double r, double d)
    {
        Console.WriteLine("The shortest distance "
            + "from the chord to centre "
            + (Math.Sqrt((r * r) - ((d * d) / 4))));
    }
     
    // Driver code
    public static void Main()
    {
        double r = 4, d = 3;
        shortdis(r, d);
    }
}
 
// This code is contributed by AnkitRai01


PHP


Javascript


输出:
The leshortest distance from the chord to centre 3.7081

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程