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;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.
Approach:Â
Here three cases can come,Â
Â
- The smaller circle lies completely inside the bigger circle without touching each other at a point of circumference.Â
If this case happens, the sum of the distance between the centres and smaller radius is lesser than the bigger radius, then obviously the smaller circle lies completely inside the circle, without touching the circumference.
- The smaller circle lies completely inside the bigger circle with touching each other at a point of the circumference. If this case happens, the sum of the distance between the centres and smaller radius is equal to the bigger radius, then obviously the smaller circle lies completely inside the circle, with touching the circumference.
- The smaller does not lies inside the bigger circle completely.If this case happens, then sum of the distance between the centers and smaller radius is greater than the bigger radius, then obviously the smaller circle does not lies completely inside the circle.
Below is the implementation of the above approach:Â Â
CPP
#include <bits/stdc++.h>
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;
}
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
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." ) ;
}
}
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);
}
}
|
Python
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 point of circumference. " )
elif (distSq + r2 < r1):
print ( "The smaller circle lies completely"
" inside the bigger circle without"
" touching each other "
"at a point of circumference. " )
else :
print ( "The smaller does not lies inside"
" the bigger circle completely." )
x1 ,y1 = 10 , 8
x2 ,y2 = 1 , 2
r1 ,r2 = 30 , 10
circle(x1, y1, x2, y2, r1, r2)
|
C#
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." ) ;
}
}
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);
}
}
|
Javascript
<script>
function circle(x1 , y1 , x2,
y2 , r1 , r2)
{
var distSq = parseInt(Math.sqrt(((x1 - x2)
* (x1 - x2))
+ ((y1 - y2)
* (y1 - y2))));
if (distSq + r2 == r1)
{
document.write( "The smaller circle lies completely"
+ " inside the bigger circle with "
+ "touching each other "
+ "at a point of circumference. " ) ;
}
else if (distSq + r2 < r1)
{
document.write( "The smaller circle lies completely"
+ " inside the bigger circle without"
+ " touching each other "
+ "at a point of circumference." ) ;
}
else
{
document.write( "The smaller does not lies inside"
+ " the bigger circle completely." ) ;
}
}
var x1 = 10, y1 = 8;
var x2 = 1, y2 = 2;
var r1 = 30, r2 = 10;
circle(x1, y1, x2, y2, r1, r2);
</script>
|
Output:
Â
The smaller circle lies completely inside the bigger circle without touching each other at a point of circumference.Â
Â
Time Complexity: O(logn) because inbuilt sqrt function is being used
Auxiliary Space: O(1)
Last Updated :
07 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...