The Wayback Machine - https://web.archive.org/web/20240823161811/https://www.geeksforgeeks.org/check-given-circle-lies-completely-inside-ring-formed-two-concentric-circles/
Open In App

Check if a given circle lies completely inside the ring formed by two concentric circles

Last Updated : 28 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.
Examples : 

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

Important : Concentric circles are those circles which have same centre. The region lying between two concentric circles is called annulus or the circular ring. 
 

circle (1)

Example : 
There are two concentric circles with their centre at origin(0, 0) and radius as r = 8 and R = 4. 
1.) Circle 1 and 2 lies inside the ring. 
2.) Circle 3 and 4 are outside the ring.
The complete figure can be visualised as given below : 
 

circle

Approach : 
This problem can be solved using Pythagoras Theorem . Compute the distance between the centre of the circle and origin using Pythagoras theorem, suppose it is denoted by ‘dis’. 
After computing the distance just check that the value of (dis – r1)> = r and (dis + r1)< = R. If both these conditions hold then the circle lies completely inside the ring.

C++




// CPP code to check if a circle
// lies in the ring
#include <bits/stdc++.h>
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




<?php
// PHP code to check if a circle
// lies in the ring
 
// Function to check if circle
// lies in the ring
function checkcircle($r, $R, $r1,
                         $x1, $y1)
{
     
    // distance between center of circle
    // center of concentric circles(origin)
    // using Pythagoras theorem
    $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
    // 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




<script>
// JavaScript program code to check if a
// circle lies in the ring
 
// Function to check if circle
    // lies in the ring
    function checkcircle(r, R, r1, x1, y1)
    {
        // distance between center of circle
        // center of concentric circles(origin)
        // using Pythagoras theorem
        let dis = 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
 
        // Both circle with radius 'r'
        // and 'R' have center (0,0)
        let r = 8, R = 4, r1 = 2, x1 = 6, y1 = 0;
         
        if (checkcircle(r, R, r1, x1, y1))
            document.write("yes");
        else
            document.write("no");
 
// This code is contributed by splevel62.
</script>


Output: 

yes

 

Time Complexity: O(log(n)) since using inbuilt sqrt function

Auxiliary Space: O(1)



Similar Reads

Check if a circle lies inside another circle or not
Given two circles with radii and centres given. The task is to check whether the smaller circle lies inside the bigger circle or not.  Examples:   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
6 min read
Program to calculate the area between two Concentric Circles
Given two concentric circles with radius X and Y where (X &gt; Y). Find the area between them. You are required to find the area of the green region as shown in the following image: Examples: Input : X = 2, Y = 1 Output : 9.42478 Input : X = 4, Y = 2 Output : 37.6991 Approach:The area between the two given concentric circles can be calculated by su
4 min read
Count points from an array that lies inside a semi-circle
Given two pairs (X, Y), (P, Q) and R the coordinate of the center of semi-circle, coordinate of the intersection of semicircle and diameter of the semicircle and, the radius of the semicircle, and an array arr[] of dimension N*2 consisting of the coordinates of few points, the task is to find the number of points from the array that lies inside or
7 min read
Find if a point lies inside a Circle
Given a circle (coordinates of centre and radius) and a point (coordinate), find if the point lies inside or on the circle, or not. Examples : Input: x = 4, y = 4 // Given Point circle_x = 1, circle_y = 1, rad = 6; // Circle Output: Inside Input: x = 3, y = 3 // Given Point circle_x = 0, circle_y = 1, rad = 2; // Circle Output: Outside We strongly
5 min read
Check if the given point lies inside given N points of a Convex Polygon
Given coordinates of the N points of a Convex Polygon. The task is to check if the given point (X, Y) lies inside the polygon. Examples: Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)}, Query: X = 3, Y = 2 Below is the image of plotting of the given points: Output: YES Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4
15 min read
Sum of area of alternative concentric circle with radius 1,2,3,4..........N
Given that Concentric circle of radii 1,2,3,4..........N cm are drawn. The interior of the smallest circle is colored white and angular regions are colored alternately green and white so that no two adjacent regions are of the same color. The task is to find the total area of the green region in sq cm. Examples: Input: N=100Output: 15865.0429006284
5 min read
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 fo
6 min read
Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
Given two circles of given radii, such that the circles don’t touch each other, the task is to find the ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles. Examples: Input: r1 = 4, r2 = 6 Output: 2:3 Input: r1 = 22, r2 = 43 Output: 22:43 Approach: Let the radii of the
5 min read
Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
Given two circles, of given radii, such that the circles don’t touch each other. The task is to find the ratio of the distance between the centres of the circles and the point of intersection of two transverse common tangents to the circles.Examples: Input :r1 = 4, r2 = 8 Output :1:2 Input :r1 = 5, r2 = 13 Output :5:13 Approach: Let the radii of th
5 min read
Check whether a given point lies on or inside the rectangle | Set 3
Given two numbers a and b where b &lt; a form a rectangle with points (0, b), (b, 0), (a-b, b), (b, a-b). Given a point (x, y), the task is to check whether this point lies inside or on the rectangle or not. Examples: Input: a = 7, b = 2, x = 5, y = 2; Output: Given point does not lie on the rectangle Input: a = 7, b = 2, x = 4, y = 5; Output: Give
6 min read
Check whether a given point lies inside a triangle or not
Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not. Example: Input: A = (0, 0), B = (10, 30), C = (20, 0), P(10, 15)Output: InsideExplanation: B(10,30) / \ / \ / \ / P \ P' / \ A(0,0) ----------- C(20,0) Input: A = (0, 0), B = (10, 30), C = (20, 0), P(30, 15)Output: Out
15+ min read
How to check if a given point lies inside or outside a polygon?
Given a polygon and a point 'p', find if 'p' lies inside the polygon or not. The points lying on the border are considered inside. Examples: Recommended ProblemPlease solve it on PRACTICE first, before moving on to the solution Solve ProblemApproach: The idea to solve this problem is based on How to check if two given line segments intersect, and t
9 min read
Check whether a given point lies inside a rectangle or not
Given four points of a rectangle, and one more point P. Write a function to check whether P lies within the given rectangle or not.Examples:   Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)] P = (0, 0)Output : yesIllustration : Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)], P = (20, 20)Output : noIllustration : Prerequisite: Che
14 min read
Check if two circles intersect such that the third circle passes through their points of intersections and centers
Given centres and the radii of three circles A, B, and C in the form of {X, Y, R}, where (X, Y) is the centre of the circle and R is the radius of that circle. The task is to check if any two circles intersect such that the third circle passes through the intersecting points and the centres of the two circles. If found to be true, then print "Yes".
9 min read
Check whether a point lies inside a sphere or not
Given co-ordinates of the center of a sphere (cx, cy, cz) and its radius r. Our task is to check whether a point (x, y, z) lies inside, outside or on this sphere.Examples: Input : Centre(0, 0, 0) Radius 3 Point(1, 1, 1) Output :Point is inside the sphere Input :Centre(0, 0, 0) Radius 3 Point(2, 1, 2) Output :Point lies on the sphere Input :Centre(0
7 min read
Check if a point lies inside a rectangle | Set-2
Given coordinates of bottom-left and top-right corners of a rectangle. Check if a point (x, y) lies inside this rectangle or not. Examples: Input: bottom-left: (0, 0), top-right: (10, 8), point: (1, 5) Output: Yes Input: bottom-left: (-1, 4), top-right:(2, 3), point:(0, 4) Output: No This problem is already discussed in a previous post. In this pos
6 min read
Minimum Circles needed to be removed so that all remaining circles are non intersecting
Given n Circles present in x-y plane such that all the circles have their center aligned on the x-axis. The task is to remove some of them, such that no two circles are intersecting. Find the minimum number of circles that need to be removed. Note : Touching circles are also considered to be intersecting. Given N and an array of pair of integers. E
7 min read
Find a point that lies inside exactly K given squares
Given an integer K and an array arr each of whose element x represents a square with two of its vertices as (0, 0) and (x, x). The task is to find a point which lies in exactly K squares. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: (3, 3) The point (3, 3) lies inside 3rd and 4th square only. Input: arr[] = {8, 1, 55, 90}, K = 3 Output: (8,
3 min read
Program to check if N is a Concentric Hexagonal Number
Given an integer N, the task is to check if N is a Concentric Hexagonal Numbers or not. If the number N is a Concentric Hexagonal Number then print “Yes” else print “No”. Concentric Hexagonal Numbers are the number sequence forms a pattern with concentric hexagons, and the numbers denote the number of points required after the N-th stage of the pat
4 min read
Count of integral coordinates that lies inside a Square
Given lower left and upper right coordinates (x1, y1) and (x2, y2) of a square, the task is to count the number of integral coordinates that lies strictly inside the square.Examples: Input: x1 = 1, y1 = 1, x2 = 5, x3 = 5 Output: 9 Explanation: Below is the square for the given coordinates: Input: x1 = 1, y1 = 1, x2 = 4, x3 = 4 Output: 4 Approach: T
4 min read
Find if a point lies inside, outside or on the circumcircle of three points A, B, C
Given four non-collinear points A, B, C, and P. The task is to find whether point P lies inside, outside or on the circumcircle formed by points A, B, and C.Examples Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {2, 4}; Output: Point (2, 4) is inside the circumcircle. Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {3, 0}; Output: Point (3, 0) lies
15+ min read
Find the radius of the circles which are lined in a row, and distance between the centers of first and last circle is given
Given here are n circles which touch each other externally, and are lined up in a row. The distance between the centers of the first and last circle is given. The circles have a radius of equal length. The task is to find the radius of each circle. Examples: Input: d = 42, n = 4 Output: The radius of each circle is 7 Input: d = 64, n = 5 Output: Th
3 min read
Radius of a circle having area equal to the sum of area of the circles having given radii
Given two integers r1 and r2, representing the radius of two circles, the task is to find the radius of the circle having area equal to the sum of the area of the two circles having given radii. Examples: Input:r1 = 8r2 = 6Output:10Explanation:Area of circle with radius 8 = 201.061929 Area of a circle with radius 6 = 113.097335Area of a circle with
5 min read
Radii of the three tangent circles of equal radius which are inscribed within a circle of given radius
Given here is a circle of a given radius. Inside it, three tangent circles of equal radius are inscribed. The task is to find the radii of these tangent circles. Examples: Input: R = 4Output: 1.858 Input: R = 11Output: 5.1095 Approach: Let the radii of the tangent circles be r, and the radius of the circumscribing circle is R.x is the smaller dista
3 min read
Rearrange two given arrays such that sum of same indexed elements lies within given range
Given two arrays arr1[] and arr2[] consisting of N positive integers and an even integer K, the task is to check if the sum of the same indexed elements in the two arrays lie in the range [K/2, K] after rearranging the given arrays or not. If it is possible to obtain such an arrangement, then print "Yes". Otherwise, print "No". Examples: Input: arr
6 min read
Number of smaller circles that can be inscribed in a larger circle
Given two positive integers R1 and R2, where R1 and R2 represent the radius of the larger and smaller circles respectively, the task is to find out the number of smaller circles that can be placed inside the larger circle such that the smaller circle touches the boundary of the larger circle. Examples: Input: R1 = 3, R2 = 1Output: 6Explanation: The
7 min read
Print concentric rectangular pattern in a 2d matrix
Given a positive integer n, print the matrix filled with rectangle pattern as shown below: a a a a a a b b b a a b c b a a b b b a a a a a a where a = n, b = n - 1,c = n - 2 and so on. Examples: Input : n = 4 Output : 4 4 4 4 4 4 4 4 3 3 3 3 3 4 4 3 2 2 2 3 4 4 3 2 1 2 3 4 4 3 2 2 2 3 4 4 3 3 3 3 3 4 4 4 4 4 4 4 4 Input : n = 3 Output : 3 3 3 3 3 3
13 min read
Maximum value in a matrix which contain intersecting concentric submatrix
Suppose a matrix of size N X N which contain concentric square submatrices centered at (xi, yi), where xi is the row number of center of ith concentric square matrix and yi is the column number of center of ith concentric square matrix. Concentric square matrix is of the form: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 . . . . . 1 0 0 1 . b b b . 1 0
8 min read
Concentric Hexagonal Numbers
The following numbers form the concentric hexagonal sequence : 0, 1, 6, 13, 24, 37, 54, 73, 96, 121, 150 ......The number sequence forms a pattern with concentric hexagons, and the numbers denote the number of points required after the n-th stage of the pattern. Examples: Input : N = 3 Output : 13 Input : N = 4 Output : 24 Approach : The above seri
2 min read
Check if two given circles touch or intersect each other
There are two circles A and B with their centres C1(x1, y1) and C2(x2, y2) and radius R1 and R2. The task is to check both circles A and B touch each other or not. Examples : Input : C1 = (3, 4) C2 = (14, 18) R1 = 5, R2 = 8Output : Circles do not touch each other. Input : C1 = (2, 3) C2 = (15, 28) R1 = 12, R2 = 10Output : Circles intersect with eac
5 min read