# 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 same center and forming boundary between them. Now, given X,Y co-ordinates which denotes center of new circle to be formed with radius rad, your task is to check whether the circle with co-ordinate X,Y as 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 = 8, r = 4 x = 5, y = 3, rad = 3. Output : Doesn't Fit

1 – Doesn’t fits

2 – Fits

The idea is to calculate the distance between the center (0, 0) and the co-ordinates 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 of Outer circle – Radius Inner circle

It fits.

Here is the implementation :

## C++

`// CPP program to check whether circle with given ` `// co-ordinates reside within the boundary ` `// of outer circle and inner circle ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 */` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check whether ` `// circle with given co-ordinates ` `// reside within the boundary ` `// of outer circle and inner circle ` ` ` `// function to check if given ` `// circle fit in boundary or not ` `function` `fitOrNotFit(` `$R` `, ` `$r` `, ` `$x` `, ` `$y` `, ` ` ` `$rad` `) ` `{ ` ` ` ` ` `// Distance from the center ` ` ` `$val` `= sqrt(pow(` `$x` `, 2) + pow(` `$y` `, 2)); ` ` ` ` ` `// Checking the corners of circle ` ` ` `if` `(` `$val` `+ ` `$rad` `<= ` `$R` `&& ` `$val` `- ` ` ` `$rad` `>= ` `$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. ` `?> ` |

*chevron_right*

*filter_none*

Output:

Doesn't Fit

This article is contributed by **Rohit Thapliyal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Check if a given circle lies completely inside the ring formed by two concentric circles
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
- Radii of the three tangent circles of equal radius which are inscribed within a circle of given radius
- Check if a circle lies inside another circle or not
- Check if two given circles touch or intersect each other
- Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
- Queries to check if it is possible to join boxes in a circle
- Check if a line touches or intersects a circle
- Check whether it is possible to join two points given on circle such that distance between them is k
- Check whether a point exists in circle sector or not.
- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Equation of circle when three points on the circle are given
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Path in a Rectangle with Circles