给定两个半径为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
重要提示:同心圆是指具有相同中心的圆。位于两个同心圆之间的区域称为环或圆环。
例子 :
有两个同心圆,其中心在原点(0,0),半径为r = 8和R = 4。
1.)圆1和2位于圆环内。
2.)圈3和圈4在圈外。
完整的图形如下所示:
方法 :
毕达哥拉斯定理可以解决这个问题。使用毕达哥拉斯(Pythagoras)定理计算圆心与原点之间的距离,假设它用“ 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)