📌  相关文章
📜  检查给定的圆是否位于由其他两个圆维护的边界内

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

给定外圆半径 R 和内圆半径 r,从同一圆心做圆并形成它们之间的边界。现在,给定 X, Y 坐标,它表示要形成的新圆的中心,半径为 rad,您的任务是检查以 X, Y 坐标为中心的圆是否适合所形成的圆的边界。
例子:

Input : R = 8, r = 4
x = 5, y = 3, rad = 1
Output : Fits

Input :  R = 8, r = 4
x = 5, y = 3,  rad = 3.
Output : Doesn't Fit

1 – 不适合
2 – 适合
思路是计算圆心(0, 0)与要检查的圆的坐标之间的距离。如果距离+半径(要检查的圆的)小于或等于外半径并且距离-半径(要检查的圆)大于或等于外圆的半径-半径内圆
它适合。

这是实现:

C++
// CPP program to check whether circle with given
// co-ordinates reside within the boundary
// of outer circle and inner circle
#include 
using namespace std;
 
// function to check if given circle fit in
// boundary or not
void fitOrNotFit(int R, int r, int x, int y,
                                 int rad) {
         
    // Distance from the center
    double val = sqrt(pow(x, 2) + pow(y, 2));
     
    // Checking the corners of circle
    if (val + rad <= R && val - rad >= R - r)   
        cout << "Fits\n";   
    else
        cout << "Doesn't Fit\n";
}
 
// driver program
int main()
{
    // Radius of outer circle and inner circle
    // respectively
    int R = 8, r = 4;
     
    // Co-ordinates and radius of the circle
    // to be checked
    int x = 5, y = 3, rad = 3;
    fitOrNotFit(R, r, x, y, rad);
    return 0;
}


Java
// Java program to check whether circle with given
// co-ordinates reside within the boundary
// of outer circle and inner circle
import java.util.*;
 
class GFG
{
// function to check if given circle fit in
// boundary or not
static void fitOrNotFit(int R, int r, int x, int y,
                                        int rad)
{
    // Distance from the center
    double val = Math.sqrt(Math.pow(x, 2) +
                           Math.pow(y, 2));
     
    // Checking the corners of circle
    if (val + rad <= R && val - rad >= R - r)
        System.out.println("Fits");
    else
    System.out.println("Doesn't Fit");    
}
 
// driver program
public static void main (String[] args)
{
    // Radius of outer circle and inner circle
    // respectively
    int R = 8, r = 4;
     
    // Co-ordinates and radius of the circle
    // to be checked
    int x = 5, y = 3, rad = 3;
    fitOrNotFit(R, r, x, y, rad);
}
}
/* This Code is contributed by Kriti Shukla */


Python3
# Python3 program to check
# whether circle with given
# co-ordinates reside
# within the boundary
# of outer circle
# and inner circle
 
import math
 
# function to check if
# given circle fit in
# boundary or not
def fitOrNotFit(R, r, x, y, rad) :
         
    # Distance from the center
    val = math.sqrt(math.pow(x, 2) + math.pow(y, 2))
     
    # Checking the corners of circle
    if (val + rad <= R and val - rad >= R - r) :
        print("Fits\n") 
    else:
        print("Doesn't Fit")
  
 
# driver program
  
# Radius of outer circle and inner circle
# respectively
R = 8
r = 4
     
# Co-ordinates and radius of the circle
# to be checked
x = 5
y = 3
rad = 3
 
fitOrNotFit(R, r, x, y, rad)
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to check whether circle with given
// co-ordinates reside within the boundary
// of outer circle and inner circle
using System;
 
class GFG
{
    // function to check if given circle fit in
    // boundary or not
    static void fitOrNotFit(int R, int r, int x, int y,
                                            int rad)
    {
        // Distance from the center
        double val = Math.Sqrt(Math.Pow(x, 2) +
                               Math.Pow(y, 2));
          
        // Checking the corners of circle
        if (val + rad <= R && val - rad >= R - r)
            Console.WriteLine("Fits");
        else
        Console.WriteLine("Doesn't Fit");    
    }
      
    // Driver program
    public static void Main ()
    {
        // Radius of outer circle and inner circle
        // respectively
        int R = 8, r = 4;
          
        // Co-ordinates and radius of the circle
        // to be checked
        int x = 5, y = 3, rad = 3;
        fitOrNotFit(R, r, x, y, rad);
    }
}
 
// This Code is contributed by Anant Agarwal.


PHP
= $R - $r)
        echo"Fits\n";
    else
        echo "Doesn't Fit\n";
}
 
    // Driver Code
 
    // Radius of outer circle and
    // inner circle respectively
    $R = 8; $r = 4;
     
    // Co-ordinates and radius of
    // the circle to be checked
    $x = 5; $y = 3; $rad = 3;
    fitOrNotFit($R, $r, $x, $y, $rad);
     
// This Code is contributed by vt_m.
?>


Javascript


输出:

Doesn't Fit

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