Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
#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
Advertisements
