The Wayback Machine - https://web.archive.org/web/20240823160310/https://www.geeksforgeeks.org/find-if-a-point-lies-inside-or-on-circle/
Open In App

Find if a point lies inside a Circle

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

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++




// C++ program to check if a point
// lies inside a circle or not
#include <bits/stdc++.h>
using namespace std;
 
bool isInside(int circle_x, int circle_y,
                   int rad, int x, int y)
{
    // Compare radius of circle with distance
    // of its center from given point
    if ((x - circle_x) * (x - circle_x) +
        (y - circle_y) * (y - circle_y) <= rad * rad)
        return true;
    else
        return false;
}
 
// Driver function
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




// Java program to check if a point lies
// inside a circle or not
 
class GFG {
 
    static boolean isInside(int circle_x, int circle_y,
                                 int rad, int x, int y)
    {
        // Compare radius of circle with
        // distance of its center from
        // given point
        if ((x - circle_x) * (x - circle_x) +
            (y - circle_y) * (y - circle_y) <= rad * rad)
            return true;
        else
            return false;
    }
 
    // Driver Program to test above function
    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");
    }
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python3 program to check if
# a point lies inside a circle
# or not
 
def isInside(circle_x, circle_y, rad, x, y):
     
    # Compare radius of circle
    # with distance of its center
    # from given point
    if ((x - circle_x) * (x - circle_x) +
        (y - circle_y) * (y - circle_y) <= rad * rad):
        return True;
    else:
        return False;
 
# Driver Code
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");
 
# This code is contributed
# by mits.


C#




// C# program to check if a point lies
// inside a circle or not
using System;
 
class GFG {
 
    static bool isInside(int circle_x, int circle_y,
                              int rad, int x, int y)
    {
        // Compare radius of circle with
        // distance of its center from
        // given point
        if ((x - circle_x) * (x - circle_x) +
            (y - circle_y) * (y - circle_y)    <= rad * rad)
            return true;
        else
            return false;
    }
 
    // Driver Program to test above function
    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");
    }
}
 
// This code is contributed by nitin mittal.


PHP




<?php
// PHP program to check if a point
// lies inside a circle or not
 
function isInside($circle_x, $circle_y,
                          $rad, $x, $y)
{
    // Compare radius of circle
    // with distance of its center
    // from given point
    if (($x - $circle_x) * ($x - $circle_x) +
        ($y - $circle_y) * ($y - $circle_y) <=
                               $rad * $rad)
        return true;
    else
        return false;
}
 
// Driver Code
$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";
 
// This code is contributed
// by nitin mittal.
?>


Javascript




<script>
 
// Javascript program to check if a point
// lies inside a circle or not
function isInside(circle_x, circle_y, rad, x, y)
{
     
    // Compare radius of circle with
    // distance of its center from
    // given point
     
    if ((x - circle_x) * (x - circle_x) +
        (y - circle_y) * (y - circle_y) <= rad * rad)
        return true;
    else
        return false;
}
 
// Driver code
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");
}
 
// This code is contributed by bunnyram19
 
</script>


Output : 

Inside

Time complexity: O(1) because doing constant operations

Auxiliary space: O(1)

