Check whether given circle resides in boundary maintained by two other circles
Given outer circle radius R and inner circle radius r, making circles from the same center and forming the boundary between them. Now, given X and Y coordinates which denote the center of the new circle to be formed with radius rad, your task is to check whether the circle with coordinates X and Y as the center can fit in the boundary of circles formed or not.
Examples:
Input: R = 8, r = 4, x = 5, y = 3, rad = 1
Output: Fits
Input: R =9, 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
Approach: The idea is to calculate the distance between the center (0, 0) and the coordinates of the circle to be checked.
- If
- distance + radius (of the circle to be checked) is less than or equal to Outer Radius and
- distance – radius (of the circle to be checked) is greater than or equal to Radius Inner circle,
- It will fit
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void fitOrNotFit( int R, int r, int x, int y,
int rad) {
double val = sqrt ( pow (x, 2) + pow (y, 2));
if (val + rad <= R && val - rad >= r)
cout << "Fits\n" ;
else
cout << "Doesn't Fit\n" ;
}
int main()
{
int R = 8, r = 4;
int x = 5, y = 3, rad = 3;
fitOrNotFit(R, r, x, y, rad);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void fitOrNotFit( int R, int r, int x, int y,
int rad)
{
double val = Math.sqrt(Math.pow(x, 2 ) +
Math.pow(y, 2 ));
if (val + rad <= R && val - rad >= r)
System.out.println( "Fits" );
else
System.out.println( "Doesn't Fit" );
}
public static void main (String[] args)
{
int R = 8 , r = 4 ;
int x = 5 , y = 3 , rad = 3 ;
fitOrNotFit(R, r, x, y, rad);
}
}
|
Python3
C#
PHP
Javascript
Time Complexity: O(log n) since using inbuilt sqrt and pow function
Auxiliary Space: O(1)
Last Updated :
03 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...