Find if a point lies inside a Circle
Last Updated :
05 Sep, 2022
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 recommend you to minimize your browser and try this yourself first.
The idea is compute distance of point from center. If distance is less than or equal to radius. the point is inside, else outside.
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
bool isInside( int circle_x, int circle_y,
int rad, int x, int y)
{
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad * rad)
return true ;
else
return false ;
}
int main()
{
int x = 1, y = 1;
int circle_x = 0, circle_y = 1, rad = 2;
isInside(circle_x, circle_y, rad, x, y) ?
cout << "Inside" : cout << "Outside" ;
}
|
Java
class GFG {
static boolean isInside( int circle_x, int circle_y,
int rad, int x, int y)
{
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad * rad)
return true ;
else
return false ;
}
public static void main(String arg[])
{
int x = 1 , y = 1 ;
int circle_x = 0 , circle_y = 1 , rad = 2 ;
if (isInside(circle_x, circle_y, rad, x, y))
System.out.print( "Inside" );
else
System.out.print( "Outside" );
}
}
|
Python3
def isInside(circle_x, circle_y, rad, x, y):
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) < = rad * rad):
return True ;
else :
return False ;
x = 1 ;
y = 1 ;
circle_x = 0 ;
circle_y = 1 ;
rad = 2 ;
if (isInside(circle_x, circle_y, rad, x, y)):
print ( "Inside" );
else :
print ( "Outside" );
|
C#
using System;
class GFG {
static bool isInside( int circle_x, int circle_y,
int rad, int x, int y)
{
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad * rad)
return true ;
else
return false ;
}
public static void Main()
{
int x = 1, y = 1;
int circle_x = 0, circle_y = 1, rad = 2;
if (isInside(circle_x, circle_y, rad, x, y))
Console.Write( "Inside" );
else
Console.Write( "Outside" );
}
}
|
PHP
<?php
function isInside( $circle_x , $circle_y ,
$rad , $x , $y )
{
if (( $x - $circle_x ) * ( $x - $circle_x ) +
( $y - $circle_y ) * ( $y - $circle_y ) <=
$rad * $rad )
return true;
else
return false;
}
$x = 1; $y = 1;
$circle_x = 0; $circle_y = 1; $rad = 2;
if (isInside( $circle_x , $circle_y ,
$rad , $x , $y ))
echo "Inside" ;
else
echo "Outside" ;
?>
|
Javascript
<script>
function isInside(circle_x, circle_y, rad, x, y)
{
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad * rad)
return true ;
else
return false ;
}
var x = 1;
var y = 1;
var circle_x = 0;
var circle_y = 1;
var rad = 2;
if (isInside(circle_x, circle_y, rad, x, y))
{
document.write( "Inside" );
}
else
{
document.write( "Outside" );
}
</script>
|
Output :
Inside
Time complexity: O(1) because doing constant operations
Auxiliary space: O(1)
Thanks to Utkarsh Trivedi for suggesting above solution
Share your thoughts in the comments
Please Login to comment...