Thanks to Utkarsh Trivedi for suggesting above solution

 



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
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
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
Check if a given circle lies completely inside the ring formed by two concentric circles
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
6 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
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
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 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
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
Find minimum radius such that atleast k point lie inside the circle
Given a positive integer K, a circle center at (0, 0) and coordinates of some points. The task is to find minimum radius of the circle so that at-least k points lie inside the circle. Output the square of the minimum radius. Examples: Input : (1, 1), (-1, -1), (1, -1), k = 3Output : 2We need a circle of radius at least 2to include 3 points.Input :
12 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
Check if given point lies in range of any of the given towers
Given a 2D array arr[][] consisting of N rows of the form {Xi, Yi, Ri} such that (Xi, Yi) represents the position of a tower and Ri represents the network range of that tower. Given two integers X and Y, the task is to check if the point (X, Y) lies in the network range of the towers or not. Examples: Input: arr[][] = { {1, 1, 3}, {10, 10, 5}, {15,
7 min read
Check if given point lies on the Square Spiral Path
Given a 2D Graph, present initially at (0, 0). Print "YES" if the given point (X, Y) lies on the spiral path, otherwise "NO". The spiral path is to be followed ln a sequential manner as given below: Move one unit right side from the current position.Move two units upwards from the current position.Move three units left side from the current positio
6 min read
Check whether the point (x, y) lies on a given line
Given the values of m and c for the equation of a line y = (m * x) + c, the task is to find whether the point (x, y) lies on the given line. Examples: Input: m = 3, c = 2, x = 1, y = 5 Output: Yes m * x + c = 3 * 1 + 2 = 3 + 2 = 5 which is equal to y Hence, the given point satisfies the line's equation Input: m = 5, c = 2, x = 2, y = 5 Output: No A
4 min read
Find intersection point of lines inside a section
Given N lines in two-dimensional space in y = mx + b form and a vertical section. We need to find out whether there is an intersection point inside the given section or not. Examples: In below diagram four lines are there, L1 : y = x + 2 L2 : y = -x + 7 L3 : y = -3 L4 : y = 2x – 7 and vertical section is given from x = 2 to x = 4 We can see that in
9 min read
Find area of the larger circle when radius of the smaller circle and difference in the area is given
Given two integers r and d where r is the radius of the smaller circle and d is the difference of the area of this circle with some larger radius circle. The task is to find the area of the larger circle.Examples: Input: r = 4, d = 5 Output: 55.24 Area of the smaller circle = 3.14 * 4 * 4 = 50.24 55.24 - 50.24 = 5Input: r = 12, d = 3 Output: 455.16
4 min read
Find the angle between tangents drawn from a given external point to a Circle
Given a positive integer R representing the radius of the circle and the center of the circle (X1, Y1) and another point (X2, Y2) in the cartesian plane, the task is to find the angle between the pair of tangents drawn from the point (X2, Y2) to the circle. Examples: Input: R = 6, (X1, Y1) = (5, 1), (X2, Y2) = (6, 9)Output: 96.1851 Input: R = 4, (X
7 min read
Find time required to reach point N from point 0 according to given rules
Given two positive integers X and Y and N number of points arranged in a line, the task is to find the time required to reach point N from point 0 according to the following rules: Every point has one barrier that closes after every Y minutes and remains closed for the next Y minutes.On reaching a point if the barrier is closed, then the person nee
7 min read
Queries on count of points lie inside a circle
Given n coordinate (x, y) of points on 2D plane and Q queries. Each query contains an integer r, the task is to count the number of points lying inside or on the circumference of the circle having radius r and centered at the origin.Examples : Input : n = 5 Coordinates: 1 1 2 2 3 3 -1 -1 4 4 Query 1: 3 Query 2: 32 Output : 3 5 For first query radiu
8 min read
Check if a point is inside, outside or on a Hyperbola
Given a hyperbola centered at (h, k), with semi-major axis a, semi-minor axis b, both aligned with the Cartesian plane, the task is to determine if the point (x, y) lies within the area bounded by the hyperbola or not. Examples: Input: h = 0, k = 0, x = 2, y = 1, a = 4, b = 5 Output: Inside Input: h = 1, k = 2, x = 200, y = 100, a = 6, b = 5Output:
5 min read
Make N pairs from Array as (X, Y) coordinate point that are enclosed inside a minimum area rectangle
Given a number N, and an array A[] of size 2N, the task is to make N pairs of these array elements and place them on an X-Y coordinate plane, such that they are enclosed inside a minimum area rectangle( with sides parallel to the X-axis and Y-axis) and print the area of the rectangle. Examples: Input: N = 4, A = {1, 4, 2, 5, 3, 6, 7, 8}Output: 9Exp
7 min read
Check if a point is inside, outside or on the ellipse
Given an ellipse centered at (h, k), with semi-major axis a, and semi-minor axis b, both aligned with the Cartesian plane. The task is to determine if the point (x, y) is within the area bounded by the ellipse. Examples:  Input: h = 0, k = 0, x = 2, y = 1, a = 4, b = 5 Output: InsideInput: h = 1, k = 2, x = 200, y = 100, a = 6, b = 5Output: Outside
10 min read
Triangle with no point inside
Given N points in 2-dimensional space, we need to find three points such that triangle made by choosing these points should not contain any other points inside. All given points will not lie on the same line so solution will always exist. Examples: In above diagram possible triangle with no point inside can be formed by choosing these triplets,[(0,
15+ min read
Check if a point is inside, outside or on the parabola
Given a parabola with vertex (h, k), and [Tex]a     [/Tex], the distance between focus and vertex. The task is to determine if the point (x, y) is inside, outside or on the parabola.Examples:   Input: h = 100, k = 500, x = 20, y = 10, a = 4 Output: OutsideInput: h = 0, k = 0, x = 2, y = 1, a = 4Output: Inside     Approach: It is very simple, we hav
5 min read
Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given
Given are two congruent circles with two equal chords. The angle subtended to the center from the chord of one of the circles is given. The task is to find the angle subtended by the chord to the center of another circle.Examples: Input: z = 48 Output: 48 degrees Input: z = 93 Output: 93 degrees Approach: In triangle AOB and PXQ AO = PX(radii of co
3 min read
Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
Given a circle C1 and it's a radius r1. And one another circle C2 whose passes through center of circle C1 and touch the circumference of circle C1. The task is to find out the area of circle C2. Examples: Input: r1 = 4Output:Area of circle c2 = 12.56Input: r1 = 7Output:Area of circle c2 = 38.465 Approach: Radius r2 of circle C2 is [Tex]\ r2 = r1/2
6 min read
Equation of circle when three points on the circle are given
Given three coordinates that lie on a circle, (x1, y1), (x2, y2), and (x3, y3). The task is to find the equation of the circle and then print the centre and the radius of the circle. The equation of circle in general form is xÂē + yÂē + 2gx + 2fy + c = 0 and in radius form is (x - h)Âē + (y -k)Âē = rÂē, where (h, k) is the centre of the circle and r is
11 min read
Article Tags :
Practice Tags :