📜  点到圆的最短距离

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

给定一个给定半径的圆,其圆心在坐标平面中的特定位置。在坐标平面中,给出了另一个点。任务是找到点和圆之间的最短距离。
例子:

Input: x1 = 4, y1 = 6, x2 = 35, y2 = 42, r = 5 
Output: 42.5079

Input: x1 = 0, y1 = 0, x2 = 5, y2 = 12, r = 3
Output: 10

方法

  • 让圆的半径 = r

  • 圆心坐标 = (x1, y1)
  • 点的坐标 = (x2, y2)
  • 让中心与点之间的距离= d
  • 由于线 AC 在 B 处与圆相交,所以最短距离将是 BC,
    等于(dr)
  • 这里使用距离公式,
    d = √((x2-x1)^2 – (y2-y1)^2)
  • 所以BC = √((x2-x1)^2 – (y2-y1)^2) – r
  • 所以,
    Shortest distance between the point and the circle = sqrt((x2-x1)^2 - (y2-y1)^2) - r

下面是上述方法的实现:

C++
// C++ program to find
// the Shortest distance
// between a point and
// a circle
#include 
using namespace std;
 
// Function to find the shortest distance
void dist(double x1, double y1, double x2, double y2, double r)
{
    cout << "The shortest distance "
         << "between a point and a circle is "
         << sqrt((pow((x2 - x1), 2))
                 + (pow((y2 - y1), 2)))
                - r
         << endl;
}
 
// Driver code
int main()
{
    double x1 = 4, y1 = 6,
           x2 = 35, y2 = 42, r = 5;
    dist(x1, y1, x2, y2, r);
    return 0;
}


Java
// Java program to find
// the Shortest distance
// between a point and
// a circle
class GFG
{
 
// Function to find the shortest distance
static void dist(double x1, double y1, double x2,
                                double y2, double r)
{
    System.out.println("The shortest distance "
            + "between a point and a circle is "
            + (Math.sqrt((Math.pow((x2 - x1), 2))
                    + (Math.pow((y2 - y1), 2)))
            - r));
}
 
// Driver code
public static void main(String[] args)
{
    double x1 = 4, y1 = 6,
            x2 = 35, y2 = 42, r = 5;
    dist(x1, y1, x2, y2, r);
}
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python program to find
# the Shortest distance
# between a point and
# a circle
  
# Function to find the shortest distance
def dist(x1, y1, x2, y2, r):
    print("The shortest distance between a point and a circle is "
    ,((((x2 - x1)** 2) + ((y2 - y1)** 2))**(1/2)) - r);
 
  
# Driver code
x1 = 4;
y1 = 6;
x2 = 35;
y2 = 42;
r = 5;
dist(x1, y1, x2, y2, r);
 
 
# This code has been contributed by 29AjayKumar


C#
// C# program to find the Shortest distance
// between a point and a circle
using System;
 
class GFG
{
 
// Function to find the shortest distance
static void dist(double x1, double y1, double x2,
                                double y2, double r)
{
    Console.WriteLine("The shortest distance "
            + "between a point and a circle is "
            + (Math.Sqrt((Math.Pow((x2 - x1), 2))
                    + (Math.Pow((y2 - y1), 2)))
            - r));
}
 
// Driver code
public static void Main(String[] args)
{
    double x1 = 4, y1 = 6,
            x2 = 35, y2 = 42, r = 5;
    dist(x1, y1, x2, y2, r);
}
}
 
/* This code contributed by PrinciRaj1992 */


PHP


Javascript


输出:
The shortest distance between a point and a circle is 42.5079

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