📌  相关文章
📜  检查一个圆是否在另一个圆内

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

给定两个给定半径和中心的圆。任务是检查较小的圆圈是否在较大的圆圈内。
例子:

Input: x1 = 10, y1 = 8, x2 = 1, y2 = 2, r1 = 30, r2 = 10 
Output: The smaller circle lies completely inside
 the bigger circle without touching each other
 at a point of circumference. 


Input :x1 = 7, y1 = 8;x2 = 3, y2 = 5;r1 = 30, r2 = 25
Output :The smaller circle lies completely inside
 the bigger circle with touching each other
 at a point of circumference.

方法
这里可以出现三种情况,

  • 较小的圆完全位于较大的圆内,在圆周的一点上没有相互接触。
    如果发生这种情况,圆心与小半径的距离之和小于大半径,那么显然小圆完全位于圆内,不接触圆周。

  • 较小的圆圈完全位于较大的圆圈内,并在圆周的一个点上相互接触。如果发生这种情况,圆心与小半径的距离之和等于大半径,那么显然小圆完全位于圆内,与圆周相接。

  • 较小的并不完全位于较大的圆内。如果发生这种情况,则圆心与较小半径的距离之和大于较大的半径,那么显然较小的圆并不完全位于圆内。

下面是上述方法的实现:

CPP
// C++ program to check if one circle
// lies inside another circle or not.
 
#include 
using namespace std;
 
void circle(int x1, int y1, int x2,
            int y2, int r1, int r2)
{
    int distSq = sqrt(((x1 - x2)
                       * (x1 - x2))
                      + ((y1 - y2)
                         * (y1 - y2)));
 
    if (distSq + r2 == r1)
        cout << "The smaller circle lies completely"
             << " inside the bigger circle with "
             << "touching each other "
             << "at a point of circumference. "
             << endl;
    else if (distSq + r2 < r1)
        cout << "The smaller circle lies completely"
             << " inside the bigger circle without"
             << " touching each other "
             << "at a point of circumference. "
             << endl;
    else
        cout << "The smaller does not lies inside"
             << " the bigger circle completely."
             << endl;
}
 
// Driver code
int main()
{
    int x1 = 10, y1 = 8;
    int x2 = 1, y2 = 2;
    int r1 = 30, r2 = 10;
    circle(x1, y1, x2, y2, r1, r2);
 
    return 0;
}


Java
// Java program to check if one circle
// lies inside another circle or not.
import java.io.*;
 
class GFG
{
         
    static void circle(int x1, int y1, int x2,
                int y2, int r1, int r2)
    {
        int distSq = (int)Math.sqrt(((x1 - x2)
                                    * (x1 - x2))
                                    + ((y1 - y2)
                                    * (y1 - y2)));
     
        if (distSq + r2 == r1)
        {
            System.out.println("The smaller circle lies completely"
                + " inside the bigger circle with "
                + "touching each other "
                + "at a point of circumference. ") ;
        }
                 
        else if (distSq + r2 < r1)
        {
            System.out.println("The smaller circle lies completely"
                + " inside the bigger circle without"
                + " touching each other "
                + "at a point of circumference.") ;
        }
                 
        else
        {
            System.out.println("The smaller does not lies inside"
                + " the bigger circle completely.") ;
        }
                 
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int x1 = 10, y1 = 8;
        int x2 = 1, y2 = 2;
        int r1 = 30, r2 = 10;
        circle(x1, y1, x2, y2, r1, r2);
    }
}
 
// This code is contributed by ajit_00023.


Python
# Python3 program to check if one circle
# lies inside another circle or not.
 
def circle(x1, y1, x2,y2, r1, r2):
 
    distSq = (((x1 - x2)* (x1 - x2))+ ((y1 - y2)* (y1 - y2)))**(.5)
 
    if (distSq + r2 == r1):
        print("The smaller circle lies completely"
            " inside the bigger circle with "
            "touching each other "
            "at a poof circumference. ")
    elif (distSq + r2 < r1):
        print("The smaller circle lies completely"
            " inside the bigger circle without"
            " touching each other "
            "at a poof circumference. ")
    else:
        print("The smaller does not lies inside"
            " the bigger circle completely.")
 
# Driver code
x1 ,y1 = 10,8
x2 ,y2 = 1, 2
r1 ,r2 = 30,10
circle(x1, y1, x2, y2, r1, r2)
 
# This code is contributed by mohit kumar 29


C#
// C# program to check if one circle
// lies inside another circle or not.
using System;
 
class GFG
{
     
    static void circle(int x1, int y1, int x2,
                int y2, int r1, int r2)
    {
        int distSq = (int)Math.Sqrt(((x1 - x2)
                        * (x1 - x2))
                        + ((y1 - y2)
                            * (y1 - y2)));
     
        if (distSq + r2 == r1)
        {
            Console.WriteLine("The smaller circle lies completely"
                + " inside the bigger circle with "
                + "touching each other "
                + "at a point of circumference. ") ;
        }
                 
        else if (distSq + r2 < r1)
        {
            Console.WriteLine("The smaller circle lies completely"
                + " inside the bigger circle without"
                + " touching each other "
                + "at a point of circumference.") ;
        }
                 
        else
        {
            Console.WriteLine("The smaller does not lies inside"
                + " the bigger circle completely.") ;
        }
                 
    }
     
    // Driver code
    static public void Main ()
    {
        int x1 = 10, y1 = 8;
        int x2 = 1, y2 = 2;
        int r1 = 30, r2 = 10;
        circle(x1, y1, x2, y2, r1, r2);
    }
}
 
// This code is contributed by AnkitRai01
PHP 


Javascript


输出:
较小的圆完全位于较大的圆内,在圆周的一点上没有相互接触。

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