Find if a point lies inside a Circle in C++


Suppose, one circle is given (the center coordinate and radius), another point is also given. We have to find whether the point is inside the circle or not. To solve it, we have to find the distance of the given point from circle center. If that distance is less or equal to the radius, then that is inside the circle, otherwise not.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
bool isInsideCircle(int cx, int cy, int r, int x, int y) {
   int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
   if ( dist <= r * r)
      return true;
   else
      return false;
}
int main() {
   int x = 4, y = 4, cx = 1, cy = 1, rad = 6;
   if(isInsideCircle(cx, cy, rad, x, y)){
      cout <<"Inside Circle";
   } else {
      cout <<"Outside Circle";
   }
}

Output

Inside Circle

Updated on: 21-Oct-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements