Check if a circle lies inside another circle or not in C++

C++Server Side ProgrammingProgramming

Suppose we have two circles (center points, and the radius values), we have to check one circle is fit inside another circle or not. There are three possible causes.

  • The smaller circle lies completely inside the bigger one, without touching each other. In this case, the sum of the distance between the centers, and the smaller radius, is lesser than a bigger radius. So the smaller one will be inside the bigger one.

  • The second case is the smaller circle is inside the bigger ones, but also touches the circumference of the bigger circle.

  • The third case is, some part of the smaller circle is inside the bigger one.

To solve this, we have to find the distance between two centers, then using the distance and the radius values, we will determine those cases.

Example

#include <iostream>
#include <cmath>
using namespace std;
void isCircleInside(int x_big, int y_big, int x_small, int y_small, int r_big, int r_small) {
   int distSq = sqrt(((x_big - x_small) * (x_big - x_small)) + ((y_big - y_small) * (y_big - y_small)));
   if (distSq + r_small == r_big)
      cout << "Inside the bigger circle, touching circimferene" << endl;
      else if (distSq + r_small < r_big)
         cout << "Completely inside the bigger circle" << endl;
   else
      cout << "Not inside the bigger circle" << endl;
}
int main() {
   int x1 = 10, y1 = 8;
   int x2 = 1, y2 = 2;
   int r1 = 30, r2 = 10;
   isCircleInside(x1, y1, x2, y2, r1, r2);
}

Output

Completely inside the bigger circle
raja
Published on 21-Oct-2019 08:59:40
Advertisements