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 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"; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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. |
chevron_right
filter_none
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. |
chevron_right
filter_none
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"; elseecho "Outside"; // This code is contributed // by nitin mittal. ?> |
chevron_right
filter_none
Output :
Inside
Thanks to Utkarsh Trivedi for suggesting above solution
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Find a point that lies inside exactly K given squares
- Check if a circle lies inside another circle or not
- Check whether a given point lies inside a triangle or not
- How to check if a given point lies inside or outside a polygon?
- Check whether a given point lies on or inside the rectangle | Set 3
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Check whether the point (x, y) lies on a given line
- Check if a point is inside, outside or on the parabola
- Check if a point is inside, outside or on the ellipse
- Shortest distance between a point and a circle
- Choose points from two ranges such that no point lies in both the ranges
- Find the GCD that lies in given range
- Find the other-end coordinates of diameter in a circle
- Program to find area of a circle
- Find the Diameter or Longest chord of a Circle



