📌  相关文章
📜  检查给定的圆是否完全位于由两个同心圆形成的环内

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

给定两个半径为 r 和 R 的圆,它们的圆心都在原点。现在,给定另一个半径为 r1 并以 (x1, y1) 为中心的圆。检查第三个圆(半径为 r1 的圆)是否完全位于由两个半径为 r 和 R 的圆组成的圆环内。
例子 :

Input : r = 8 R = 4
        r1 = 2 x1 = 6 y1 = 0
Output : yes

Input : r = 8 R = 4 
        r1 = 2 x1 = 5 y1 = 0
Output : no

重要提示:同心圆是那些中心相同的圆。位于两个同心圆之间的区域称为环面或圆环。

圆圈 (1)

例子 :
有两个同心圆,它们的中心在原点 (0, 0) 和半径为 r = 8 和 R = 4。
1.) 圆 1 和 2 位于圆环内。
2.) 第 3 和第 4 圈在圈外。
完整的图形可以如下所示:

圆圈

方法 :
这个问题可以用毕达哥拉斯定理解决。使用勾股定理计算圆心和原点之间的距离,假设它用“dis”表示。
计算距离后,只需检查 (dis – r1)> = r 和 (dis + r1)< = R 的值。如果这两个条件都成立,则圆完全位于环内。

C++
// CPP code to check if a circle
// lies in the ring
#include 
using namespace std;
 
// Function to check if circle
// lies in the ring
bool checkcircle(int r, int R, int r1,
                        int x1, int y1)
{
    // distance between center of circle
    // center of concentric circles(origin)
    // using Pythagoras theorem
    int dis = sqrt(x1*x1+y1*y1);
     
    // Condition to check if circle is
    // strictly inside the ring
    return (dis-r1 >= R && dis+r1 <= r);
}
 
// Driver Code
int main()
{
    // Both circle with radius 'r'
    // and 'R' have center (0,0)
    int r = 8, R = 4, r1 = 2, x1 = 6, y1 = 0;   
    if (checkcircle(r, R, r1, x1, y1))
       cout << "yes" << endl;
    else
       cout << "no" << endl;
     
    return 0;
}


Java
// Java code to check if a
// circle lies in the ring
import java.io.*;
 
class ring
{
    // Function to check if circle
    // lies in the ring
    public static boolean checkcircle(int r, int R,
                            int r1, int x1, int y1)
    {
        // distance between center of circle
        // center of concentric circles(origin)
        // using Pythagoras theorem
        int dis = (int)Math.sqrt(x1 * x1 +
                                 y1 * y1);
         
         // Condition to check if circle
         // is strictly inside the ring
        return (dis - r1 >= R && dis + r1 <= r);
    }
     
    // Driver Code
    public static void main(String args[])
    {
        // Both circle with radius 'r'
        // and 'R' have center (0,0)
        int r = 8, R = 4, r1 = 2, x1 = 6, y1 = 0;
        
        if (checkcircle(r, R, r1, x1, y1))
            System.out.println("yes");
        else
            System.out.println("no");
    }
}


Python3
# Python3 code to check if
# a circle  lies in the ring
import math
 
# Function to check if circle
# lies in the ring
def checkcircle(r, R, r1, x1, y1):
 
    # distance between center of circle
    # center of concentric circles(origin)
    # using Pythagoras theorem
    dis = int(math.sqrt(x1 * x1 + y1 * y1))
     
    # Condition to check if circle is
    # strictly inside the ring
    return (dis-r1 >= R and dis+r1 <= r)
 
 
# Driver Code
 
# Both circle with radius 'r'
# and 'R' have center (0,0)
r = 8; R = 4; r1 = 2; x1 = 6; y1 = 0
if (checkcircle(r, R, r1, x1, y1)):
    print("yes")
else:
    print("no")
     
# This code is contributed by Smitha Dinesh Semwal.


C#
// C# code to check if a
// circle lies in the ring
using System;
 
class ring {
     
    // Function to check if circle
    // lies in the ring
    public static bool checkcircle(int r, int R,
                         int r1, int x1, int y1)
    {
        // distance between center of circle
        // center of concentric circles(origin)
        // using Pythagoras theorem
        int dis = (int)Math.Sqrt(x1 * x1 + y1 * y1);
 
        // Condition to check if circle
        // is strictly inside the ring
        return (dis - r1 >= R && dis + r1 <= r);
    }
 
    // Driver Code
    public static void Main()
    {
        // Both circle with radius 'r'
        // and 'R' have center (0, 0)
        int r = 8, R = 4, r1 = 2, x1 = 6, y1 = 0;
 
        if (checkcircle(r, R, r1, x1, y1))
            Console.WriteLine("yes");
        else
            Console.WriteLine("no");
    }
}
 
// This code is contributed by vt_m.


PHP
= $R && $dis + $r1 <= $r);
}
 
    // Driver Code
    // Both circle with radius 'r'
    // and 'R' have center (0,0)
    $r = 8; $R = 4;
    $r1 = 2; $x1 = 6;
    $y1 = 0;
    if (checkcircle($r, $R, $r1, $x1, $y1))
     
    echo "yes" ,"\n";
    else
    echo "no" ,"\n";
     
// This code is contributed by ajit.
?>


Javascript


输出:
yes

时间复杂度: O(1)

辅助空间: O(1)